package com.ecep.contract.controller.bank;

import com.ecep.contract.controller.AbstEntityManagerSkin;
import com.ecep.contract.controller.ManagerSkin;
import com.ecep.contract.controller.table.EditableEntityTableTabSkin;
import com.ecep.contract.model.Bank;
import com.ecep.contract.vm.BankViewModel;

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


public class BankManagerSkin
        extends AbstEntityManagerSkin<Bank, BankViewModel, BankManagerSkin, BankManagerWindowController>
        implements ManagerSkin, EditableEntityTableTabSkin<Bank, BankViewModel> {

    public BankManagerSkin(BankManagerWindowController controller) {
        super(controller);
    }

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

        controller.codeColumn.setCellValueFactory(param -> param.getValue().getCode());
        controller.codeColumn.setCellFactory(TextFieldTableCell.forTableColumn());
        controller.codeColumn.setOnEditCommit(this::onCodeColumnEditCommit);

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

    }

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

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