package com.ecep.contract.controller.company_old_name;

import java.io.File;
import java.util.concurrent.CompletableFuture;

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.CompanyOldName;
import com.ecep.contract.service.CompanyOldNameService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.CompanyFileViewModel;
import com.ecep.contract.vm.CompanyOldNameViewModel;

import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableView;
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/company_old_name.fxml")
public class CompanyOldNameWindowController extends AbstEntityController<CompanyOldName, CompanyOldNameViewModel> {
    private static final Logger logger = LoggerFactory.getLogger(CompanyOldNameWindowController.class);

    /**
     * 显示界面
     */
    public static void show(CompanyOldNameViewModel viewModel, Window window) {
        show(CompanyOldNameWindowController.class, viewModel, window);
    }

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

    public TextField nameField;
    public CheckBox ambiguityField;
    public DatePicker startDateField;
    public DatePicker endDateField;
    public TextField pathField;
    public TextArea descriptionField;

    public Label versionLabel;
    public Button saveBtn;
    public Button saveBtn2;

    public TableView<CompanyFileViewModel> fileTable;

    @Override
    public void onShown(WindowEvent windowEvent) {
        super.onShown(windowEvent);
        getTitle().set("[" + viewModel.getId().get() + "] " + viewModel.getName().getValue() + " 曾用名详情");
    }

    @Override
    protected void registerTabSkins() {
        registerTabSkin(baseInfoTab, this::createBaseTabSkin);
        registerTabSkin(fileTab, this::createFileTabSkin);
    }

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

    private CompanyOldNameTabSkinBase createBaseTabSkin(Tab tab) {
        CompanyOldNameTabSkinBase skin = new CompanyOldNameTabSkinBase(this);
        skin.setCompanyOldNameService(getViewModelService());
        return skin;
    }

    private CompanyOldNameTabSkinFile createFileTabSkin(Tab tab) {
        CompanyOldNameTabSkinFile skin = new CompanyOldNameTabSkinFile(this);
        skin.setCompanyOldNameService(getViewModelService());
        // skin.setCompanyFileService(companyFileService);
        return skin;
    }

    public void onOldCompanyOpenInExplorerAction(ActionEvent event) {
        CompanyOldName companyOldName = getEntity();
        String path = companyOldName.getPath();

        CompletableFuture.runAsync(() -> {
            if (!StringUtils.hasText(path)) {
                ButtonType buttonType = UITools.showConfirmation("目录未设置", "是否创建目录").join();
                if (buttonType == ButtonType.OK) {
                    if (getViewModelService().makePathAbsent(companyOldName)) {
                        save(companyOldName);
                    }
                } else {
                    setStatus("未设置目录");
                    return;
                }
            }
            File file = new File(companyOldName.getPath());
            DesktopUtils.showInExplorer(file);
        });
    }
}
