package com.ecep.contract.controller.project;

import java.time.LocalDate;

import com.ecep.contract.controller.contract.ContractWindowController;
import com.ecep.contract.controller.tab.TabSkin;
import com.ecep.contract.model.Contract;
import com.ecep.contract.service.ContractService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.ContractViewModel;

import javafx.application.Platform;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;

@FxmlPath("/ui/project/project-tab-contract.fxml")
public class ProjectTabSkinContract
        extends AbstProjectTableTabSkin<Contract, ContractViewModel>
        implements TabSkin {
    public TableColumn<ContractViewModel, Number> idColumn;
    public TableColumn<ContractViewModel, String> codeColumn;
    public TableColumn<ContractViewModel, String> nameColumn;
    public TableColumn<ContractViewModel, LocalDate> setupDateColumn;
    public TableColumn<ContractViewModel, LocalDate> inureDateColumn;
    public TableColumn<ContractViewModel, LocalDate> orderDateColumn;
    public TableColumn<ContractViewModel, LocalDate> varyDateColumn;
    public MenuItem contractTable_menu_refresh;

    public ProjectTabSkinContract(ProjectWindowController controller) {
        super(controller);
    }

    @Override
    protected ContractService getViewModelService() {
        return getContractService();
    }

    @Override
    public Tab getTab() {
        return controller.contractTab;
    }

    @Override
    public void initializeTable() {
        super.initializeTable();

        bindNumberColumn(idColumn, ContractViewModel::getId);
        codeColumn.setCellValueFactory(param -> param.getValue().getCode());
        nameColumn.setCellValueFactory(param -> param.getValue().getName());

        bindLocalDateColumn(setupDateColumn, ContractViewModel::getSetupDate);
        bindLocalDateColumn(inureDateColumn, ContractViewModel::getInureDate);
        bindLocalDateColumn(orderDateColumn, ContractViewModel::getOrderDate);
        bindLocalDateColumn(varyDateColumn, ContractViewModel::getVaryDate);

        Platform.runLater(() -> {
            getTableView().getSortOrder().add(setupDateColumn);
        });

        contractTable_menu_refresh.setOnAction(this::onTableRefreshAction);
    }

    @Override
    protected void onTableRowDoubleClickedAction(ContractViewModel item) {
        ContractWindowController.show(item, controller.root.getScene().getWindow());
    }

    private ContractService getContractService() {
        return controller.contractService;
    }
}
