package com.qingyun.service.compare.strategy;

import com.qingyun.service.compare.DiffRegion;
import com.qingyun.service.domain.ContractCompareResult;
import com.qingyun.service.domain.ContractCompareTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;


/**
 * 抽象模板类
 */
public abstract class AbstractFileComparisonStrategy implements FileComparisonStrategy {

    protected Logger log = LoggerFactory.getLogger(getClass());

    /**
     * 提取文件内容（如文本、结构等）
     *
     * @param task
     */
    @Override
    public void extractContent(ContractCompareTask task) {
        doExtract(task);
    }

    /**
     * 执行比对逻辑
     *
     * @param task
     * @return DiffRegion 列表
     */
    @Override
    public List<DiffRegion> compare(ContractCompareTask task) {
        return doCompare(task);
    }

    /**
     * 组装并保存比对结果
     *
     * @param task
     * @param diffRegions
     */
    @Override
    public void assembleAndSaveResult(ContractCompareTask task, List<DiffRegion> diffRegions) {
        List<ContractCompareResult> results = new ArrayList<>();
        for (DiffRegion change : diffRegions) {
            results.add(buildResult(task, change));
        }
        saveResults(results);
        updateTaskStatus(task, 3, "任务执行完成");
    }

    // 子类实现具体逻辑
    protected abstract void doExtract(ContractCompareTask task);
    protected abstract List<DiffRegion> doCompare(ContractCompareTask task);
    protected abstract ContractCompareResult buildResult(ContractCompareTask task, DiffRegion change);
    protected abstract void saveResults(List<ContractCompareResult> results);
    protected abstract void updateTaskStatus(ContractCompareTask task, int status, String msg);
}
