package com.ecep.contract.controller.customer;

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.CompanyCustomer;
import com.ecep.contract.service.CompanyContactService;
import com.ecep.contract.service.CompanyCustomerService;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.util.ProxyUtils;
import com.ecep.contract.vm.CompanyCustomerViewModel;

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.Stage;
import javafx.stage.Window;
import javafx.stage.WindowEvent;

@Lazy
@Scope("prototype")
@Component
@FxmlPath("/ui/company/customer/customer.fxml")
public class CompanyCustomerWindowController extends AbstEntityController<CompanyCustomer, CompanyCustomerViewModel> {
    private static final Logger logger = LoggerFactory.getLogger(CompanyCustomerWindowController.class);

    /**
     * 显示界面
     */
    public static void show(CompanyCustomer customer, Window window) {
        CompanyCustomerViewModel viewModel = new CompanyCustomerViewModel();
        viewModel.update(customer);
        show(CompanyCustomerWindowController.class, viewModel, window);
    }

    public Tab baseInfoTab;
    public BorderPane root;
    public TabPane tabPane;
    public Tab fileTab;
    public Tab entityTab;
    public Tab satisfactionTab;
    public TextField companyField;
    public TextField contactField;
    public TextField pathField;
    public TextArea descriptionField;
    public DatePicker developDateField;
    public TextField createdField;
    public Label versionLabel;

    public Button relativeCompanyBtn;
    public Button createPathBtn;
    public Button changePathBtn;
    public Button pathAsNameBtn;
    public Button OpenCustomerPathInExplorerBtn;

    @Override
    public void show(Stage stage) {
        super.show(stage);
        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.customer.title", String.valueOf(viewModel.getId().get()), company.getName());
        }));
    }

    @Override
    protected void registerTabSkins() {
        registerTabSkin(baseInfoTab, tab -> new CompanyCustomerTabSkinBase(this));
        registerTabSkin(fileTab, tab -> new CustomerTabSkinFile(this));
        registerTabSkin(entityTab, tab -> new CustomerTabSkinEntity(this));
        registerTabSkin(satisfactionTab, tab -> new CustomerTabSkinSatisfactionSurvey(this));
    }

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

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

    public CompanyContactService getCompanyContactService() {
        return getCachedBean(CompanyContactService.class);
    }

    @Override
    public void onShown(WindowEvent windowEvent) {
        super.onShown(windowEvent);
        OpenCustomerPathInExplorerBtn.setOnAction(this::onCompanyCustomerOpenInExplorerAction);
    }

    public void onCompanyCustomerOpenInExplorerAction(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);
    }

}
