package com.ecep.contract.controller.tab;

import java.time.LocalDateTime;

import org.springframework.beans.BeansException;

import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.controller.company.AbstCompanyTableTabSkin;
import com.ecep.contract.controller.company.CompanyWindowController;
import com.ecep.contract.converter.EmployeeStringConverter;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.Invoice;
import com.ecep.contract.model.PurchaseBillVoucher;
import com.ecep.contract.service.InvoiceService;
import com.ecep.contract.service.YongYouU8Service;
import com.ecep.contract.service.PurchaseBillVoucherService;
import com.ecep.contract.vm.PurchaseBillVoucherViewModel;
import com.ecep.contract.controller.table.cell.InvoiceTableCell;
import com.ecep.contract.controller.table.cell.LocalDateTimeTableCell;

import javafx.scene.control.Button;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.KeyCode;
import lombok.Setter;

/**
 * 子合同
 */
@FxmlPath("/ui/company/company-tab-purchase-bill-voucher.fxml")
public class CompanyTabSkinPurchaseBillVoucher
        extends AbstCompanyTableTabSkin<PurchaseBillVoucher, PurchaseBillVoucherViewModel>
        implements TabSkin {

    public TableColumn<PurchaseBillVoucherViewModel, Number> contractTable_idColumn;
    public TableColumn<PurchaseBillVoucherViewModel, Number> contractTable_refIdColumn;
    public TableColumn<PurchaseBillVoucherViewModel, Invoice> invoiceColumn;
    public TableColumn<PurchaseBillVoucherViewModel, String> contractTable_codeColumn;
    public TableColumn<PurchaseBillVoucherViewModel, Employee> employeeColumn;
    public TableColumn<PurchaseBillVoucherViewModel, Employee> makerColumn;
    public TableColumn<PurchaseBillVoucherViewModel, LocalDateTime> makerDateColumn;
    public TableColumn<PurchaseBillVoucherViewModel, LocalDateTime> modifyDateColumn;
    public TableColumn<PurchaseBillVoucherViewModel, Employee> verifierColumn;
    public TableColumn<PurchaseBillVoucherViewModel, LocalDateTime> verifierDateColumn;
    public TableColumn<PurchaseBillVoucherViewModel, String> descriptionColumn;

    public TextField searchKeyField;
    public Button searchBtn;
    public MenuItem contractTable_menu_refresh;
    public MenuItem contractTable_menu_add;
    public MenuItem contractTable_menu_del;


    @Setter
    private PurchaseBillVoucherService purchaseBillVoucherService;
    @Setter
    private InvoiceService invoiceService;
    @Setter
    private YongYouU8Service u8Service;
    @Setter
    private EmployeeStringConverter employeeStringConverter;

    public CompanyTabSkinPurchaseBillVoucher(CompanyWindowController controller) {
        super(controller);
    }

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

    @Override
    public void initializeTab() {

        if (u8Service == null) {
            try {
                u8Service = getBean(YongYouU8Service.class);
            } catch (BeansException ignored) {
            }
        }

        searchKeyField.setOnKeyReleased(event -> {
            if (event.getCode() == KeyCode.ENTER) {
                searchBtn.fire();
            }
        });

        searchBtn.setOnAction(this::onTableRefreshAction);
        searchKeyField.setOnKeyReleased(event -> {
            if (event.getCode() == KeyCode.ENTER) {
                searchBtn.fire();
            }
        });

        contractTable_idColumn.setCellValueFactory(param -> param.getValue().getId());
        contractTable_refIdColumn.setCellValueFactory(param -> param.getValue().getRefId());
        contractTable_codeColumn.setCellValueFactory(param -> param.getValue().getCode());

        employeeColumn.setCellValueFactory(param -> param.getValue().getEmployee());
        employeeColumn.setCellFactory(TextFieldTableCell.forTableColumn(getEmployeeStringConverter()));

        invoiceColumn.setCellValueFactory(param -> param.getValue().getInvoice());
        invoiceColumn.setCellFactory(param -> new InvoiceTableCell<>(getInvoiceService()));


        makerColumn.setCellValueFactory(param -> param.getValue().getMaker());
        makerColumn.setCellFactory(TextFieldTableCell.forTableColumn(getEmployeeStringConverter()));
        makerDateColumn.setCellValueFactory(param -> param.getValue().getMakerDate());
        makerDateColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());
        modifyDateColumn.setCellValueFactory(param -> param.getValue().getModifyDate());
        modifyDateColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());

        verifierColumn.setCellValueFactory(param -> param.getValue().getVerifier());
        verifierColumn.setCellFactory(TextFieldTableCell.forTableColumn(getEmployeeStringConverter()));
        verifierDateColumn.setCellValueFactory(param -> param.getValue().getVerifierDate());
        verifierDateColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());

        descriptionColumn.setCellValueFactory(param -> param.getValue().getDescription());

        contractTable_menu_refresh.setOnAction(this::onTableRefreshAction);
        contractTable_menu_del.setOnAction(this::onTableDeleteAction);

        super.initializeTab();
    }

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

    @Override
    protected PurchaseBillVoucherService getViewModelService() {
        return getPurchaseBillVoucherService();
    }

    private PurchaseBillVoucherService getPurchaseBillVoucherService() {
        if (purchaseBillVoucherService == null) {
            purchaseBillVoucherService = getBean(PurchaseBillVoucherService.class);
        }
        return purchaseBillVoucherService;
    }

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

    private InvoiceService getInvoiceService() {
        if (invoiceService == null) {
            invoiceService = getBean(InvoiceService.class);
        }
        return invoiceService;
    }

}
