package com.ecep.contract.controller.permission;

import com.ecep.contract.controller.AbstEntityManagerSkin;
import com.ecep.contract.model.Function;
import com.ecep.contract.service.FunctionService;
import com.ecep.contract.service.PermissionService;
import com.ecep.contract.vm.FunctionViewModel;

import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.TextFieldTableCell;

public class FunctionManagerSkin
        extends
        AbstEntityManagerSkin<Function, FunctionViewModel, FunctionManagerSkin, EmployeeFunctionsManagerWindowController> {

    public FunctionManagerSkin(EmployeeFunctionsManagerWindowController controller) {
        super(controller);
    }

    @Override
    public void initializeTable() {
        controller.idColumn.setCellValueFactory(param -> param.getValue().getId());

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

        controller.keyColumn.setCellValueFactory(param -> param.getValue().getKey());
        controller.keyColumn.setCellFactory(TextFieldTableCell.forTableColumn());
        controller.keyColumn.setOnEditCommit(this::onKeyColumnEditCommit);

        controller.controllerColumn.setCellValueFactory(param -> param.getValue().getController());
        controller.controllerColumn.setCellFactory(TextFieldTableCell.forTableColumn());
        controller.controllerColumn.setOnEditCommit(this::onControllerColumnEditCommit);

        controller.iconColumn.setCellValueFactory(param -> param.getValue().getIcon());
        controller.iconColumn.setCellFactory(TextFieldTableCell.forTableColumn());
        controller.iconColumn.setOnEditCommit(this::onIconColumnEditCommit);

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

        controller.activeColumn.setCellValueFactory(param -> param.getValue().getActive());
        controller.activeColumn.setCellFactory(CheckBoxTableCell.forTableColumn(controller.activeColumn));
    }

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

    private void onKeyColumnEditCommit(TableColumn.CellEditEvent<FunctionViewModel, String> event) {
        acceptCellEditEvent(event, FunctionViewModel::getKey);
    }

    private void onControllerColumnEditCommit(TableColumn.CellEditEvent<FunctionViewModel, String> event) {
        acceptCellEditEvent(event, FunctionViewModel::getController);
    }

    private void onIconColumnEditCommit(TableColumn.CellEditEvent<FunctionViewModel, String> event) {
        acceptCellEditEvent(event, FunctionViewModel::getIcon);
    }

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

    @Override
    protected void onTableRowDoubleClickedAction(FunctionViewModel item) {
        showInOwner(FunctionWindowController.class, item);
    }

    protected PermissionService getPermissionService() {
        return controller.getPermissionService();
    }

    protected FunctionService getFunctionService() {
        return controller.getViewModelService();
    }

}
