package com.ecep.contract.controller.vendor;

import java.io.File;

import com.ecep.contract.VendorType;
import com.ecep.contract.vo.VendorCatalogVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.service.CompanyService;
import com.ecep.contract.service.VendorService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.CompanyVendorViewModel;

import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
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;
import com.ecep.contract.vo.VendorVo;
import com.ecep.contract.vo.CompanyVo;

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

    /**
     * 显示界面
     */
    public static void show(VendorVo companyVendor, Window window) {
        show(VendorWindowController.class, CompanyVendorViewModel.from(companyVendor), window);
    }

    public BorderPane root;
    public TabPane tabPane;
    public Tab baseInfoTab;
    public Tab fileTab;
    public Tab entityTab;

    public ComboBox<VendorCatalogVo> catalogField;
    public ComboBox<VendorType> typeField;
    public CheckBox protocolProviderField;
    public TextField pathField;
    public TextArea descriptionField;
    public TextField purchaseField;
    public TextField contactField;
    public DatePicker developDateField;
    public TextField createdField;
    public Label versionLabel;

    public Button relativeCompanyBtn;

    public Button createPathBtn;
    public Button changePathBtn;
    public Button pathAsNameBtn;

    @Override
    public void onShown(WindowEvent windowEvent) {
        super.onShown(windowEvent);
        getTitle().bind(viewModel.getCompany().map(companyId -> {
            if (companyId == null) {
                return "-";
            }
            CompanyVo company = getCompanyService().findById(companyId);
            return getMessage("ui.vendor.title", String.valueOf(viewModel.getId().get()), company.getName());
        }));
    }

    @Override
    protected void registerTabSkins() {
        registerTabSkin(baseInfoTab, tab -> new VendorTabSkinBase(this));
        registerTabSkin(fileTab, tab -> new VendorTabSkinFile(this));
        registerTabSkin(entityTab, tab -> new VendorTabSkinEntity(this));
    }

    @Override
    public VendorService getViewModelService() {
        return getCachedBean(VendorService.class);
    }

    public CompanyService getCompanyService() {
        return getCachedBean(CompanyService.class);
    }

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