package com.ecep.contract.controller.vendor;

import com.ecep.contract.MyDateTimeUtils;
import com.ecep.contract.controller.AbstEntityManagerSkin;
import com.ecep.contract.controller.ComboBoxUtils;
import com.ecep.contract.controller.table.cell.CompanyTableCell;
import com.ecep.contract.controller.table.cell.VendorCatalogTableCell;
import com.ecep.contract.controller.table.cell.VendorTypeTableCell;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.service.CompanyVendorService;
import com.ecep.contract.service.VendorCatalogService;
import com.ecep.contract.service.VendorTypeService;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.CompanyVendorViewModel;
import com.ecep.contract.vo.CompanyVendorVo;
import javafx.application.Platform;
import lombok.Setter;

public class CompanyVendorManagerSkin
        extends
        AbstEntityManagerSkin<CompanyVendorVo, CompanyVendorViewModel, CompanyVendorManagerSkin, CompanyVendorManagerWindowController> {

    @Setter
    private CompanyService companyService;
    public CompanyVendorManagerSkin(CompanyVendorManagerWindowController controller) {
        super(controller);
    }

    public CompanyService getCompanyService() {
        if (companyService == null) {
            companyService = getBean(CompanyService.class);
        }
        return companyService;
    }

    public CompanyVendorService getCompanyVendorService() {
        return controller.getViewModelService();
    }

    @Override
    public ParamUtils.Builder getSpecification() {
        ParamUtils.Builder params = super.getSpecification();
        if (controller.typeSelector.getValue() != null) {
            params.equals("type", controller.typeSelector.getValue().getType().name());
        }
        return params;
    }

    @Override
    public void initializeTable() {
        ComboBoxUtils.initialComboBox(controller.typeSelector, null, getBean(VendorTypeService.class), true);

        controller.idColumn.setCellValueFactory(param -> param.getValue().getId());
        // id column
        controller.codeColumn.setCellValueFactory(param -> param.getValue().getCode());
        // catalog column
        controller.catalogColumn.setCellValueFactory(param -> param.getValue().getCatalog());
        controller.catalogColumn.setCellFactory(VendorCatalogTableCell.forTableColumn(getBean(VendorCatalogService.class)));
        // type column
        controller.typeColumn.setCellValueFactory(param -> param.getValue().getType());
        controller.typeColumn.setCellFactory(VendorTypeTableCell.forTableColumn(getBean(VendorTypeService.class)));

        controller.purchaseColumn.setCellValueFactory(param -> param.getValue().getPurchase());
        // company column
        controller.companyColumn.setCellValueFactory(param -> param.getValue().getCompany());
        controller.companyColumn.setCellFactory(CompanyTableCell.forTableColumn(getCompanyService()));

        controller.developDateColumn.setCellValueFactory(param -> param.getValue().getDevelopDate());
        controller.pathColumn.setCellValueFactory(param -> param.getValue().getPath());
        controller.createdColumn
                .setCellValueFactory(param -> param.getValue().getCreated().map(MyDateTimeUtils::format));

        Platform.runLater(() -> {
            getTableView().getSortOrder().add(controller.idColumn);
        });
    }

    @Override
    protected void onTableRowDoubleClickedAction(CompanyVendorViewModel item) {
        showInOwner(item);
    }

    private void showInOwner(CompanyVendorViewModel model) {
        showInOwner(CompanyVendorWindowController.class, model);
    }
}