package com.ecep.contract.controller.tab;

import java.time.LocalDateTime;

import com.ecep.contract.ContractPayWay;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.controller.contract.AbstContractTableTabSkin;
import com.ecep.contract.controller.contract.ContractWindowController;
import com.ecep.contract.controller.vendor.PurchaseOrderWindowController;
import com.ecep.contract.converter.EmployeeStringConverter;
import com.ecep.contract.service.PurchaseOrdersService;
import com.ecep.contract.vm.PurchaseOrderViewModel;
import com.ecep.contract.model.PurchaseOrder;
import com.ecep.contract.controller.table.cell.LocalDateTimeTableCell;

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

/**
 * 子合同
 */
@FxmlPath("/ui/contract/contract-tab-purchase-orders.fxml")
public class ContractTabSkinPurchaseOrders
        extends AbstContractTableTabSkin<PurchaseOrder, PurchaseOrderViewModel>
        implements TabSkin {

    private PurchaseOrdersService purchaseOrdersService;
    private EmployeeStringConverter employeeStringConverter;

    public TableColumn<PurchaseOrderViewModel, Number> idColumn;
    public TableColumn<PurchaseOrderViewModel, String> codeColumn;
    public TableColumn<PurchaseOrderViewModel, String> table_makerColumn;
    public TableColumn<PurchaseOrderViewModel, LocalDateTime> table_makerDateColumn;
    public TableColumn<PurchaseOrderViewModel, String> table_verifierColumn;
    public TableColumn<PurchaseOrderViewModel, LocalDateTime> table_verifierDateColumn;
    public TableColumn<PurchaseOrderViewModel, String> table_closerColumn;
    public TableColumn<PurchaseOrderViewModel, LocalDateTime> table_closerDateColumn;
    public TableColumn<PurchaseOrderViewModel, String> table_descriptionColumn;

    public MenuItem table_menu_refresh;

    private Tab tab;

    public ContractTabSkinPurchaseOrders(ContractWindowController controller, Tab tab) {
        super(controller);
        this.tab = tab;
    }

    @Override
    protected PurchaseOrdersService getViewModelService() {
        return getPurchaseOrdersService();
    }

    @Override
    public void initializeUIComponents() {
        super.initializeUIComponents();
        // 合同是收时，有关联子合同
        getTab().disableProperty().bind(viewModel.getPayWay().isEqualTo(ContractPayWay.PAY).not());
    }

    @Override
    public Tab getTab() {
        return tab;
    }

    @Override
    public void initializeTab() {
        table_menu_refresh.setOnAction(this::onTableRefreshAction);

        idColumn.setCellValueFactory(param -> param.getValue().getId());
        codeColumn.setCellValueFactory(param -> param.getValue().getCode());

        EmployeeStringConverter converter = getEmployeeStringConverter();
        table_makerColumn.setCellValueFactory(param -> param.getValue().getMaker().map(converter::toString));
        table_makerDateColumn.setCellValueFactory(param -> param.getValue().getMakerDate());
        table_makerDateColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());
        table_verifierColumn.setCellValueFactory(param -> param.getValue().getMaker().map(converter::toString));
        table_verifierDateColumn.setCellValueFactory(param -> param.getValue().getVerifierDate());
        table_verifierDateColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());
        table_closerColumn.setCellValueFactory(param -> param.getValue().getMaker().map(converter::toString));
        table_closerDateColumn.setCellValueFactory(param -> param.getValue().getCloserDate());
        table_closerDateColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());

        table_descriptionColumn.setCellValueFactory(param -> param.getValue().getDescription());
        super.initializeTab();
    }

    @Override
    protected void onTableRowDoubleClickedAction(PurchaseOrderViewModel item) {
        showInOwner(PurchaseOrderWindowController.class, item);
    }

    PurchaseOrdersService getPurchaseOrdersService() {
        if (purchaseOrdersService == null) {
            purchaseOrdersService = getBean(PurchaseOrdersService.class);
        }
        return purchaseOrdersService;
    }

    EmployeeStringConverter getEmployeeStringConverter() {
        if (employeeStringConverter == null) {
            employeeStringConverter = getBean(EmployeeStringConverter.class);
        }
        return employeeStringConverter;
    }
}
