package com.ecep.contract.controller.customer;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;

import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.controller.project.satisfaction_survey.CustomerSatisfactionSurveyWindowController;
import com.ecep.contract.controller.table.cell.EmployeeTableCell;
import com.ecep.contract.controller.table.cell.ProjectTableCell;
import com.ecep.contract.converter.EntityStringConverter;
import com.ecep.contract.model.CompanyCustomer;
import com.ecep.contract.model.CustomerCatalog;
import com.ecep.contract.model.CustomerSatisfactionSurvey;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.Project;
import com.ecep.contract.service.CustomerSatisfactionSurveyService;
import com.ecep.contract.service.ProjectService;
import com.ecep.contract.vm.CustomerEntityViewModel;
import com.ecep.contract.vm.CustomerSatisfactionSurveyViewModel;

import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;
import lombok.Setter;

@FxmlPath("/ui/company/customer/customer-tab-satisfaction-survey.fxml")
public class CustomerTabSkinSatisfactionSurvey
        extends AbstCompanyCustomerTableTabSkin<CustomerSatisfactionSurvey, CustomerSatisfactionSurveyViewModel> {

    // 关联项 tab
    public TableColumn<CustomerSatisfactionSurveyViewModel, Number> idColumn;
    public TableColumn<CustomerSatisfactionSurveyViewModel, Project> projectColumn;
    public TableColumn<CustomerSatisfactionSurveyViewModel, String> codeColumn;
    public TableColumn<CustomerSatisfactionSurveyViewModel, Number> totalScoreColumn;
    public TableColumn<CustomerSatisfactionSurveyViewModel, Employee> applicantColumn;
    public TableColumn<CustomerSatisfactionSurveyViewModel, LocalDateTime> applyTimeColumn;
    public TableColumn<CustomerSatisfactionSurveyViewModel, String> descriptionColumn;
    public TableColumn<CustomerSatisfactionSurveyViewModel, LocalDate> dateColumn;

    public MenuItem entityTable_menu_refresh;
    public MenuItem entityTable_menu_del;
    @Setter
    private ProjectService projectService;
    @Setter
    private CustomerSatisfactionSurveyService satisfactionSurveyService;

    public CustomerTabSkinSatisfactionSurvey(CompanyCustomerWindowController controller) {
        super(controller);
    }

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

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

        bindNumberColumn(idColumn, CustomerSatisfactionSurveyViewModel::getId);
        bindColumn(codeColumn, CustomerSatisfactionSurveyViewModel::getCode);
        projectColumn.setCellValueFactory(param -> param.getValue().getProject());
        projectColumn.setCellFactory(cell -> new ProjectTableCell<>(getProjectService()));
        bindLocalDateColumn(dateColumn, CustomerSatisfactionSurveyViewModel::getDate);
        bindNumberColumn(totalScoreColumn, CustomerSatisfactionSurveyViewModel::getTotalScore);
        applicantColumn.setCellValueFactory(param -> param.getValue().getApplicant());
        applicantColumn.setCellFactory(cell -> new EmployeeTableCell<>(getEmployeeService()));
        bindLocalDateTimeColumn(applyTimeColumn, CustomerSatisfactionSurveyViewModel::getApplyTime);
        bindColumn(descriptionColumn, CustomerSatisfactionSurveyViewModel::getDescription);

        entityTable_menu_refresh.setOnAction(this::onTableRefreshAction);
        entityTable_menu_del.setOnAction(this::onTableDeleteAction);
    }

    private void initializeEntityTabCatalogColumn(TableColumn<CustomerEntityViewModel, String> column) {
        EntityStringConverter<CustomerCatalog> converter = new EntityStringConverter<>();
        converter.setInitialized(v -> getCompanyCustomerService().findCatalogById(v.getId()));
        column.setCellValueFactory(param -> param.getValue().getCatalog().map(converter::toString));
    }

    private CustomerSatisfactionSurveyService getCustomerSatisfactionSurveyService() {
        if (satisfactionSurveyService == null) {
            satisfactionSurveyService = getBean(CustomerSatisfactionSurveyService.class);
        }
        return satisfactionSurveyService;
    }

    ProjectService getProjectService() {
        if (projectService == null) {
            projectService = getBean(ProjectService.class);
        }
        return projectService;
    }

    @Override
    protected CustomerSatisfactionSurveyService getViewModelService() {
        return getCustomerSatisfactionSurveyService();
    }

    @Override
    public Map<String, Object> getSpecification(CompanyCustomer parent) {
        Map<String, Object> params = getSpecification();
        params.put("project.customer", parent.getId());
        return params;
    }

    @Override
    protected void onTableRowDoubleClickedAction(CustomerSatisfactionSurveyViewModel item) {
        showInOwner(CustomerSatisfactionSurveyWindowController.class, item);
    }
}
