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

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.cloud.rk.CloudRkService;
import com.ecep.contract.cloud.rk.ctx.CloudRkCtx;
import com.ecep.contract.cloud.tyc.CloudTycService;
import com.ecep.contract.cloud.u8.YongYouU8Service;
import com.ecep.contract.cloud.u8.ctx.CompanyCtx;
import com.ecep.contract.cloud.u8.ctx.ContractCtx;
import com.ecep.contract.cloud.u8.ctx.CustomerCtx;
import com.ecep.contract.cloud.u8.ctx.VendorCtx;
import com.ecep.contract.constant.CloudServiceConstant;
import com.ecep.contract.model.CloudRk;
import com.ecep.contract.model.CloudYu;
import com.ecep.contract.model.Company;
import com.ecep.contract.ui.Tasker;

import lombok.Setter;

/**
 * 合并更新
 */
public class CompanyCompositeUpdateTasker extends Tasker<Object> {
    private static final Logger logger = LoggerFactory.getLogger(CompanyCompositeUpdateTasker.class);

    CloudRkCtx cloudRkCtx = new CloudRkCtx();

    @Setter
    private CloudRkService cloudRkService;
    @Setter
    private CloudTycService cloudTycService;
    @Setter
    private YongYouU8Service yongYouU8Service;
    @Setter
    private Company company;

    @Override
    protected Object execute(MessageHolder holder) throws Exception {

        updateProgress(0.1, 1);
        syncFromCloudRk(holder);
        updateProgress(0.3, 1);
        syncFromYongYouU8(holder);
        updateProgress(0.6, 1);
        syncFromCloudTyc(holder);
        return null;
    }


    private void syncFromCloudRk(MessageHolder holder) {
        holder.debug("1. 从 " + CloudServiceConstant.RK_NAME + " 更新...");
        try {
            cloudRkService = getBean(CloudRkService.class);
        } catch (BeansException e) {
            holder.warn("未启用 " + CloudServiceConstant.RK_NAME + " 服务");
            return;
        }
        CloudRk cloudRk = cloudRkService.getOrCreateCloudRk(company);
        if (cloudRk == null) {
            holder.error("无法创建或获取 CloudRk 对象");
            return;
        }

        try {
            cloudRkCtx.setCloudRkService(cloudRkService);
            if (cloudRkCtx.syncCompany(company, cloudRk, holder)) {

            }
        } catch (Exception e) {
            cloudRk.setDescription(e.getMessage());
        } finally {
            cloudRk.setLatestUpdate(LocalDateTime.now());
            cloudRkService.save(cloudRk);
        }
    }

    private void syncFromYongYouU8(MessageHolder holder) {
        holder.debug("2. 从 " + CloudServiceConstant.U8_NAME + " 更新...");
        try {
            yongYouU8Service = getBean(YongYouU8Service.class);
        } catch (BeansException e) {
            holder.warn("未启用 " + CloudServiceConstant.U8_NAME + " 服务");
            return;
        }

        CloudYu cloudYu = yongYouU8Service.getOrCreateCloudYu(company);
        if (cloudYu == null) {
            holder.error("无法创建或获取 CloudYu 对象");
            return;
        }
        try {
            CompanyCtx companyCtx = new CompanyCtx();
            yongYouU8Service.initialize(companyCtx);
            if (companyCtx.syncCompany(company, holder)) {
                holder.info("更新");
            }

            VendorCtx vendorCtx = new VendorCtx();
            yongYouU8Service.initialize(vendorCtx);
            vendorCtx.setCompanyCtx(companyCtx);
            if (vendorCtx.syncVendor(company, holder)) {
                cloudYu.setVendorUpdateDate(LocalDate.now());
            }

            CustomerCtx customerCtx = new CustomerCtx();
            yongYouU8Service.initialize(customerCtx);
            customerCtx.setCompanyCtx(companyCtx);
            if (customerCtx.syncCustomer(company, holder)) {
                cloudYu.setCustomerUpdateDate(LocalDate.now());
            }

            ContractCtx contractCtx = new ContractCtx();
            yongYouU8Service.initialize(contractCtx);
            contractCtx.syncContract(company, holder);

            cloudYu.setCloudLatest(Instant.now());
            cloudYu.setExceptionMessage("");
        } catch (Exception e) {
            String message = e.getMessage();
            holder.error("同步过程中发生错误: " + message);
            // 保留255个字符
            if (message.length() > 255) {
                message = message.substring(0, 255);
            }
            cloudYu.setExceptionMessage(message);
        } finally {
            cloudYu.setLatestUpdate(Instant.now());
            yongYouU8Service.save(cloudYu);
        }
    }

    private void syncFromCloudTyc(MessageHolder holder) {
        holder.debug("3. 从 " + CloudServiceConstant.TYC_NAME + " 更新...");
        try {
            cloudTycService = getBean(CloudTycService.class);
        } catch (BeansException e) {
            holder.warn("未启用 " + CloudServiceConstant.TYC_NAME + " 服务");
            return;
        }
        cloudTycService.syncCompany(company, holder);
        updateProgress(1, 1);
    }
}
