package com.ecep.contract.controller;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ecep.contract.constant.CompanyCustomerConstant;
import com.ecep.contract.constant.CompanyVendorConstant;
import com.ecep.contract.constant.ContractConstant;
import com.ecep.contract.util.StringConfig;

import jakarta.annotation.PreDestroy;
import javafx.event.ActionEvent;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;

@Lazy
@Scope("prototype")
@Component
public class SysConfWindowController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SysConfWindowController.class);

    public Label companyContractPathLabel;

    public TextField u8DataBaseServerHostField;
    public TextField u8DataBaseCatalogField;
    public TextField u8DataBaseUserNameField;
    public TextField u8DataBasePasswordField;

    public Label vendorPathLabel;
    public Label vendorEvaluationFormTemplateLabel;

    public Label customerPathLabel;
    public Label customerEvaluationFormTemplateLabel;
    public Label customerSaleBookPathLabel;

    StringConfig contractPathConfig = new StringConfig(ContractConstant.KEY_BASE_PATH);
    StringConfig vendorPathConfig = new StringConfig(CompanyVendorConstant.KEY_BASE_PATH);
    StringConfig vendorEvaluationFormTemplateConfig = new StringConfig(
            CompanyVendorConstant.KEY_EVALUATION_FORM_TEMPLATE);
    StringConfig customerPathConfig = new StringConfig(CompanyCustomerConstant.KEY_BASE_PATH);
    StringConfig customerEvaluationFormTemplateConfig = new StringConfig(
            CompanyCustomerConstant.KEY_EVALUATION_FORM_TEMPLATE);
    StringConfig customerSaleBookPathConfig = new StringConfig(CompanyCustomerConstant.KEY_SALEBOOK_PATH);

    public void initialize() {
        contractPathConfig.setControl(companyContractPathLabel);
        contractPathConfig.initialize();

        vendorPathConfig.setControl(vendorPathLabel);
        vendorPathConfig.initialize();
        vendorEvaluationFormTemplateConfig.setControl(vendorEvaluationFormTemplateLabel);
        vendorEvaluationFormTemplateConfig.initialize();

        customerPathConfig.setControl(customerPathLabel);
        customerPathConfig.initialize();
        customerEvaluationFormTemplateConfig.setControl(customerEvaluationFormTemplateLabel);
        customerEvaluationFormTemplateConfig.initialize();
        customerSaleBookPathConfig.setControl(customerSaleBookPathLabel);
        customerSaleBookPathConfig.initialize();

        logger.debug("#initialize()");
    }

    private void directoryChoose(StringConfig config, String title, String key, ActionEvent event) {
        DirectoryChooser chooser = new DirectoryChooser();
        chooser.setTitle(title);
        File value = new File(config.getProperty().getValue());
        chooser.setInitialDirectory(value);

        Node node = (Node) event.getSource();
        File selected = chooser.showDialog(node.getScene().getWindow());
        if (selected != null) {
            config.getProperty().setValue(selected.getAbsolutePath());
            config.save();
        }
    }

    public void changeCompanyContractPath(ActionEvent actionEvent) {
        directoryChoose(contractPathConfig, "请选择合同目录", ContractConstant.KEY_BASE_PATH, actionEvent);
    }

    public void changeVendorPath(ActionEvent actionEvent) {
        directoryChoose(vendorPathConfig, "请选择供应商目录", CompanyVendorConstant.KEY_BASE_PATH, actionEvent);
    }

    public void changeCustomerPath(ActionEvent actionEvent) {
        directoryChoose(customerPathConfig, "请选择客户目录", CompanyCustomerConstant.KEY_BASE_PATH, actionEvent);
    }

    public void changeCustomerSaleBookPath(ActionEvent actionEvent) {
        directoryChoose(customerSaleBookPathConfig, "请选择销售台账目录", CompanyCustomerConstant.KEY_SALEBOOK_PATH, actionEvent);
    }

    // 模拟销毁方法
    @PreDestroy
    public void destroy() {

    }

    private void fileChoose(StringConfig config, String title, String key, ActionEvent event) {
        FileChooser chooser = new FileChooser();
        chooser.setTitle(title);
        File value = new File(config.getProperty().getValue());
        chooser.setInitialDirectory(value.getParentFile());
        chooser.setInitialFileName(value.getName());
        chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(" 模板文件(*.xlsx)", "*.xlsx"));
        Node node = (Node) event.getSource();
        File selected = chooser.showOpenDialog(node.getScene().getWindow());
        if (selected != null) {
            config.getProperty().setValue(selected.getAbsolutePath());
            config.save();
        }
    }

    public void changeCustomerEvaluationFormTemplate(ActionEvent actionEvent) {
        fileChoose(customerEvaluationFormTemplateConfig, "请选择客户资信评估表模板",
                CompanyCustomerConstant.KEY_EVALUATION_FORM_TEMPLATE, actionEvent);
    }

    public void changeVendorEvaluationFormTemplate(ActionEvent actionEvent) {
        fileChoose(vendorEvaluationFormTemplateConfig, "请选择供方调查评价表模板",
                CompanyVendorConstant.KEY_EVALUATION_FORM_TEMPLATE,
                actionEvent);
    }
}
