package com.ecep.contract.controller.customer;

import com.ecep.contract.MyDateTimeUtils;
import com.ecep.contract.controller.AbstEntityManagerSkin;
import com.ecep.contract.controller.table.cell.CompanyTableCell;
import com.ecep.contract.model.CompanyCustomer;
import com.ecep.contract.service.CompanyCustomerService;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.vm.CompanyCustomerViewModel;

import javafx.application.Platform;

public class CompanyCustomerManagerSkin
        extends
        AbstEntityManagerSkin<CompanyCustomer, CompanyCustomerViewModel, CompanyCustomerManagerSkin, CompanyCustomerManagerWindowController> {

    public CompanyCustomerManagerSkin(CompanyCustomerManagerWindowController controller) {
        super(controller);
    }

    public CompanyService getCompanyService() {
        return getBean(CompanyService.class);
    }

    public CompanyCustomerService getCompanyCustomerService() {
        return controller.getViewModelService();
    }

    @Override
    public void initializeTable() {
        controller.idColumn.setCellValueFactory(param -> param.getValue().getId());
        controller.companyColumn.setCellValueFactory(param -> param.getValue().getCompany());
        controller.companyColumn.setCellFactory(param -> new CompanyTableCell<>(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(CompanyCustomerViewModel item) {
        showInOwner(item);
    }

    private void showInOwner(CompanyCustomerViewModel model) {
        showInOwner(CompanyCustomerWindowController.class, model);
    }
}
