package com.ecep.contract.controller.vendor.approved_list;

import java.io.File;

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 org.springframework.util.StringUtils;

import com.ecep.contract.DesktopUtils;
import com.ecep.contract.controller.AbstEntityController;
import com.ecep.contract.model.CompanyVendorApprovedList;
import com.ecep.contract.service.CompanyVendorApprovedListService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.CompanyVendorApprovedListViewModel;

import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Window;
import javafx.stage.WindowEvent;

@Lazy
@Scope("prototype")
@Component
@FxmlPath("/ui/company/vendor/vendor_approved_list.fxml")
public class CompanyVendorApprovedListWindowController
        extends AbstEntityController<CompanyVendorApprovedList, CompanyVendorApprovedListViewModel> {
    private static final Logger logger = LoggerFactory.getLogger(CompanyVendorApprovedListWindowController.class);


    public static void show(CompanyVendorApprovedList list, Window window) {
        CompanyVendorApprovedListViewModel model = CompanyVendorApprovedListViewModel.from(list);
        show(CompanyVendorApprovedListWindowController.class, model, window);
    }

    @Autowired
    private CompanyVendorApprovedListService service;

    public BorderPane root;
    public TabPane tabPane;

    // base info tab
    public Tab baseInfoTab;
    public Button saveBtn;
    public TextField titleField;
    public TextArea descriptionField;
    public Label versionLabel;
    public DatePicker publishDateField;
    public TextField pathField;
    public Button verifyBtn;

    // vendor tab
    public Tab vendorTab;
    // file tab
    public Tab fileTab;

    @Override
    public void onShown(WindowEvent windowEvent) {
        super.onShown(windowEvent);
        getTitle().bind(viewModel.getTitle().map(v -> "[" + viewModel.getId().get() + "] " + viewModel.getTitle().getValue() + " 供方名录"));
    }

    @Override
    protected void registerTabSkins() {
        registerTabSkin(baseInfoTab, tab -> new CompanyVendorApprovedListTabSkinBase(this));
        registerTabSkin(vendorTab, tab -> new CompanyVendorApprovedListTabSkinVendors(this));
        registerTabSkin(fileTab, tab -> new CompanyVendorApprovedListTabSkinFiles(this));
    }

    @Override
    public CompanyVendorApprovedListService getViewModelService() {
        return service;
    }

    public void onVendorApprovedListOpenInExplorerAction(ActionEvent event) {
        String path = getEntity().getPath();
        if (!StringUtils.hasText(path)) {
            setStatus("未设置目录");
            return;
        }
        File file = new File(path);
        if (!file.exists()) {
            setStatus("目录错误,不存在");
            return;
        }
        DesktopUtils.showInExplorer(file);
    }

    public void onVendorApprovedListVerifyAction(ActionEvent event) {

    }

    public void onApprovedListCreatePathAction(ActionEvent event) {
        int id = viewModel.getId().get();
        CompanyVendorApprovedList list = service.findById(id);

        if (service.makePathAbsent(list)) {
            CompanyVendorApprovedList saved = service.save(list);
            viewModel.update(saved);
        } else {
            setStatus("目录存在或创建失败");
        }
    }

    public void onApprovedListChangePathAction(ActionEvent event) {
    }

    public void onVendorTableExportAction(ActionEvent event) {
        CompanyVendorApprovedListVendorExportTask task = new CompanyVendorApprovedListVendorExportTask();
        task.setApprovedList(getEntity());
        UITools.showTaskDialogAndWait("导出供方", task, null);
    }

    private void save(ActionEvent event) {
        saveTabSkins();
    }
}
