package xyz.wbsite.dbtool.javafx.ctrl;

import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableView;
import javafx.scene.control.TreeView;
import javafx.scene.layout.Pane;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import xyz.wbsite.dbtool.javafx.JavaFxApplication;
import xyz.wbsite.dbtool.javafx.manger.ManagerFactory;
import xyz.wbsite.dbtool.javafx.manger.ProjectManager;
import xyz.wbsite.dbtool.javafx.tool.Dialog;

import java.io.File;

public class MainController {
    private ProjectManager dBmanger = ManagerFactory.getProjectManager();

    @FXML
    private TreeView dbtree;
    @FXML
    private Pane detail;
    @FXML
    private TableView feilds;
    @FXML
    private Button add;
    @FXML
    private Button sub;
    @FXML
    private CheckBox mcdx;

    private JavaFxApplication main;

    public ProjectManager getdBmanger() {
        return dBmanger;
    }

    public void setdBmanger(ProjectManager dBmanger) {
        this.dBmanger = dBmanger;
    }

    public JavaFxApplication getMain() {
        return main;
    }

    public void setMain(JavaFxApplication main) {
        this.main = main;
    }

    public Button getAdd() {
        return add;
    }

    public void setAdd(Button add) {
        this.add = add;
    }

    public Button getSub() {
        return sub;
    }

    public void setSub(Button sub) {
        this.sub = sub;
    }

    public TableView getFeilds() {
        return feilds;
    }

    public void setFeilds(TableView feilds) {
        this.feilds = feilds;
    }

    public TreeView getDbtree() {
        return dbtree;
    }

    public void setDbtree(TreeView dbtree) {
        this.dbtree = dbtree;
    }

    public Pane getDetail() {
        return detail;
    }

    public void setDetail(Pane detail) {
        this.detail = detail;
    }

    @FXML
    public void modelOpen(ActionEvent actionEvent) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("选择模型文件");
        fileChooser.setInitialDirectory(dBmanger.getPath());

        fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("模型文件", "*.xml", "*.XML"));

        Stage stage = new Stage();
        File file = fileChooser.showOpenDialog(stage);

        if (file != null && file.exists()) {
            dBmanger.invalidate(file);//初始化模型树 - 到内容
            main.loadProjectTree();//加载模型树 - 到组件
            main.loadProject();
        }
    }

    @FXML
    public void modelSave(ActionEvent actionEvent) {
        if (dBmanger.getPath() == null || "".equals(dBmanger.getPath())) {
            DirectoryChooser directoryChooser = new DirectoryChooser();
            Stage stage = new Stage();
            File file = directoryChooser.showDialog(stage);

            if (file == null) {
                System.err.println("请选择目录!");
            } else {
                dBmanger.setPath(file);
                System.out.println(file.getAbsolutePath());
            }
        }
        dBmanger.save();
    }

    @FXML
    public void modelSaveAs(ActionEvent actionEvent) {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        Stage stage = new Stage();
        File file = directoryChooser.showDialog(stage);

        if (file != null) {
            dBmanger.save(file);
        }
    }

    @FXML
    public void modelCreate(ActionEvent actionEvent) {
        dBmanger.modelCreate();
        main.loadProjectTree();
        main.loadProject();
    }

    @FXML
    public void modelDelete(ActionEvent actionEvent) {
        Dialog.showConfirmDialog("确认删除当前模型?", new EventHandler() {

            @Override
            public void handle(Event event) {
                dBmanger.delete();
                dBmanger.invalidate();
                main.loadProjectTree();
                main.loadProject();
            }
        });
    }

    @FXML
    public void modelImport(ActionEvent actionEvent) {
        Dialog.showDBConnectInput();
    }

    @FXML
    public void modelGenerate(ActionEvent actionEvent) {
        this.modelSave(null);
        if (dBmanger.doCheck()) {
            DirectoryChooser directoryChooser = new DirectoryChooser();
            directoryChooser.setInitialDirectory(dBmanger.getPath());
            Stage stage = new Stage();
            File file = directoryChooser.showDialog(stage);

            if (file != null) {
                System.out.println("生成目录:" + file.getAbsolutePath());
                dBmanger.generate(ManagerFactory.getProjectManager().getProject(), file);
            }
        }
    }

    @FXML
    public void generateVue(ActionEvent actionEvent) {
        Dialog.showVue();
    }

    @FXML
    public void generateService(ActionEvent actionEvent) {
        this.modelSave(null);
        if (dBmanger.doCheck()) {
            DirectoryChooser directoryChooser = new DirectoryChooser();
            directoryChooser.setInitialDirectory(dBmanger.getPath());
            Stage stage = new Stage();
            File file = directoryChooser.showDialog(stage);

            if (file != null) {
                System.out.println("生成目录:" + file.getAbsolutePath());
                dBmanger.generateService(ManagerFactory.getProjectManager().getProject(), file);
            }
        }
    }

    @FXML
    public void generateApi(ActionEvent actionEvent) {
        Dialog.showApi();
    }

    @FXML
    public void generateAndroid(ActionEvent actionEvent) {
        Dialog.showAndroid();
    }

    @FXML
    public void exit(ActionEvent actionEvent) {
        modelSave(actionEvent);
        System.exit(0);
    }

    public CheckBox getMcdx() {
        return mcdx;
    }

    public void setMcdx(CheckBox mcdx) {
        this.mcdx = mcdx;
    }
}
