package com.ecep.contract.ds.contract.tasker;

import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.ds.contract.service.ContractService;
import com.ecep.contract.model.Contract;
import com.ecep.contract.ui.Tasker;

import lombok.Getter;
import lombok.Setter;

public class ContractVerifyTasker extends Tasker<Object> {

    @Getter
    @Setter
    private Contract contract;
    ContractVerifyComm comm = new ContractVerifyComm();

    AtomicBoolean verified = new AtomicBoolean(true);

    @Override
    public Object call() throws Exception {
        updateTitle("验证合同 " + contract.getCode() + " 及其子合同是否符合合规要求");
        return execute(new MessageHolderImpl() {
            @Override
            public void addMessage(Level level, String message) {
                super.addMessage(level, message);
                if (level.intValue() > Level.INFO.intValue()) {
                    verified.set(false);
                }
            }
        });
    }

    @Override
    protected Object execute(MessageHolder holder) {
        comm.verify(contract, holder);
        if (verified.get()) {
            updateMessage(Level.CONFIG, "合规验证通过");
        } else {
            updateMessage(Level.SEVERE, "合规验证不通过");
        }
        return null;
    }


    public void setContractService(ContractService contractService) {
        comm.setContractService(contractService);
    }

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

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