package com.ecep.contract.service.tasker;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.ds.customer.service.CompanyCustomerFileService;
import com.ecep.contract.ds.customer.service.CustomerService;
import com.ecep.contract.ds.company.model.Company;
import com.ecep.contract.ds.customer.model.CompanyCustomer;
import com.ecep.contract.ds.customer.model.CompanyCustomerFile;
import com.ecep.contract.ui.Tasker;
import com.fasterxml.jackson.databind.JsonNode;

import org.hibernate.Hibernate;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.logging.Level;

/**
 * 客户文件移动到公司目录任务器
 * 用于将客户相关文件从客户目录移动到公司目录
 */
public class CustomerFileMoveTasker extends Tasker<Object> implements WebSocketServerTasker {
    private CompanyCustomerFile file;

    @Override
    public void init(JsonNode argsNode) {
        if (argsNode != null && argsNode.size() > 0) {
            this.file = getCompanyCustomerFileService().findById(argsNode.get(0).asInt());
        }
    }

    @Override
    protected Object execute(MessageHolder holder) throws Exception {
        try {
            updateTitle("移动文件到公司目录");
            holder.addMessage(Level.INFO, "开始执行文件移动任务...");

            if (file == null) {
                throw new IllegalArgumentException("文件信息不能为空");
            }

            // 获取客户信息
            CompanyCustomer customer = file.getCustomer();
            if (customer == null) {
                throw new IllegalArgumentException("客户不存在");
            }
            if (!Hibernate.isInitialized(customer)) {
                customer = getCompanyCustomerService().getById(customer.getId());
            }

            // 获取公司信息
            Company company = customer.getCompany();
            if (company == null) {
                throw new IllegalArgumentException("公司不存在: " + customer.getCompany());
            }
            if (!Hibernate.isInitialized(company)) {
                company = getCompanyService().getById(company.getId());
            }

            if (!StringUtils.hasText(company.getPath())) {
                throw new IllegalArgumentException("公司目录未设置");
            }

            File companyPath = new File(company.getPath());
            if (!companyPath.exists()) {
                throw new IllegalArgumentException("公司目录设置异常，无法访问: " + company.getPath());
            }

            // 移动文件
            boolean moveFile = moveFile(file.getFilePath(), companyPath, holder);;
            moveFile(file.getEditFilePath(), companyPath, holder);

            // 删除数据库记录
            if (moveFile) {
                getCompanyCustomerFileService().delete(file);
                holder.addMessage(Level.INFO, "文件移动任务执行完成");
                updateProperty("filesUpdated", true);
            }

            return true;
        } catch (Exception e) {
            holder.addMessage(Level.SEVERE, "文件移动失败: " + e.getMessage());
            throw e;
        }
    }

    private boolean moveFile(String filePath, File companyPath, MessageHolder holder) {
        if (StringUtils.hasText(filePath)) {
            File file = new File(filePath);
            if (file.exists()) {
                File dest = new File(companyPath, file.getName());
                if (file.renameTo(dest)) {
                    holder.info(file.getAbsolutePath() + " -> " + dest.getAbsolutePath());
                    return true;
                } else {
                    holder.error("无法移动文件: " + file.getAbsolutePath());
                }
            }
        }
        return false;
    }

    private CompanyCustomerFileService getCompanyCustomerFileService() {
        return getCachedBean(CompanyCustomerFileService.class);
    }

    private CustomerService getCompanyCustomerService() {
        return getCachedBean(CustomerService.class);
    }
}