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

import com.ecep.contract.controller.AbstEntityManagerSkin;
import com.ecep.contract.controller.ManagerSkin;
import com.ecep.contract.controller.table.EditableEntityTableTabSkin;
import com.ecep.contract.model.ProjectType;
import com.ecep.contract.service.ProjectService;
import com.ecep.contract.service.ProjectTypeService;
import com.ecep.contract.vm.ProjectTypeViewModel;

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

public class ProjectTypeManagerSkin
        extends AbstEntityManagerSkin<ProjectType, ProjectTypeViewModel, ProjectTypeManagerSkin, ProjectTypeManagerWindowController>
        implements ManagerSkin , EditableEntityTableTabSkin<ProjectType, ProjectTypeViewModel> {

    @Setter
    private ProjectService projectService;
    @Setter
    private ProjectTypeService projectTypeService;

    public ProjectTypeManagerSkin(ProjectTypeManagerWindowController 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<ProjectTypeViewModel, String> event) {
        acceptCellEditEvent(event, ProjectTypeViewModel::getDescription);
    }

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

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


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

    public ProjectTypeService getProjectTypeService() {
        if (projectTypeService == null) {
            projectTypeService = getBean(ProjectTypeService.class);
        }
        return projectTypeService;
    }
}
