package com.ecep.contract.controller.project;

import java.time.LocalDate;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ecep.contract.controller.AbstManagerWindowController;
import com.ecep.contract.controller.BaseController;
import com.ecep.contract.controller.project.industry.ProjectIndustryManagerWindowController;
import com.ecep.contract.controller.project.product_type.ProductTypeManagerWindowController;
import com.ecep.contract.controller.project.project_type.ProjectTypeManagerWindowController;
import com.ecep.contract.controller.project.sale_type.ProjectSaleTypeManagerWindowController;
import com.ecep.contract.controller.project.usage.ProductUsageManagerWindowController;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.Project;
import com.ecep.contract.model.ProjectSaleType;
import com.ecep.contract.service.ProjectService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.ProjectViewModel;

import javafx.event.ActionEvent;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.stage.WindowEvent;

@Lazy
@Scope("prototype")
@Component
@FxmlPath("/ui/project/project-manager.fxml")
public class ProjectManagerWindowController
        extends AbstManagerWindowController<Project, ProjectViewModel, ProjectManagerSkin> {
    private static final Logger logger = LoggerFactory.getLogger(ProjectManagerWindowController.class);

    public ComboBox<ProjectSaleType> saleTypeSelector;

    public TableColumn<ProjectViewModel, Number> idColumn;
    public TableColumn<ProjectViewModel, String> codeColumn;
    public TableColumn<ProjectViewModel, String> nameColumn;
    public TableColumn<ProjectViewModel, String> saleTypeColumn;
    public TableColumn<ProjectViewModel, String> projectTypeColumn;
    public TableColumn<ProjectViewModel, String> productTypeColumn;
    public TableColumn<ProjectViewModel, LocalDate> createdColumn;
    public TableColumn<ProjectViewModel, Company> customerColumn;
    public TableColumn<ProjectViewModel, Boolean> useBidColumn;
    public TableColumn<ProjectViewModel, Boolean> useOfferColumn;
    public TableColumn<ProjectViewModel, Number> amountColumn;

    @Autowired
    private ProjectService projectService;

    @Override
    public void onShown(WindowEvent windowEvent) {
        super.onShown(windowEvent);
        getTitle().set("项目管理");
    }

    @Override
    public ProjectService getViewModelService() {
        return projectService;
    }

    @Override
    protected ProjectManagerSkin createDefaultSkin() {
        return new ProjectManagerSkin(this);
    }

    public void onReBuildFilesAction(ActionEvent event) {
    }

    public void onBaseTableRefreshAction(ActionEvent event) {
        getSkin().loadTableDataSet(true);
    }

    public void onCreateNewProjectAction(ActionEvent event) {
        getSkin().onTableCreateNewAction(event);
    }

    public void onOpenProjectTypeManagerAction(ActionEvent event) {
        showInOwner(ProjectTypeManagerWindowController.class);
    }

    public void onOpenSaleTypeManagerAction(ActionEvent event) {
        showInOwner(ProjectSaleTypeManagerWindowController.class);
    }

    public void onOpenUsedIndustryManagerAction(ActionEvent event) {
        showInOwner(ProjectIndustryManagerWindowController.class);
    }

    public void onOpenProductTypeManagerAction(ActionEvent event) {
        showInOwner(ProductTypeManagerWindowController.class);
    }

    public void onOpenProductUsageManagerAction(ActionEvent event) {
        showInOwner(ProductUsageManagerWindowController.class);
    }

    private <T extends BaseController> void showInOwner(Class<T> clz) {
        show(clz, table.getScene().getWindow());
    }

}
