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

import com.ecep.contract.controller.AbstEntityManagerSkin;
import com.ecep.contract.controller.ManagerSkin;
import com.ecep.contract.controller.table.EditableEntityTableTabSkin;
import com.ecep.contract.model.ProjectIndustry;
import com.ecep.contract.service.ProjectService;
import com.ecep.contract.vm.ProjectIndustryViewModel;

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

public class ProjectIndustryManagerSkin
        extends AbstEntityManagerSkin<ProjectIndustry, ProjectIndustryViewModel, ProjectIndustryManagerSkin, ProjectIndustryManagerWindowController>
        implements ManagerSkin, EditableEntityTableTabSkin<ProjectIndustry, ProjectIndustryViewModel> {
    @Setter
    private ProjectService projectService;

    public ProjectIndustryManagerSkin(ProjectIndustryManagerWindowController 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<ProjectIndustryViewModel, String> event) {
        acceptCellEditEvent(event, ProjectIndustryViewModel::getDescription);
    }

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

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

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