package com.ecep.contract.controller.customer;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;

import com.ecep.contract.SpringApp;
import com.ecep.contract.controller.table.cell.LocalDateTimeTableCell;
import com.ecep.contract.converter.EmployeeStringConverter;
import com.ecep.contract.converter.EntityStringConverter;
import com.ecep.contract.model.CompanyCustomer;
import com.ecep.contract.model.CompanyCustomerEntity;
import com.ecep.contract.model.CustomerCatalog;
import com.ecep.contract.service.CompanyCustomerEntityService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.CustomerEntityViewModel;

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

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

    // 关联项 tab
    public TableColumn<CustomerEntityViewModel, Number> entityTable_idColumn;
    public TableColumn<CustomerEntityViewModel, String> entityTable_catalogColumn;
    public TableColumn<CustomerEntityViewModel, String> entityTable_nameColumn;
    public TableColumn<CustomerEntityViewModel, String> entityTable_abbNameColumn;
    public TableColumn<CustomerEntityViewModel, String> entityTable_codeColumn;
    public TableColumn<CustomerEntityViewModel, String> entityTable_creatorColumn;
    public TableColumn<CustomerEntityViewModel, LocalDate> entityTable_developDateColumn;
    public TableColumn<CustomerEntityViewModel, String> 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;

    @Setter
    private CompanyCustomerEntityService customerEntityService;

    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());
        initializeEntityTabCatalogColumn(entityTable_catalogColumn);

        EmployeeStringConverter stringConverter = SpringApp.getBean(EmployeeStringConverter.class);
        entityTable_developDateColumn.setCellValueFactory(param -> param.getValue().getDevelopDate());
        entityTable_modifyDateColumn.setCellValueFactory(param -> param.getValue().getModifyDate());
        entityTable_creatorColumn
                .setCellValueFactory(param -> param.getValue().getCreator().map(stringConverter::toString));
        entityTable_modifierColumn
                .setCellValueFactory(param -> param.getValue().getModifier().map(stringConverter::toString));
        entityTable_updatedDateColumn.setCellValueFactory(param -> param.getValue().getUpdatedDate());

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

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

    private void initializeEntityTabCatalogColumn(TableColumn<CustomerEntityViewModel, String> column) {
        EntityStringConverter<CustomerCatalog> converter = new EntityStringConverter<>();
        converter.setInitialized(v -> getCompanyCustomerService().findCatalogById(v.getId()));
        column.setCellValueFactory(param -> param.getValue().getCatalog().map(converter::toString));
    }

    CompanyCustomerEntityService getCompanyCustomerEntityService() {
        if (customerEntityService == null) {
            customerEntityService = getBean(CompanyCustomerEntityService.class);
        }
        return customerEntityService;
    }

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

    @Override
    public Map<String, Object> getSpecification(CompanyCustomer parent) {
        Map<String, Object> params = getSpecification();
        params.put("customer", parent.getId());
        return params;
    }

}
