package com.brilliance.isc.doc.textcompare;

import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class TextComparer {
    public CompareResult compareGitText(String leftText,String rightText){
        diff_match_patch dmp = new diff_match_patch();
        CompareResult compareResult = new CompareResult();
        LinkedList<diff_match_patch.Diff> diffs = dmp.diff_main(leftText,rightText);
        dmp.diff_cleanupSemantic(diffs);
        AtomicInteger counter = new AtomicInteger();
        diffs.forEach(diff -> {
            if(!diff.operation.equals(diff_match_patch.Operation.EQUAL)){
                counter.incrementAndGet();
            }
        });
        //设置差异
        compareResult.setEditCount(counter.intValue());
        compareResult.setDiffs(diffs);
        String html = dmp.diff_prettyHtml(diffs)
                .replaceAll("&para;","")
                .replaceAll("&amp;nbsp;","&nbsp;");
        compareResult.setResultHtml(html);
        return compareResult;
    }
}
