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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;

import com.ecep.contract.controller.AbstEntityManagerSkin;
import com.ecep.contract.controller.ManagerSkin;
import com.ecep.contract.controller.table.EditableEntityTableTabSkin;
import com.ecep.contract.model.VendorGroup;
import com.ecep.contract.service.VendorGroupService;
import com.ecep.contract.vm.VendorGroupViewModel;

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

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

    @Setter
    private VendorGroupService vendorGroupService;

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

    private VendorGroupService getVendorGroupService() {
        if (vendorGroupService == null) {
            vendorGroupService = getBean(VendorGroupService.class);
        }
        return vendorGroupService;
    }

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

    @Override
    protected List<VendorGroupViewModel> loadTableData() {
        Map<String, Object> params = new HashMap<>();
        String searchText = controller.searchKeyField.getText();
        if (StringUtils.hasText(searchText)) {
            params.put("searchText", searchText);
        }
        Page<VendorGroup> page = getVendorGroupService().findAll(params, getPageable());
        updateFooter(page);
        return page.map(VendorGroupViewModel::from).toList();
    }

    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 VendorGroup loadRowData(VendorGroupViewModel row) {
        VendorGroup group = super.loadRowData(row);
        if (group == null) {
            group = getVendorGroupService().newInstance();
        }
        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);
    }

}
