package com.ecep.contract.controller.employee;

import java.time.format.DateTimeFormatter;

import com.ecep.contract.controller.tab.TabSkin;
import com.ecep.contract.converter.EntityStringConverter;
import com.ecep.contract.model.Department;
import com.ecep.contract.service.DepartmentService;
import com.ecep.contract.util.UITools;

import javafx.scene.control.Tab;
import javafx.util.converter.LocalDateStringConverter;

public class EmployeeTabSkinBase
        extends AbstEmployeeBasedTabSkin
        implements TabSkin {
    public EmployeeTabSkinBase(EmployeeWindowController controller) {
        super(controller);
    }

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

    @Override
    public void initializeTab() {
        EntityStringConverter<Department> departmentEntityStringConverter = new EntityStringConverter<>();
        DepartmentService departmentService = getBean(DepartmentService.class);
        departmentEntityStringConverter.setInitialized(department -> departmentService.findById(department.getId()));
        UITools.autoCompletion(controller.departmentField, viewModel.getDepartment(),
                p -> departmentService.search(p.getUserText()), departmentEntityStringConverter);

        controller.nameField.textProperty().bindBidirectional(viewModel.getName());
        controller.aliasField.textProperty().bindBidirectional(viewModel.getAlias());
        controller.codeField.textProperty().bind(viewModel.getCode());
        controller.accountField.textProperty().bindBidirectional(viewModel.getAccount());
        controller.emailField.textProperty().bindBidirectional(viewModel.getEmail());
        controller.phoneField.textProperty().bindBidirectional(viewModel.getPhone());

        LocalDateStringConverter converter = new LocalDateStringConverter(DateTimeFormatter.ISO_LOCAL_DATE, null);
        controller.leaveDateField.setConverter(converter);
        controller.leaveDateField.valueProperty().bindBidirectional(viewModel.getLeaveDate());
        controller.entryDateField.setConverter(converter);
        controller.entryDateField.valueProperty().bindBidirectional(viewModel.getEntryDate());

        controller.createdField.setConverter(converter);
        controller.createdField.valueProperty().bindBidirectional(viewModel.getCreated());

        controller.isActiveField.selectedProperty().bindBidirectional(viewModel.getIsActive());


//        Callback<ListView<EmployeeRole>, ListCell<EmployeeRole>> cellFactory = controller.rolesField.getCellFactory();
//        StringConverter<EmployeeRole> employeeRoleStringConverter = new EntityStringConverter<>();
//        controller.rolesField.setCellFactory(param -> {
//            ListCell<EmployeeRole> cell = cellFactory.call(param);
//            if (cell instanceof CheckBoxListCell<EmployeeRole> list) {
//                list.setConverter(employeeRoleStringConverter);
//            }
//            return cell;
//        });


//        controller.rolesField.getCheckModel().getCheckedItems().setAll();
//        Property<IndexedCheckModel<EmployeeRole>> selectedRoles = new SimpleObjectProperty<>();
//        controller.rolesField.getCheckModel().getCheckedItems().addListener((ListChangeListener<? super EmployeeRole>) changed -> {
//            System.out.println("newValue = " + changed);
//        });
    }

}
