package com.ecep.contract.service.tasker;

import java.util.Locale;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.ds.contract.service.ContractService;
import com.ecep.contract.ds.contract.tasker.ContractVerifyComm;
import com.ecep.contract.ds.contract.model.Contract;
import com.ecep.contract.ui.Tasker;
import com.fasterxml.jackson.databind.JsonNode;

import lombok.Getter;
import lombok.Setter;

public class ContractVerifyTasker extends Tasker<Object> implements WebSocketServerTasker {
    @Getter
    @Setter
    private Contract contract;
    ContractVerifyComm comm = new ContractVerifyComm();
    @Getter
    @Setter
    boolean passed = true;

    @Override
    protected Object execute(MessageHolder holder) {
        updateTitle("验证合同 " + contract.getCode() + " 及其子合同是否符合合规要求");
        updateProgress(1, 10);
        if (!comm.verify(contract, holder)) {
            passed = false;
        }
        updateProgress(9, 10);
        if (passed) {
            holder.info("合规验证通过");
        } else {
            holder.error("合规验证不通过");
        }
        updateProgress(10, 10);
        updateProperty("passed", passed);
        return null;
    }

    public Locale getLocale() {
        return comm.getLocale();
    }

    public void setLocale(Locale locale) {
        comm.setLocale(locale);
    }

    @Override
    public void init(JsonNode argsNode) {
        int contractId = argsNode.get(0).asInt();
        contract = getCachedBean(ContractService.class).getById(contractId);
    }

}
