package com.ecep.contract.controller.project;

import java.time.format.DateTimeFormatter;

import com.ecep.contract.util.ProxyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ecep.contract.SpringApp;
import com.ecep.contract.controller.AbstEntityController;
import com.ecep.contract.model.DeliverySignMethod;
import com.ecep.contract.model.ProductType;
import com.ecep.contract.model.ProductUsage;
import com.ecep.contract.model.Project;
import com.ecep.contract.model.ProjectIndustry;
import com.ecep.contract.model.ProjectSaleType;
import com.ecep.contract.model.ProjectType;
import com.ecep.contract.service.ContractService;
import com.ecep.contract.service.EmployeeService;
import com.ecep.contract.service.ProjectService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.ProjectViewModel;

import javafx.beans.binding.Bindings;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Window;
import javafx.stage.WindowEvent;
import javafx.util.converter.LocalDateStringConverter;

@Lazy
@Scope("prototype")
@Component
@FxmlPath("/ui/project/project.fxml")
public class ProjectWindowController extends AbstEntityController<Project, ProjectViewModel> {
    private static final Logger logger = LoggerFactory.getLogger(ProjectWindowController.class);



    public static void show(Project project, Window window) {
        ProjectViewModel viewModel = new ProjectViewModel();
        if (!ProxyUtils.isInitialized(project)) {
            project = SpringApp.getBean(ProjectService.class).findById(project.getId());
        }
        viewModel.update(project);
        show(viewModel, window);
    }

    public static void show(ProjectViewModel model, Window window) {
        show(ProjectWindowController.class, model, window);
    }

    public BorderPane root;
    public TabPane tabPane;

    public Button refreshBtn;
    public Button deleteBtn;


    LocalDateStringConverter localDateStringConverter = new LocalDateStringConverter(DateTimeFormatter.ISO_LOCAL_DATE, null);

    // 基本信息 Tab
    public Tab baseInfoTab;
    public TextField nameField;
    public TextField codeField;
    public TextField codeYearField;
    public TextField codeSequenceNumberField;
    public TextField addressField;
    public TextArea descriptionField;
    public CheckBox useBidField;
    public CheckBox useOfferField;
    public TextField amountField;
    public CheckBox standardPayWayField;
    public ComboBox<ProjectIndustry> industryField;
    public ComboBox<ProjectSaleType> saleTypeField;
    public ComboBox<ProjectType> projectTypeField;
    public ComboBox<ProductType> productTypeField;
    public ComboBox<DeliverySignMethod> deliverySignMethodField;
    public ComboBox<ProductUsage> productUsageField;
    public TextField applicantField;
    public TextField authorizerField;
    public DatePicker plannedStartTimeField;
    public DatePicker plannedCompletionTimeField;
    public DatePicker createdField;
    public Label versionLabel;

    /*
     合同 Tab
     */
    public Tab contractTab;
    /*         成本审批           */
    public Tab costTab;


    // 文件 Tab
    public Tab fileTab;
    public TableView fileTable;
    public TableColumn fileTable_idColumn;
    public TableColumn fileTable_typeColumn;
    public TableColumn fileTable_filePathColumn;
    public TableColumn fileTable_applyDateColumn;
    public TableColumn fileTable_descriptionColumn;

    // 客户信息 Tab
    public Tab companyTab;

    // 报价审批 Tab
    public Tab quotationApprovalTab;

    /**
     * 投标 Bid Tab
     */
    public Tab bidTab;
    /**
     * 资金计划 Tab
     */
    public Tab fundPlanTab;
    /**
     * 顾客满意度 Tab
     */
    public Tab satisfactionTab;

    @Autowired
    ProjectService projectService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    ContractService contractService;

    public ProjectWindowController() {
    }

    public void onShown(WindowEvent windowEvent) {
        super.onShown(windowEvent);
        refreshBtn.setOnAction(event -> refreshByButton((Button) event.getSource()));
        getTitle().bind(Bindings.createStringBinding(() -> "[" + viewModel.getId().get() + "] " + viewModel.getCode().get() + " " + viewModel.getName().getValue() + " 项目详情", viewModel.getCode(), viewModel.getName()));
    }

    @Override
    protected void registerTabSkins() {
        registerTabSkin(baseInfoTab, this::createBaseTabSkin);
        registerTabSkin(companyTab, tab -> new ProjectTabSkinCustomerInfo(this));
        registerTabSkin(contractTab, tab -> new ProjectTabSkinContract(this));
        registerTabSkin(costTab, tab -> new ProjectTabSkinCost(this));
        registerTabSkin(quotationApprovalTab, tab -> new ProjectTabSkinQuotation(this));
        registerTabSkin(bidTab, tab -> new ProjectTabSkinBid(this));
        registerTabSkin(fundPlanTab, tab -> new ProjectTabSkinFundPlan(this));
//        registerTabSkin(costItemTab, this::createCostItemTabSkin);
        registerTabSkin(satisfactionTab, tab -> new ProjectTabSkinCustomerSatisfactionSurvey(this));
    }

    @Override
    public ProjectService getViewModelService() {
        return projectService;
    }


    private ProjectTabSkinBase createBaseTabSkin(Tab tab) {
        ProjectTabSkinBase skin = new ProjectTabSkinBase(this);
        skin.setLocalDateStringConverter(localDateStringConverter);
        skin.setEmployeeService(employeeService);
        return skin;
    }

    public void onFileReBuildingAction(ActionEvent event) {
    }

    public void onFileTableContextMenuShowing(WindowEvent windowEvent) {
    }

    public void onFileTableRefreshAction(ActionEvent event) {
    }

    public void onFileTableDeleteAction(ActionEvent event) {
    }

    public void onGetNextCodeSNAction(ActionEvent event) {
        ProjectSaleType projectSaleType = viewModel.getSaleType().get();
        int year = viewModel.getCodeYear().get();
        int sn = viewModel.getCodeSequenceNumber().get();

        int nextSequenceNumber = projectService.getNextCodeSequenceNumber(projectSaleType, year);
        viewModel.getCodeSequenceNumber().set(nextSequenceNumber);
    }
}
