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.bid.ProjectBidWindowController;
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.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.ProjectBid;
import com.ecep.contract.service.CompanyCustomerFileService;
import com.ecep.contract.service.CompanyCustomerService;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.service.ProjectBidService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.ProjectBidViewModel;

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

@FxmlPath("/ui/project/project-tab-bid.fxml")
public class ProjectTabSkinBid
        extends AbstProjectTableTabSkin<ProjectBid, ProjectBidViewModel>
        implements TabSkin, EditableEntityTableTabSkin<ProjectBid, ProjectBidViewModel>, RefreshableSkin {

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

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

    @Setter
    private ProjectBidService projectBidService;
    @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 ProjectTabSkinBid(ProjectWindowController controller) {
        super(controller);
    }

    @Override
    protected ProjectBidService getViewModelService() {
        return getProjectBidService();
    }

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

    @Override
    public void initializeTable() {
        super.initializeTable();
        bindNumberColumn(idColumn, ProjectBidViewModel::getId);

//        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());


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

        applicantColumn.setCellValueFactory(param -> param.getValue().getApplicant());
        applicantColumn.setCellFactory(cell -> new EmployeeTableCell<>(getEmployeeService()));
        bindLocalDateTimeColumn(applyTimeColumn, ProjectBidViewModel::getApplyTime);
        authorizerColumn.setCellValueFactory(param -> param.getValue().getAuthorizer());
        authorizerColumn.setCellFactory(cell -> new EmployeeTableCell<>(getEmployeeService()));
        bindLocalDateTimeColumn(authorizationTimeColumn, ProjectBidViewModel::getAuthorizationTime);

        bindColumn(descriptionColumn, ProjectBidViewModel::getDescription);
    }

    @Override
    protected ProjectBidViewModel createNewViewModel() {
        ProjectBidViewModel model = new ProjectBidViewModel();
        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(ProjectBidViewModel item) {
        showInOwner(ProjectBidWindowController.class, item);
    }

    private ProjectBidService getProjectBidService() {
        if (projectBidService == null) {
            projectBidService = getBean(ProjectBidService.class);
        }
        return projectBidService;
    }

    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;
    }

    @Override
    public void deleteRowData(ProjectBid entity) {
        getProjectBidService().delete(entity);
    }

    @Override
    public ProjectBid loadRowData(ProjectBidViewModel row) {
        return getProjectBidService().findById(row.getId().get());
    }

    @Override
    public ProjectBid saveRowData(ProjectBid entity) {
        return getProjectBidService().save(entity);
    }


    private static class LevelTableCell extends TableCell<ProjectBidViewModel, 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));
        }
    }
}
