package com.ecep.contract.controller.department;

import java.util.List;

import org.springframework.data.domain.Pageable;

import com.ecep.contract.controller.AbstEntityManagerSkin;
import com.ecep.contract.controller.ManagerSkin;
import com.ecep.contract.controller.table.EditableEntityTableTabSkin;
import com.ecep.contract.converter.EmployeeStringConverter;
import com.ecep.contract.model.Department;
import com.ecep.contract.model.Employee;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.DepartmentViewModel;

import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.scene.control.cell.TextFieldTableCell;

public class DepartmentManagerSkin
        extends AbstEntityManagerSkin<Department, DepartmentViewModel, DepartmentManagerSkin, DepartmentManagerWindowController>
        implements ManagerSkin, EditableEntityTableTabSkin<Department, DepartmentViewModel> {


    public DepartmentManagerSkin(DepartmentManagerWindowController controller) {
        super(controller);
    }

    @Override
    public void initializeTable() {
        getTableView().setEditable(true);

        List<Employee> employees = controller.getEmployeeService().findAll(ParamUtils.equal("isActive", true), Pageable.ofSize(30)).getContent();

        controller.idColumn.setCellValueFactory(param -> param.getValue().getId());

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

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

        controller.leaderColumn.setCellValueFactory(param -> param.getValue().getLeader());
        controller.leaderColumn.setCellFactory(ComboBoxTableCell.forTableColumn(getBean(EmployeeStringConverter.class), FXCollections.observableArrayList(employees)));
        controller.leaderColumn.setOnEditCommit(this::onLeaderColumnEditCommit);

        controller.activeColumn.setCellValueFactory(param -> param.getValue().getIsActive());
        controller.activeColumn.setEditable(true);
        controller.activeColumn.setCellFactory(CheckBoxTableCell.forTableColumn(controller.activeColumn));
        controller.activeColumn.setOnEditCommit(this::onActiveColumnEditCommit);
    }


    private void onCodeColumnEditCommit(TableColumn.CellEditEvent<DepartmentViewModel, String> event) {
        DepartmentViewModel row = event.getRowValue();
        row.getCode().set(event.getNewValue());
        saveRowData(row);
    }

    private void onNameColumnEditCommit(TableColumn.CellEditEvent<DepartmentViewModel, String> event) {
        DepartmentViewModel row = event.getRowValue();
        row.getName().set(event.getNewValue());
        saveRowData(row);
    }

    private void onLeaderColumnEditCommit(TableColumn.CellEditEvent<DepartmentViewModel, Employee> event) {
        DepartmentViewModel row = event.getRowValue();
        row.getLeader().set(event.getNewValue());
        saveRowData(row);
    }

    private void onActiveColumnEditCommit(TableColumn.CellEditEvent<DepartmentViewModel, Boolean> event) {
        DepartmentViewModel row = event.getRowValue();
        row.getIsActive().set(event.getNewValue());
        saveRowData(row);
    }

    @Override
    protected void onTableRowDoubleClickedAction(DepartmentViewModel item) {
        //TODO 显示详情
    }

    @Override
    protected void onTableCreateNewAction(ActionEvent event) {
        Department employee = new Department();
        employee = controller.getViewModelService().save(employee);
        DepartmentViewModel viewModel = DepartmentViewModel.from(employee);
        dataSet.add(viewModel);
    }
}
