package com.ecep.contract.cloud.u8.ctx;

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

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

import org.springframework.util.StringUtils;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.constant.CloudServiceConstant;
import com.ecep.contract.constant.CloudYuConstant;
import com.ecep.contract.ds.company.service.CompanyOldNameService;
import com.ecep.contract.ds.company.service.CompanyService;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyOldName;

import lombok.Setter;

public class CompanyCtx extends AbstractYongYouU8Ctx {
    @Setter
    private CompanyOldNameService companyOldNameService;

    CompanyOldNameService getCompanyOldNameService() {
        if (companyOldNameService == null) {
            companyOldNameService = getBean(CompanyOldNameService.class);
        }
        return companyOldNameService;
    }

    public boolean updateCompanyNameIfAbsent(Company company, String name, MessageHolder holder) {
        if (!StringUtils.hasText(name)) {
            return false;
        }
        if (StringUtils.hasText(company.getName())) {
            if (Objects.equals(company.getName(), name)) {
                return false;
            }
            appendOldNameIfAbsent(company, name, false, holder);
            return false;
        } else {
            return updateText(company::getName, company::setName, name, holder, "公司名称");
        }
    }


    public boolean updateCompanyAbbNameIfAbsent(Company company, String abbName, MessageHolder holder) {
        if (!StringUtils.hasText(abbName)) {
            return false;
        }
        // 简称与公司名相同，跳过
        if (Objects.equals(company.getName(), abbName)) {
            return false;
        }
        if (StringUtils.hasText(company.getShortName())) {
            if (Objects.equals(company.getShortName(), abbName)) {
                return false;
            }
            appendOldNameIfAbsent(company, abbName, true, holder);
            return false;
        } else {
            return updateText(company::getShortName, company::setShortName, abbName, holder, "公司简称");
        }
    }

    private void appendOldNameIfAbsent(Company company, String name, boolean ambiguity, MessageHolder holder) {
        List<CompanyOldName> oldNames = getCompanyOldNameService().findAllByCompanyAndName(company, name);
        if (oldNames.isEmpty()) {
            CompanyOldName companyOldName = getCompanyOldNameService().createNew(company, name, ambiguity);
            companyOldName.setMemo("来自" + CloudServiceConstant.U8_NAME);
            getCompanyOldNameService().save(companyOldName);
            holder.info("新增曾用名：" + name);
        }
    }

    public boolean syncCompany(Company company, MessageHolder holder) {
        return false;
    }

    public Company findOrCreateByNameOrAbbName(String name, String abbName, LocalDate developDate, MessageHolder holder) {
        Company company = getCompanyService().findAndRemoveDuplicateCompanyByNameOrAbbName(name, abbName);
        if (company != null) {
            return company;
        }

        // 判断是否是个人
        holder.warn("企业库中找不到" + name + " 或 " + abbName + " 的企业");
        // 推测个人用户，归入散户
        if (name.length() < 4 && !name.contains("公司")) {
            company = getCompanyService().findByName("散户");
            if (company == null) {
                return null;
            }
            holder.info(name + " 个人用户归集到散户");
            return company;
        }

        // 尝试创建公司
        company = autoCreateNewCompanyAfter(developDate, name, abbName, holder);
        if (company != null) {
            holder.info("自动创建公司：" + company.getName());
        }
        return null;
    }

    private Company autoCreateNewCompanyAfter(LocalDate devDate, String name, String abbName, MessageHolder holder) {
        if (devDate == null) {
            holder.warn("开发时间未知，不创建公司");
            return null;
        }
        CompanyService companyService = getCompanyService();
        String autoCreateAfter =  getConfService().getString(CloudYuConstant.KEY_AUTO_CREATE_COMPANY_AFTER);
        // 当配置存在，且开发时间小于指定时间，不创建
        if (StringUtils.hasText(autoCreateAfter)) {
            LocalDate miniDate = LocalDate.parse(autoCreateAfter);
            if (devDate.isBefore(miniDate)) {
                // 创建时间小于指定时间，不创建
                holder.warn(name + " 的开发时间 " + devDate + " 是 " + miniDate + " 之前的, 按规定不创建, 如有需要请手动创建");
                return null;
            }
        }
        Company com = companyService.createNewCompany(name);
        com.setShortName(abbName);
        return companyService.save(com);
    }
}
