package com.ecep.contract.controller.vendor;

import java.time.LocalDate;

import com.ecep.contract.controller.tab.TabSkin;
import com.ecep.contract.controller.table.cell.EmployeeTableCell;
import com.ecep.contract.converter.EntityStringConverter;
import com.ecep.contract.model.CompanyVendorEntity;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.VendorCatalog;
import com.ecep.contract.service.CompanyVendorEntityService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.CompanyVendorEntityViewModel;
import com.ecep.contract.vm.VendorEntityViewModel;

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

@FxmlPath("/ui/company/vendor/vendor-tab-entity.fxml")
public class CompanyVendorTabSkinEntity
        extends AbstCompanyVendorTableTabSkin<CompanyVendorEntity, CompanyVendorEntityViewModel>
        implements TabSkin {

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

    public MenuItem entityTable_menu_refresh;
    public MenuItem entityTable_menu_del;

    public CompanyVendorTabSkinEntity(CompanyVendorWindowController controller) {
        super(controller);
    }

    CompanyVendorEntityService getVendorEntityService() {
        return getCachedBean(CompanyVendorEntityService.class);
    }

    @Override
    protected CompanyVendorEntityService getViewModelService() {
        return getVendorEntityService();
    }

    @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);

        entityTable_developDateColumn.setCellValueFactory(param -> param.getValue().getDevelopDate());
        entityTable_modifyDateColumn.setCellValueFactory(param -> param.getValue().getModifyDate());
        entityTable_creatorColumn.setCellValueFactory(param -> param.getValue().getCreator());
        entityTable_creatorColumn.setCellFactory(param -> new EmployeeTableCell<>(getEmployeeService()));
        entityTable_modifierColumn.setCellValueFactory(param -> param.getValue().getModifier());
        entityTable_modifierColumn.setCellFactory(param -> new EmployeeTableCell<>(getEmployeeService()));
        entityTable_updatedDateColumn.setCellValueFactory(param -> param.getValue().getUpdatedDate());

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

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