package com.ecep.contract.controller.vendor;

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

import com.ecep.contract.service.VendorService;
import com.ecep.contract.task.VendorReBuildFilesTasker;
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.VendorType;
import com.ecep.contract.controller.AbstManagerWindowController;
import com.ecep.contract.controller.vendor.approved_list.VendorApprovedListManagerWindowController;
import com.ecep.contract.controller.vendor.group.VendorGroupManagerWindowController;
import com.ecep.contract.service.CompanyService;
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 com.ecep.contract.vo.VendorVo;
import com.ecep.contract.vo.VendorTypeLocalVo;

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 VendorManagerWindowController
        extends AbstManagerWindowController<VendorVo, CompanyVendorViewModel, VendorManagerSkin> {

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

    public ComboBox<VendorTypeLocalVo> typeSelector;

    @Autowired
    private CompanyService companyService;
    @Autowired
    private VendorService vendorService;

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

    @Override
    public VendorService getViewModelService() {
        return vendorService;
    }

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

            Pageable pageRequest = PageRequest.ofSize(25);
            MessageHolder messageHolder = (level, msg) -> {
                System.out.println(level + ">" + msg);
            };
            VendorReBuildFilesTasker tasker = new VendorReBuildFilesTasker();
            while (true) {
                Page<VendorVo> page = vendorService.findAll(null, pageRequest);
                for (VendorVo companyVendor : page) {
                    MessageHolder sub = messageHolder
                            .sub(page.getNumber() + "/" + page.getTotalPages() + " #" + companyVendor.getId() + ">");
                    tasker.setVendor(companyVendor);
                    tasker.callRemoteTask(sub, getLocale(), companyVendor.getId());
                }
                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(VendorApprovedListManagerWindowController.class, null);
    }

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

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

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

}
