package com.ecep.contract.controller.employee;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.data.domain.Sort;

import com.ecep.contract.controller.table.cell.EmployeeTableCell;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.EmployeeAuthBind;
import com.ecep.contract.service.EmployeeAuthBindService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.EmployeeAuthBindViewModel;

import javafx.application.Platform;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;
import lombok.Setter;

@FxmlPath("/ui/employee/employee-auth-bind.fxml")
public class EmployeeTabSkinAuthBind
        extends AbstEmployeeTableTabSkin<EmployeeAuthBind, EmployeeAuthBindViewModel> {
    public TableColumn<EmployeeAuthBindViewModel, Number> idColumn;
    public TableColumn<EmployeeAuthBindViewModel, String> ipColumn;
    public TableColumn<EmployeeAuthBindViewModel, String> macColumn;
    public TableColumn<EmployeeAuthBindViewModel, LocalDateTime> createTime;
    public TableColumn<EmployeeAuthBindViewModel, LocalDateTime> updateTimeColumn;
    public TableColumn<EmployeeAuthBindViewModel, Employee> updaterColumn;
    public TableColumn<EmployeeAuthBindViewModel, String> descriptionColumn;

    @Setter
    private EmployeeAuthBindService employeeAuthBindService;

    public EmployeeTabSkinAuthBind(EmployeeWindowController controller) {
        super(controller);
    }

    EmployeeAuthBindService getEmployeeAuthBindService() {
        if (employeeAuthBindService == null) {
            employeeAuthBindService = getBean(EmployeeAuthBindService.class);
        }
        return employeeAuthBindService;
    }

    @Override
    protected EmployeeAuthBindService getViewModelService() {
        return getEmployeeAuthBindService();
    }

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

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

        bindNumberColumn(idColumn, EmployeeAuthBindViewModel::getId);

        bindColumn(ipColumn, EmployeeAuthBindViewModel::getIp);
        bindColumn(macColumn, EmployeeAuthBindViewModel::getMac);

        bindLocalDateTimeColumn(createTime, EmployeeAuthBindViewModel::getCreateTime);
        bindLocalDateTimeColumn(updateTimeColumn, EmployeeAuthBindViewModel::getUpdateTime);

        updaterColumn.setCellValueFactory(param -> param.getValue().getUpdater());
        updaterColumn.setCellFactory(cell -> new EmployeeTableCell<>());

        bindColumn(descriptionColumn, EmployeeAuthBindViewModel::getDescription);

        Platform.runLater(() -> {
            getTableView().getSortOrder().add(updateTimeColumn);
        });
    }

    @Override
    protected void createContextMenu(ContextMenu contextMenu) {
        super.createContextMenu(contextMenu);
        MenuItem menuItem = new MenuItem("导入未关联");
        menuItem.setOnAction(event -> {
            EmployeeAuthBindService service = getEmployeeAuthBindService();
            List<EmployeeAuthBind> authBinds = service.findAllByEmployee(null, Sort.unsorted());
            for (EmployeeAuthBind authBind : authBinds) {
                authBind.setEmployee(getEntity());
                authBind.setUpdateTime(LocalDateTime.now());
                authBind.setUpdater(controller.getCurrentUser());
                service.save(authBind);
            }
        });
        contextMenu.getItems().add(menuItem);
    }
}
