package com.ecep.contract.controller.vendor;

import java.io.File;

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.model.CompanyVendor;
import com.ecep.contract.model.VendorTypeLocal;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.service.CompanyVendorService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.util.ProxyUtils;
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;

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

    /**
     * 显示界面
     */
    public static void show(CompanyVendor companyVendor, Window window) {
        CompanyVendorViewModel viewModel = new CompanyVendorViewModel();
        viewModel.update(companyVendor);
        show(CompanyVendorWindowController.class, viewModel, window);
    }

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

    public ComboBox<VendorTypeLocal> catalogField;
    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(company -> {
            if (company == null) {
                return "-";
            }
            if (!ProxyUtils.isInitialized(company)) {
                company = getCompanyService().findById(company.getId());
                viewModel.getCompany().set(company);
            }
            return getMessage("ui.vendor.title", String.valueOf(viewModel.getId().get()), company.getName());
        }));
    }

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

    @Override
    public CompanyVendorService getViewModelService() {
        return getCachedBean(CompanyVendorService.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);
    }
}
