package com.ecep.contract.controller.employee;

import java.time.LocalDateTime;

import com.ecep.contract.controller.tab.TabSkin;
import com.ecep.contract.controller.table.cell.LocalDateTimeTableCell;
import com.ecep.contract.model.EmployeeLoginHistory;
import com.ecep.contract.service.EmployeeLoginHistoryService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.EmployeeLoginHistoryViewModel;

import javafx.application.Platform;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;

@FxmlPath("/ui/employee/employee-login-history.fxml")
public class EmployeeTabSkinLoginHistory
        extends AbstEmployeeTableTabSkin<EmployeeLoginHistory, EmployeeLoginHistoryViewModel>
        implements TabSkin {
    public TableColumn<EmployeeLoginHistoryViewModel, Number> idColumn;
    public TableColumn<EmployeeLoginHistoryViewModel, String> ipColumn;
    public TableColumn<EmployeeLoginHistoryViewModel, String> macColumn;
    public TableColumn<EmployeeLoginHistoryViewModel, LocalDateTime> loginTimeColumn;
    public TableColumn<EmployeeLoginHistoryViewModel, LocalDateTime> activeTimeColumn;

    private EmployeeLoginHistoryService loginHistoryService;

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

    EmployeeLoginHistoryService getLoginHistoryService() {
        if (loginHistoryService == null) {
            loginHistoryService = getBean(EmployeeLoginHistoryService.class);
        }
        return loginHistoryService;
    }

    @Override
    protected EmployeeLoginHistoryService getViewModelService() {
        return getLoginHistoryService();
    }

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

    @Override
    public void initializeTab() {
        super.initializeTab();

        idColumn.setCellValueFactory(param -> param.getValue().getId());
        ipColumn.setCellValueFactory(param -> param.getValue().getIp());
        macColumn.setCellValueFactory(param -> param.getValue().getMac());
        loginTimeColumn.setCellValueFactory(param -> param.getValue().getLoginTime());
        loginTimeColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());
        activeTimeColumn.setCellValueFactory(param -> param.getValue().getActiveTime());
        activeTimeColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());

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