package com.ecep.contract.controller.project;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

import com.ecep.contract.controller.project.quotation.ProjectQuotationWindowController;
import com.ecep.contract.controller.tab.RefreshableSkin;
import com.ecep.contract.controller.tab.TabSkin;
import com.ecep.contract.controller.table.EditableEntityTableTabSkin;
import com.ecep.contract.controller.table.cell.EmployeeTableCell;
import com.ecep.contract.controller.table.cell.EvaluationFileTableCell;
import com.ecep.contract.controller.table.cell.LocalDateTimeTableCell;
import com.ecep.contract.converter.CompanyStringConverter;
import com.ecep.contract.converter.EmployeeStringConverter;
import com.ecep.contract.model.CompanyCustomerEvaluationFormFile;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.Project;
import com.ecep.contract.model.ProjectQuotation;
import com.ecep.contract.service.CompanyCustomerFileService;
import com.ecep.contract.service.CompanyCustomerService;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.service.ProjectQuotationService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.ProjectQuotationViewModel;

import javafx.scene.control.Tab;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.converter.CurrencyStringConverter;
import javafx.util.converter.LocalDateStringConverter;
import lombok.Setter;

@FxmlPath("/ui/project/project-tab-quotation.fxml")
public class ProjectTabSkinQuotation
        extends AbstProjectTableTabSkin<ProjectQuotation, ProjectQuotationViewModel>
        implements TabSkin, EditableEntityTableTabSkin<ProjectQuotation, ProjectQuotationViewModel>, RefreshableSkin {

    public TableColumn<ProjectQuotationViewModel, Number> idColumn;
    public TableColumn<ProjectQuotationViewModel, Number> versionColumn;
    public TableColumn<ProjectQuotationViewModel, CompanyCustomerEvaluationFormFile> evaluationFileColumn;

    public TableColumn<ProjectQuotationViewModel, String> descriptionColumn;
    public TableColumn<ProjectQuotationViewModel, Employee> applicantColumn;
    public TableColumn<ProjectQuotationViewModel, LocalDateTime> applyTimeColumn;
    public TableColumn<ProjectQuotationViewModel, Employee> authorizerColumn;
    public TableColumn<ProjectQuotationViewModel, LocalDateTime> authorizationTimeColumn;
    public TableColumn<ProjectQuotationViewModel, Number> levelColumn;
    public TableColumn<ProjectQuotationViewModel, String> standardPayWayColumn;
    public TableColumn<ProjectQuotationViewModel, Number> amountColumn;

    @Setter
    private ProjectQuotationService projectQuotationService;
    @Setter
    private LocalDateStringConverter localDateStringConverter;
    @Setter
    private EmployeeStringConverter employeeStringConverter;
    @Setter
    private CompanyStringConverter companyStringConverter;
    @Setter
    private CompanyService companyService;
    @Setter
    private CompanyCustomerService customerService;
    @Setter
    private CompanyCustomerFileService customerFileService;


    public ProjectTabSkinQuotation(ProjectWindowController controller) {
        super(controller);
    }

    @Override
    protected ProjectQuotationService getViewModelService() {
        return getProjectQuotationService();
    }

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

    @Override
    public void initializeTable() {
        super.initializeTable();
        levelColumn.setCellValueFactory(param -> param.getValue().getLevel());
        levelColumn.setCellFactory(param -> new LevelTableCell());
        standardPayWayColumn.setCellValueFactory(param -> param.getValue().getStandardPayWay().map(value -> value == null ? "" : (value ? "标准" : "非标准")));
        evaluationFileColumn.setCellValueFactory(param -> param.getValue().getEvaluationFile());
        evaluationFileColumn.setCellFactory(param -> new EvaluationFileTableCell<>(getCompanyCustomerFileService()));


        amountColumn.setCellValueFactory(param -> param.getValue().getAmount());
        amountColumn.setCellFactory(TextFieldTableCell.forTableColumn(new CurrencyStringConverter(getLocale())));

        applicantColumn.setCellValueFactory(param -> param.getValue().getApplicant());
        applicantColumn.setCellFactory(column1 -> new EmployeeTableCell<>());
        applyTimeColumn.setCellValueFactory(param -> param.getValue().getApplyTime());
        applyTimeColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());
        authorizerColumn.setCellValueFactory(param -> param.getValue().getAuthorizer());
        authorizerColumn.setCellFactory(column -> new EmployeeTableCell<>());
        authorizationTimeColumn.setCellValueFactory(param -> param.getValue().getAuthorizationTime());
        authorizationTimeColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());


        descriptionColumn.setCellValueFactory(param -> param.getValue().getDescription());

    }

    @Override
    protected ProjectQuotationViewModel createNewViewModel() {
        ProjectQuotationViewModel model = new ProjectQuotationViewModel();
        Project project = getParent();
        model.getProject().set(project);
        model.getApplicant().set(project.getApplicant());
        if (project.getAmount() != null) {
            model.getAmount().set(project.getAmount());
        }

        LocalDate created = project.getCreated();
        if (created == null) {
            created = LocalDate.now();
        }
        model.getApplyTime().set(LocalDateTime.of(created, LocalTime.now()));
        saveRow(model);
        return model;
    }

    @Override
    protected void onTableRowDoubleClickedAction(ProjectQuotationViewModel item) {
        showInOwner(ProjectQuotationWindowController.class, item);
    }

    private ProjectQuotationService getProjectQuotationService() {
        if (projectQuotationService == null) {
            projectQuotationService = getBean(ProjectQuotationService.class);
        }
        return projectQuotationService;
    }

    private LocalDateStringConverter getLocalDateStringConverter() {
        if (localDateStringConverter == null) {
            localDateStringConverter = new LocalDateStringConverter(DateTimeFormatter.ISO_LOCAL_DATE, null);
        }
        return localDateStringConverter;
    }

    private EmployeeStringConverter getEmployeeStringConverter() {
        if (employeeStringConverter == null) {
            employeeStringConverter = getBean(EmployeeStringConverter.class);
        }
        return employeeStringConverter;
    }

    public CompanyService getCompanyService() {
        if (companyService == null) {
            companyService = getBean(CompanyService.class);
        }
        return companyService;
    }

    private CompanyStringConverter getCompanyStringConverter() {
        if (companyStringConverter == null) {
            companyStringConverter = getBean(CompanyStringConverter.class);
        }
        return companyStringConverter;
    }

    private CompanyCustomerService getCompanyCustomerService() {
        if (customerService == null) {
            customerService = getBean(CompanyCustomerService.class);
        }
        return customerService;
    }

    private CompanyCustomerFileService getCompanyCustomerFileService() {
        if (customerFileService == null) {
            customerFileService = getBean(CompanyCustomerFileService.class);
        }
        return customerFileService;
    }

    private static class LevelTableCell extends TableCell<ProjectQuotationViewModel, Number> {
        @Override
        protected void updateItem(Number item, boolean empty) {
            super.updateItem(item, empty);
            if (empty || item == null) {
                setText("");
                return;
            }
            // 较差,一般,良好
            int value = item.intValue();
            if (value < 0) {
                setText("-");
                return;
            }
            if (value < 1) {
                setText("较差");
                return;
            }
            if (value < 2) {
                setText("一般");
                return;
            }
            if (value < 3) {
                setText("良好");
                return;
            }
            setText(String.valueOf(value));
        }
    }
}
