package com.ecep.contract.controller.vendor;

import java.time.LocalDate;
import java.util.concurrent.CompletableFuture;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.controller.AbstManagerWindowController;
import com.ecep.contract.controller.vendor.approved_list.CompanyVendorApprovedListManagerWindowController;
import com.ecep.contract.controller.vendor.group.VendorGroupManagerWindowController;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyVendor;
import com.ecep.contract.model.VendorTypeLocal;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.service.CompanyVendorService;
import com.ecep.contract.task.VendorVerifyAllTasker;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.CompanyVendorViewModel;

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

@Lazy
@Scope("prototype")
@Component
@FxmlPath("/ui/company/vendor/vendor_manager.fxml")
public class CompanyVendorManagerWindowController
        extends AbstManagerWindowController<CompanyVendor, CompanyVendorViewModel, CompanyVendorManagerSkin> {

    // columns
    public TableColumn<CompanyVendorViewModel, Number> idColumn;
    public TableColumn<CompanyVendorViewModel, Company> companyColumn;
    public TableColumn<CompanyVendorViewModel, String> codeColumn;
    public TableColumn<CompanyVendorViewModel, String> catalogColumn;
    public TableColumn<CompanyVendorViewModel, String> typeColumn;
    public TableColumn<CompanyVendorViewModel, String> purchaseColumn;
    public TableColumn<CompanyVendorViewModel, LocalDate> developDateColumn;
    public TableColumn<CompanyVendorViewModel, String> pathColumn;
    public TableColumn<CompanyVendorViewModel, String> createdColumn;

    public ComboBox<VendorTypeLocal> typeSelector;

    @Autowired
    private CompanyService companyService;
    @Autowired
    private CompanyVendorService companyVendorService;

    @Override
    public void show(Stage stage) {
        super.show(stage);
        getTitle().set("供应商管理");
    }

    @Override
    public CompanyVendorService getViewModelService() {
        return companyVendorService;
    }

    public void onFileReBuildingAction(ActionEvent event) {
        CompletableFuture.runAsync(() -> {

            Pageable pageRequest = PageRequest.ofSize(25);
            MessageHolder messageHolder = (level, msg) -> {
                System.out.println(level + ">" + msg);
            };

            while (true) {
                Page<CompanyVendor> page = companyVendorService.findAll(null, pageRequest);
                for (CompanyVendor companyVendor : page) {
                    MessageHolder sub = messageHolder
                            .sub(page.getNumber() + "/" + page.getTotalPages() + " #" + companyVendor.getId() + ">");
                    companyVendorService.reBuildingFiles(companyVendor, sub);
                }
                if (!page.hasNext()) {
                    break;
                }
                pageRequest = page.nextPageable();
            }
        });

    }

    public void onVerifyAction(ActionEvent event) {
        VendorVerifyAllTasker task = new VendorVerifyAllTasker();
        UITools.showTaskDialogAndWait("核验所有供应商", task, null);
    }

    public void onCreateNewVendorAction(ActionEvent event) {
        UITools.showAlertAndWait("请从公司管理中，创建供应商");
    }

    public void onOpenApprovedVendorListWindowAction(ActionEvent event) {
        show(CompanyVendorApprovedListManagerWindowController.class, null);
    }

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

    public void onOpenVendorGroupManagerAction(ActionEvent event) {
        show(VendorGroupManagerWindowController.class, null);
    }

    @Override
    protected CompanyVendorManagerSkin createDefaultSkin() {
        CompanyVendorManagerSkin skin = new CompanyVendorManagerSkin(this);
        skin.setCompanyService(companyService);
        skin.setCompanyVendorService(companyVendorService);
        return skin;
    }

}
