package com.ecep.contract.controller.customer;

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

import com.ecep.contract.controller.table.cell.CustomerCatalogTableCell;
import com.ecep.contract.controller.table.cell.EmployeeTableCell;
import com.ecep.contract.controller.table.cell.LocalDateTimeTableCell;
import com.ecep.contract.service.CompanyCustomerEntityService;
import com.ecep.contract.service.CustomerCatalogService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.CustomerEntityViewModel;
import com.ecep.contract.vo.CompanyCustomerEntityVo;
import com.ecep.contract.vo.CustomerVo;

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

@FxmlPath("/ui/company/customer/customer-tab-entity.fxml")
public class CustomerTabSkinEntity
        extends AbstCompanyCustomerTableTabSkin<CompanyCustomerEntityVo, CustomerEntityViewModel> {

    // 关联项 tab
    public TableColumn<CustomerEntityViewModel, Number> entityTable_idColumn;
    public TableColumn<CustomerEntityViewModel, Integer> entityTable_catalogColumn;
    public TableColumn<CustomerEntityViewModel, String> entityTable_nameColumn;
    public TableColumn<CustomerEntityViewModel, String> entityTable_abbNameColumn;
    public TableColumn<CustomerEntityViewModel, String> entityTable_codeColumn;
    public TableColumn<CustomerEntityViewModel, Integer> entityTable_creatorColumn;
    public TableColumn<CustomerEntityViewModel, LocalDate> entityTable_developDateColumn;
    public TableColumn<CustomerEntityViewModel, Integer> entityTable_modifierColumn;
    public TableColumn<CustomerEntityViewModel, LocalDate> entityTable_modifyDateColumn;
    public TableColumn<CustomerEntityViewModel, LocalDate> entityTable_updatedDateColumn;
    public TableColumn<CustomerEntityViewModel, LocalDateTime> fetchedTimeColumn;

    public MenuItem entityTable_menu_refresh;
    public MenuItem entityTable_menu_del;

    public CustomerTabSkinEntity(CompanyCustomerWindowController controller) {
        super(controller);
    }

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

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

        entityTable_idColumn.setCellValueFactory(param -> param.getValue().getId());
        entityTable_nameColumn.setCellValueFactory(param -> param.getValue().getName());
        entityTable_abbNameColumn.setCellValueFactory(param -> param.getValue().getAbbName());
        entityTable_codeColumn.setCellValueFactory(param -> param.getValue().getCode());

        entityTable_catalogColumn.setCellValueFactory(param -> param.getValue().getCatalog());
        entityTable_catalogColumn.setCellFactory(CustomerCatalogTableCell.forTableColumn(getCustomerCatalogService()));

        entityTable_developDateColumn.setCellValueFactory(param -> param.getValue().getDevelopDate());
        entityTable_modifyDateColumn.setCellValueFactory(param -> param.getValue().getModifyDate());
        entityTable_updatedDateColumn.setCellValueFactory(param -> param.getValue().getUpdatedDate());

        fetchedTimeColumn.setCellValueFactory(param -> param.getValue().getFetchedTime());
        fetchedTimeColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());

        entityTable_creatorColumn.setCellValueFactory(param -> param.getValue().getCreator());
        entityTable_creatorColumn.setCellFactory(EmployeeTableCell.forTableColumn(getEmployeeService()));
        entityTable_modifierColumn.setCellValueFactory(param -> param.getValue().getModifier());
        entityTable_modifierColumn.setCellFactory(EmployeeTableCell.forTableColumn(getEmployeeService()));

        entityTable_menu_refresh.setOnAction(this::onTableRefreshAction);
        entityTable_menu_del.setOnAction(this::onTableDeleteAction);
    }

    CompanyCustomerEntityService getCompanyCustomerEntityService() {
        return getCachedBean(CompanyCustomerEntityService.class);
    }

    @Override
    protected CompanyCustomerEntityService getViewModelService() {
        return getCompanyCustomerEntityService();
    }

    CustomerCatalogService getCustomerCatalogService() {
        return getCachedBean(CustomerCatalogService.class);
    }

    @Override
    public ParamUtils.Builder getSpecification(CustomerVo parent) {
        ParamUtils.Builder params = getSpecification();
        params.equals("customer", parent.getId());
        return params;
    }

}
