package com.ecep.contract.controller.project.product_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.ProductType;
import com.ecep.contract.vm.ProductTypeViewModel;

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

public class ProductTypeManagerSkin
        extends AbstEntityManagerSkin<ProductType, ProductTypeViewModel, ProductTypeManagerSkin, ProductTypeManagerWindowController>
        implements ManagerSkin, EditableEntityTableTabSkin<ProductType, ProductTypeViewModel> {

    public ProductTypeManagerSkin(ProductTypeManagerWindowController 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<ProductTypeViewModel, String> event) {
        acceptCellEditEvent(event, ProductTypeViewModel::getDescription);
    }


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

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