package com.ecep.contract.controller.vendor.group;

import com.ecep.contract.controller.AbstEntityManagerSkin;
import com.ecep.contract.controller.ManagerSkin;
import com.ecep.contract.controller.table.EditableEntityTableTabSkin;
import com.ecep.contract.service.VendorGroupService;
import com.ecep.contract.vm.VendorGroupViewModel;
import com.ecep.contract.vo.VendorGroupVo;

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

public class VendorGroupManagerSkin
        extends
        AbstEntityManagerSkin<VendorGroupVo, VendorGroupViewModel, VendorGroupManagerSkin, VendorGroupManagerWindowController>
        implements ManagerSkin, EditableEntityTableTabSkin<VendorGroupVo, VendorGroupViewModel> {

    public VendorGroupManagerSkin(VendorGroupManagerWindowController controller) {
        super(controller);
    }

    private VendorGroupService getVendorGroupService() {
        return controller.getViewModelService();
    }

    @Override
    public void initializeTable() {
        initializeSaleTypeTable();
    }

    private void initializeSaleTypeTable() {
        // table.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);
    }

    @Override
    public VendorGroupVo loadRowData(VendorGroupViewModel row) {
        VendorGroupVo group = super.loadRowData(row);
        if (group == null) {
            group = getVendorGroupService().createNewEntity();
        }
        return group;
    }

    @Override
    protected void onTableRowDoubleClickedAction(VendorGroupViewModel item) {
        VendorGroupWindowController.show(item, controller.table.getScene().getWindow());
    }

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

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

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

}
