package com.ecep.contract.controller.project.usage;

import com.ecep.contract.controller.AbstEntityManagerSkin;
import com.ecep.contract.controller.ManagerSkin;
import com.ecep.contract.controller.table.EditableEntityTableTabSkin;
import com.ecep.contract.model.ProductUsage;
import com.ecep.contract.service.ProductUsageService;
import com.ecep.contract.service.ProjectService;
import com.ecep.contract.vm.ProductUsageViewModel;

import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.TextFieldTableCell;
import lombok.Setter;

public class ProductUsageManagerSkin
        extends AbstEntityManagerSkin<ProductUsage, ProductUsageViewModel, ProductUsageManagerSkin, ProductUsageManagerWindowController>
        implements ManagerSkin , EditableEntityTableTabSkin<ProductUsage, ProductUsageViewModel> {

    @Setter
    private ProjectService projectService;
    @Setter
    private ProductUsageService productUsageService;

    public ProductUsageManagerSkin(ProductUsageManagerWindowController controller) {
        super(controller);
    }

    @Override
    public void initializeTable() {
        getTableView().setEditable(true);
        controller.idColumn.setCellValueFactory(param -> param.getValue().getId());
        controller.idColumn.setEditable(false);
        controller.codeColumn.setCellValueFactory(param -> param.getValue().getCode());
        controller.codeColumn.setCellFactory(TextFieldTableCell.forTableColumn());
        controller.codeColumn.setOnEditCommit(this::onCodeColumnEditCommitAction);

        controller.nameColumn.setCellValueFactory(param -> param.getValue().getName());
        controller.nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());
        controller.nameColumn.setOnEditCommit(this::onNameColumnEditCommitAction);

        controller.descriptionColumn.setCellValueFactory(param -> param.getValue().getDescription());
        controller.descriptionColumn.setCellFactory(TextFieldTableCell.forTableColumn());
        controller.descriptionColumn.setOnEditCommit(this::onDescriptionColumnEditCommitAction);

        controller.createdColumn.setCellValueFactory(param -> param.getValue().getCreated());
    }


    private void onDescriptionColumnEditCommitAction(TableColumn.CellEditEvent<ProductUsageViewModel, String> event) {
        acceptCellEditEvent(event, ProductUsageViewModel::getDescription);
    }


    private void onNameColumnEditCommitAction(TableColumn.CellEditEvent<ProductUsageViewModel, String> event) {
        acceptCellEditEvent(event, ProductUsageViewModel::getName);
    }

    private void onCodeColumnEditCommitAction(TableColumn.CellEditEvent<ProductUsageViewModel, String> event) {
        acceptCellEditEvent(event, ProductUsageViewModel::getCode);
    }

    public ProjectService getProjectService() {
        if (projectService == null) {
            projectService = getBean(ProjectService.class);
        }
        return projectService;
    }

    public ProductUsageService getProductUsageService() {
        if (productUsageService == null) {
            productUsageService = getBean(ProductUsageService.class);
        }
        return productUsageService;
    }
}
