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

import static com.ecep.contract.SpringApp.getBean;

import java.io.File;

import org.springframework.util.StringUtils;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.SpringApp;
import com.ecep.contract.ds.company.CompanyFileUtils;
import com.ecep.contract.ds.company.service.CompanyService;
import com.ecep.contract.ds.contract.service.ContractService;
import com.ecep.contract.ds.project.service.ProjectService;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.Contract;
import com.ecep.contract.model.ContractCatalog;
import com.ecep.contract.model.Project;

import lombok.Data;
import lombok.Setter;

@Data
public class ContractRepairComm {
    @Setter
    private ProjectService projectService;
    @Setter
    private CompanyService companyService;
    @Setter
    private ContractService contractService;

    public ContractService getContractService() {
        if (contractService == null) {
            contractService = getBean(ContractService.class);
        }
        return contractService;
    }

    public ProjectService getProjectService() {
        if (projectService == null) {
            projectService = SpringApp.getBean(ProjectService.class);
        }
        return projectService;
    }

    private CompanyService getCompanyService() {
        if (companyService == null) {
            companyService = SpringApp.getBean(CompanyService.class);
        }
        return companyService;
    }

    /**
     * 创建合同目录
     *
     * @param contract 合同对象
     * @return 合同目录
     */
    public boolean makePathAbsent(Contract contract, MessageHolder holder) {
        if (CompanyFileUtils.exists(contract.getPath())) {
            return false;
        }
        File dir = makePath(contract, holder);
        if (dir == null) {
            return false;
        }
        if (!dir.exists()) {
            return false;
        }
        contract.setPath(dir.getAbsolutePath());
        return true;
    }

    public Contract save(Contract contract) {
        return getContractService().save(contract);
    }

    public Project save(Project project) {
        return getProjectService().save(project);
    }

    public File makePath(Contract contract, MessageHolder holder) {
        File parentDir = null;
        String contractDirName = contract.getCode();

        if (getContractService().isSubContract(contract)) {
            // 子合同，需要在主合同目录下
            String parentCode = contract.getParentCode();
            if (!StringUtils.hasText(parentCode)) {
                return null;
            }

            Contract parent = getContractService().findByCode(parentCode);
            if (parent == null) {
                return null;
            }

            String parentPath = parent.getPath();
            if (!StringUtils.hasText(parentPath)) {
                if (makePathAbsent(parent, holder)) {
                    parent = save(parent);
                    parentPath = parent.getPath();
                } else {
                    return null;
                }
            }
            parentDir = new File(parentPath);
        } else {
            // 主合同
            ContractCatalog catalog = getContractService().findContractCatalogByContract(contract);
            if (catalog == null) {
                return null;
            }

            File catalogPath = getContractService().getContractCatalogPath(catalog, contract);
            if (catalogPath == null) {
                return null;
            }
            parentDir = catalogPath;
            contractDirName = contract.getCode() + "-" + CompanyFileUtils.escapeFileName(contract.getName());
        }

        File path = new File(parentDir, contractDirName);
        if (!path.exists()) {
            if (!path.mkdir()) {
                //
                holder.warn("unable make directory = " + path.getAbsolutePath());
                return null;
            }
        }
        return path;
    }

    public Project findProjectById(Integer projectId) {
        return getProjectService().findById(projectId);
    }

    public Company findCompanyById(Integer companyId) {
        return getCompanyService().findById(companyId);
    }

    public Contract findContractByGuid(String guid) {
        return contractService.findByGuid(guid);
    }


}
