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

import com.ecep.contract.SpringApp;
import com.ecep.contract.controller.tab.TabSkin;
import com.ecep.contract.model.DeliverySignMethod;
import com.ecep.contract.service.DeliverySignMethodService;
import com.ecep.contract.vm.DeliverySignMethodViewModel;

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

public class SignMethodTabSkin
        extends AbstProjectSaleTypeTableTabSkin<DeliverySignMethod, DeliverySignMethodViewModel>
        implements TabSkin {

    @Setter
    private DeliverySignMethodService deliverySignMethodService;


    public SignMethodTabSkin(ProjectSaleTypeWindowController controller) {
        super(controller);
    }

    @Override
    public TableView<DeliverySignMethodViewModel> getTableView() {
        return controller.signMethodTable;
    }

    @Override
    protected DeliverySignMethodService getViewModelService() {
        return getDeliverySignMethodService();
    }

    @Override
    public Tab getTab() {
        return controller.signMethodTab;
    }

    @Override
    protected DeliverySignMethodViewModel createNewViewModel() {
        DeliverySignMethodViewModel model = super.createNewViewModel();
        model.getSaleType().set(getEntity());
        return model;
    }

    @Override
    public void initializeTab() {
        getTableView().setEditable(true);
        controller.signMethodTable_idColumn.setCellValueFactory(param -> param.getValue().getId());
        controller.signMethodTable_idColumn.setEditable(false);
        controller.signMethodTable_codeColumn.setCellValueFactory(param -> param.getValue().getCode());
        controller.signMethodTable_codeColumn.setCellFactory(TextFieldTableCell.forTableColumn());
        controller.signMethodTable_codeColumn.setOnEditCommit(this::onSignMethodTableCodeColumnEditCommitAction);

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

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

        controller.signMethodTable_createdColumn.setCellValueFactory(param -> param.getValue().getCreated());
    }

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

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

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

    public DeliverySignMethodService getDeliverySignMethodService() {
        if (deliverySignMethodService == null) {
            deliverySignMethodService = SpringApp.getBean(DeliverySignMethodService.class);
        }
        return deliverySignMethodService;
    }
}
