package com.ecep.contract.controller.employee;

import java.util.Map;

import com.ecep.contract.controller.AbstEntityManagerSkin;
import com.ecep.contract.controller.ManagerSkin;
import com.ecep.contract.controller.table.cell.DepartmentTableCell;
import com.ecep.contract.model.Employee;
import com.ecep.contract.service.DepartmentService;
import com.ecep.contract.vm.EmployeeViewModel;

import javafx.event.ActionEvent;
import javafx.scene.control.cell.CheckBoxTableCell;

public class EmployeeManagerSkin
        extends AbstEntityManagerSkin<Employee, EmployeeViewModel, EmployeeManagerSkin, EmployeeManagerWindowController>
        implements ManagerSkin {
    public EmployeeManagerSkin(EmployeeManagerWindowController controller) {
        super(controller);
    }

    DepartmentService departmentService;

    DepartmentService getDepartmentService() {
        if (departmentService == null) {
            departmentService = getBean(DepartmentService.class);
        }
        return departmentService;
    }

    @Override
    public Map<String, Object> getSpecification() {
        Map<String, Object> params = super.getSpecification();
        if (controller.activeCheckBox.isSelected()) {
            params.put("isActive", true);
        }
        return params;
    }

    @Override
    public void initializeTable() {
        controller.idColumn.setCellValueFactory(param -> param.getValue().getId());
        controller.nameColumn.setCellValueFactory(param -> param.getValue().getName());
        controller.aliasColumn.setCellValueFactory(param -> param.getValue().getAlias());
        controller.codeColumn.setCellValueFactory(param -> param.getValue().getCode());
        controller.accountColumn.setCellValueFactory(param -> param.getValue().getAccount());

        controller.departmentColumn.setCellValueFactory(param -> param.getValue().getDepartment());
        controller.departmentColumn.setCellFactory(param -> new DepartmentTableCell<>(getDepartmentService()));

        controller.emailColumn.setCellValueFactory(param -> param.getValue().getEmail());
        controller.createdColumn.setCellValueFactory(param -> param.getValue().getCreated());
        controller.entryDateColumn.setCellValueFactory(param -> param.getValue().getEntryDate());
        controller.leaveDateColumn.setCellValueFactory(param -> param.getValue().getLeaveDate());
        controller.activeColumn.setCellValueFactory(param -> param.getValue().getIsActive());
        controller.activeColumn.setCellFactory(CheckBoxTableCell.forTableColumn(controller.activeColumn));

    }

    @Override
    protected void onTableRowDoubleClickedAction(EmployeeViewModel item) {
        show(item);
    }

    @Override
    protected void onTableCreateNewAction(ActionEvent event) {
        Employee employee = new Employee();
        employee = controller.getViewModelService().save(employee);
        EmployeeViewModel viewModel = EmployeeViewModel.from(employee);
        dataSet.add(viewModel);
        show(viewModel);

    }

    public void show(EmployeeViewModel viewModel) {
        showInOwner(EmployeeWindowController.class, viewModel);
    }
}
