package com.ecep.contract.controller.contract;

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

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.controller.AbstManagerWindowController;
import com.ecep.contract.controller.tab.ContractManagerSkin;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.Contract;
import com.ecep.contract.model.ContractGroup;
import com.ecep.contract.model.ContractKind;
import com.ecep.contract.model.ContractType;
import com.ecep.contract.model.Employee;
import com.ecep.contract.service.ContractService;
import com.ecep.contract.task.ContractFilesRebuildAllTasker;
import com.ecep.contract.task.ContractRepairAllTasker;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.ContractViewModel;

import javafx.event.ActionEvent;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.stage.Stage;

@Lazy
@Scope("prototype")
@Component
@FxmlPath("/ui/contract/contract-manager.fxml")
public class ContractManagerWindowController
        extends AbstManagerWindowController<Contract, ContractViewModel, ContractManagerSkin> {

    public ComboBox<ContractGroup> groupSelector;
    public CheckBox composeViewBtn;

    // columns
    public TableColumn<ContractViewModel, Number> idColumn;
    public TableColumn<ContractViewModel, String> nameColumn;
    public TableColumn<ContractViewModel, String> codeColumn;
    public TableColumn<ContractViewModel, ContractGroup> groupColumn;
    public TableColumn<ContractViewModel, ContractType> typeColumn;
    public TableColumn<ContractViewModel, ContractKind> kindColumn;

    public TableColumn<ContractViewModel, String> parentCodeColumn;
    public TableColumn<ContractViewModel, LocalDate> setupDateColumn;
    public TableColumn<ContractViewModel, LocalDate> orderDateColumn;
    public TableColumn<ContractViewModel, LocalDate> startDateColumn;
    public TableColumn<ContractViewModel, Employee> employeeColumn;
    public TableColumn<ContractViewModel, LocalDateTime> createdColumn;
    public TableColumn<ContractViewModel, Number> amountColumn;
    public TableColumn<ContractViewModel, Company> companyColumn;

    @Autowired
    private ContractService contractService;

    @Override
    public ContractService getViewModelService() {
        return contractService;
    }

    @Override
    protected ContractManagerSkin createDefaultSkin() {
        ContractManagerSkin skin = new ContractManagerSkin(this);
        return skin;
    }

    @Override
    public void show(Stage stage) {
        super.show(stage);
        getTitle().set("合同管理");
    }

    public void onVerifyAction(ActionEvent event) {
        show(ContractVerifyWindowController.class, null);
    }

    public void onCreateNewContractAction(ActionEvent event) {
        // getSkin().onTableCreateNewAction(event);
    }

    public void onContractFilesRebuildAction(ActionEvent event) {
        ContractFilesRebuildAllTasker task = new ContractFilesRebuildAllTasker();
        task.setContractService(contractService);
        task.setGroup(groupSelector.getValue());
        UITools.showTaskDialogAndWait("合同文件重置", task, null);
    }

    public void onContractRepairAllAction(ActionEvent event) {
        ContractRepairAllTasker task = new ContractRepairAllTasker();
        UITools.showTaskDialogAndWait("合同同步修复", task, null);
    }
}
