package xyz.wbsite.dbtool.javafx;

import javafx.application.Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.Toggle;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.ChoiceBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.image.Image;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.Callback;
import javafx.util.converter.DefaultStringConverter;
import javafx.util.converter.IntegerStringConverter;
import xyz.wbsite.dbtool.javafx.ctrl.DetailModuleController;
import xyz.wbsite.dbtool.javafx.ctrl.DetailProjectController;
import xyz.wbsite.dbtool.javafx.ctrl.DetailTableController;
import xyz.wbsite.dbtool.javafx.ctrl.MainController;
import xyz.wbsite.dbtool.javafx.manger.ManagerFactory;
import xyz.wbsite.dbtool.javafx.manger.ProjectManager;
import xyz.wbsite.dbtool.javafx.po.DataBase;
import xyz.wbsite.dbtool.javafx.po.Field;
import xyz.wbsite.dbtool.javafx.po.FieldType;
import xyz.wbsite.dbtool.javafx.po.Module;
import xyz.wbsite.dbtool.javafx.po.Project;
import xyz.wbsite.dbtool.javafx.po.Table;
import xyz.wbsite.dbtool.javafx.tool.Tool;
import xyz.wbsite.dbtool.javafx.view.DBCheckBoxTableCell;
import xyz.wbsite.dbtool.web.frame.utils.ResourceUtil;

import java.net.URL;
import java.util.List;

public class JavaFxApplication extends Application {

    private ProjectManager projectManager = ManagerFactory.getProjectManager();
    private TreeView mTree = null;
    private Pane mDetail = null;
    private TableView mFields = null;
    private Button add = null;
    private Button sub = null;
    private FXMLLoader mMainLoader;
    private FXMLLoader mProjectLoader;
    private FXMLLoader mModuleLoader;
    private FXMLLoader mTableLoader;
    private MainController mainController;
    private DetailProjectController detailProjectController;
    private DetailModuleController mdDetailController;
    private DetailTableController detailTableController;
    private Project currentProject;
    private Module currentMD;
    private Table currentTable;
    private ContextMenu project_menu;
    private ContextMenu md_right_menu;
    private ContextMenu table_right_menu;
    private XEventHandler xEventHandler = new XEventHandler();
    private boolean dragMD = false;

    @Override
    public void start(Stage primaryStage) throws Exception {

        BorderPane root = mMainLoader.getRoot();

        primaryStage.setTitle("DBtool");
        primaryStage.setScene(new Scene(root, 700, 500));
        primaryStage.show();

        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                primaryStage.close();
                System.exit(0);
            }
        });
        primaryStage.getIcons().add(new Image(ResourceUtil.getResourceInput("icon.png")));
    }

    @Override
    public void init() throws Exception {
        URL main = JavaFxApplication.class.getClassLoader().getResource("fxml/main.fxml");
        if (main == null) {
            main = getClass().getResource("../../../fxml/main.fxml");
        }
        mMainLoader = new FXMLLoader(main);
        mMainLoader.load();
        mainController = mMainLoader.getController();
        mainController.setMain(this);
        mTree = mainController.getDbtree();
        mDetail = mainController.getDetail();
        mFields = mainController.getFeilds();
        mFields.setEditable(true);
        mFields.setSortPolicy(new Callback<TableView, Boolean>() {
            @Override
            public Boolean call(TableView param) {
                //禁止点击列头排序
                return false;
            }
        });
        add = mainController.getAdd();
        add.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                addField();
            }
        });
        sub = mainController.getSub();
        sub.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                subField();
            }
        });

        ContextMenu con = new ContextMenu(new MenuItem("新增"), new MenuItem("删除"), new MenuItem("向上调整"), new MenuItem("向下调整"));
        con.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                MenuItem target = (MenuItem) event.getTarget();
                int index = mFields.getSelectionModel().getSelectedIndex();
                List<Field> fields = currentTable.getFields();

                if ("新增".equals(target.getText())) {
                    addField();
                }

                if (index != -1) {
                    switch (target.getText()) {
                        case "删除":
                            subField();
                            break;
                        case "向上调整":
                            if (index > 0) {
                                fields.add(index - 1, fields.get(index));
                                fields.add(index + 1, fields.get(index));
                                fields.remove(index);
                                fields.remove(index + 1);
                            }
                            break;
                        case "向下调整":
                            if (index < fields.size() - 1) {
                                fields.add(index, fields.get(index + 1));
                                fields.add(index + 2, fields.get(index + 1));
                                fields.remove(index + 1);
                                fields.remove(index + 2);
                            }
                            break;
                    }
                    loadTable();
                    JavaFxApplication.this.mFields.getSelectionModel().clearSelection();
                }
            }
        });
        mFields.setContextMenu(con);

        project_menu = new ContextMenu(new MenuItem("新增模块"));
        md_right_menu = new ContextMenu(new MenuItem("新增对象"), new MenuItem("删除模块"), new MenuItem("向上调整"), new MenuItem("向下调整"));
        table_right_menu = new ContextMenu(new MenuItem("删除对象"));
        project_menu.setOnAction(xEventHandler);
        md_right_menu.setOnAction(xEventHandler);
        table_right_menu.setOnAction(xEventHandler);

        URL projectdetail = JavaFxApplication.class.getClassLoader().getResource("fxml/projectdetail.fxml");
        if (projectdetail == null) {
            projectdetail = getClass().getResource("../../../fxml/projectdetail.fxml");
        }
        mProjectLoader = new FXMLLoader(projectdetail);
        mProjectLoader.load();
        detailProjectController = mProjectLoader.getController();

        URL mddetail = JavaFxApplication.class.getClassLoader().getResource("fxml/moduledetail.fxml");
        if (mddetail == null) {
            mddetail = getClass().getResource("../../../fxml/moduledetail.fxml");
        }
        mModuleLoader = new FXMLLoader(mddetail);
        mModuleLoader.load();
        mdDetailController = mModuleLoader.getController();


        URL tabledetail = JavaFxApplication.class.getClassLoader().getResource("fxml/tabledetail.fxml");
        if (tabledetail == null) {
            tabledetail = getClass().getResource("../../../fxml/tabledetail.fxml");
        }
        mTableLoader = new FXMLLoader(tabledetail);
        mTableLoader.load();
        detailTableController = mTableLoader.getController();

        mTree.setShowRoot(true);
        mTree.setEditable(true);
        mTree.setDisable(false);
        mTree.setCellFactory(new Callback<TreeView, TreeCell>() {
            @Override
            public TreeCell call(TreeView param) {
                TextFieldTreeCell textFieldTreeCell = new TextFieldTreeCell(new DefaultStringConverter());
                textFieldTreeCell.setEditable(false);
                // creating cell from deafult factory
                TreeCell treeCell = textFieldTreeCell.forTreeView().call(param);
                // setting handlers
                textFieldTreeCell.setOnDragDetected(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        TextFieldTreeCell source = (TextFieldTreeCell) event.getSource();
                        String text = source.getText();
                        Module dbByDBName = projectManager.findDBByDBName(text);
                        if (dbByDBName != null) {
                            dragMD = true;
                        } else {
                            dragMD = false;
                        }
                        if (dragMD) {
                            System.out.println("拖拽模块:" + text);
                        } else {
                            System.out.println("拖拽对象:" + text);
                        }
                        Dragboard md = source.startDragAndDrop(TransferMode.ANY);
                        ClipboardContent content = new ClipboardContent();
                        content.putString((String) source.getText());
                        md.setContent(content);
                        event.consume();
                    }
                });
                textFieldTreeCell.setOnDragOver(new EventHandler<DragEvent>() {
                    @Override
                    public void handle(DragEvent event) {
                        Dragboard md = event.getDragboard();
                        TextFieldTreeCell source = (TextFieldTreeCell) event.getSource();

                        Module dbByDBName = projectManager.findDBByDBName(source.getText());

                        if (dragMD && dbByDBName != null) {
                            double y = event.getY();
                            double height = textFieldTreeCell.getHeight();

                            if (source.getText().equals(md.getString())) {
                                event.acceptTransferModes(TransferMode.MOVE);
                            } else if (y >= height / 4 && y < height * 3 / 4) {
                                event.acceptTransferModes(TransferMode.MOVE);
                            }
                        }

                        if (!dragMD && dbByDBName == null) {
                            double y = event.getY();
                            double height = textFieldTreeCell.getHeight();

                            if (y >= height / 4 && y < height * 3 / 4) {
                                event.acceptTransferModes(TransferMode.MOVE);
                            } else {
                                event.acceptTransferModes(TransferMode.COPY);
                            }
                        }
                        event.consume();
                    }
                });
                textFieldTreeCell.setOnDragDropped(new EventHandler<DragEvent>() {
                    @Override
                    public void handle(DragEvent event) {
                        Dragboard md = event.getDragboard();
                        String m1 = md.getString();

                        TreeCell source = (TreeCell<String>) event.getSource();
                        String m2 = ((TreeCell<String>) event.getGestureTarget()).getItem();

                        if (dragMD) {
                            List<Module> mds = projectManager.getMds();

                            int i1 = 0, i2 = 0;
                            for (int i = 0; i < mds.size(); i++) {
                                if (mds.get(i).getModuleName().equals(m1)) {
                                    i1 = i;
                                }
                                if (mds.get(i).getModuleName().equals(m2)) {
                                    i2 = i;
                                }
                            }

                            if (event.getTransferMode().equals(TransferMode.COPY)) {//插入

                            } else if (event.getTransferMode().equals(TransferMode.MOVE)) {//交换
                                Tool.exchange(mds, i1, i2);
                                Tool.exchange(currentProject.getChildren(), i1, i2);
                            }
                        } else {
                            if (currentMD != null) {
                                int i1 = 0, i2 = 0;
                                Table t1 = null, t2 = null;
                                for (int i = 0; i < currentMD.getTables().size(); i++) {
                                    if (currentMD.getTables().get(i).getTableName().equals(m1)) {
                                        i1 = i;
                                        t1 = currentMD.getTables().get(i);
                                    }
                                    if (currentMD.getTables().get(i).getTableName().equals(m2)) {
                                        i2 = i;
                                        t2 = currentMD.getTables().get(i);
                                    }
                                }
                                if (t1 == null || t2 == null) {
                                    return;
                                }

                                if (event.getTransferMode().equals(TransferMode.COPY)) {//插入
                                    double y = event.getY();
                                    double height = textFieldTreeCell.getHeight();
                                    if (y < height / 2) {
                                        currentMD.getTables().add(i2, t1);
                                        currentMD.getChildren().add(i2, t1);
                                    } else {
                                        currentMD.getTables().add(i2 + 1, t1);
                                        currentMD.getChildren().add(i2 + 1, t1);
                                    }
                                    if (i1 < i2) {
                                        currentMD.getTables().remove(i1);
                                        currentMD.getChildren().remove(i1);
                                    } else {
                                        currentMD.getTables().remove(i1 + 1);
                                        currentMD.getChildren().remove(i1 + 1);
                                    }

                                } else if (event.getTransferMode().equals(TransferMode.MOVE)) {//交换
                                    Tool.exchange(currentMD.getTables(), i1, i2);
                                    Tool.exchange(currentMD.getChildren(), i1, i2);
                                }
                            }
                        }
                        event.setDropCompleted(true);
                        event.consume();
                        mTree.getSelectionModel().clearSelection();
                        int focusedIndex = mTree.getFocusModel().getFocusedIndex();
                        System.out.println(focusedIndex);
                    }
                });
                return textFieldTreeCell;
            }
        });
        mTree.setOnEditCommit(new YEventHandler());
        mTree.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                TreeItem targetItem = (TreeItem) mTree.getSelectionModel().getSelectedItem();

                if (targetItem == null)
                    return;

                int level = getLevel(targetItem);

                switch (level) {
                    case 0: {//查看模块
                        mTree.setContextMenu(project_menu);
                        loadProject();
                    }
                    break;
                    case 1: {//查看模块

                        mTree.setContextMenu(md_right_menu);
                        currentMD = projectManager.findDBByDBName((String) targetItem.getValue());
                        currentTable = null;
                        loadingModule();
                    }
                    break;
                    case 2: {//查看对象
                        mTree.setContextMenu(table_right_menu);
                        TreeItem parent = targetItem.getParent();
                        currentMD = projectManager.findDBByDBName((String) parent.getValue());
                        if (currentMD != null) {
                            currentTable = projectManager.findTableByTableName(currentMD, (String) targetItem.getValue());
                        }
                        loadTable();
                        break;
                    }
                    default:
                        break;
                }
            }
        });


        {//初始化模块面板
            mdDetailController.getModuleComment().textProperty().addListener(new ChangeListener<String>() {
                @Override
                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    if (currentMD != null) {
                        currentMD.setModuleComment(newValue);
                    }
                }
            });

            mdDetailController.getModulePrefix().textProperty().addListener(new ChangeListener<String>() {
                @Override
                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    if (currentMD != null) {
                        currentMD.setModulePrefix(newValue);
                    }
                }
            });

            mdDetailController.getModuleName().textProperty().addListener(new ChangeListener<String>() {
                @Override
                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    if (currentMD != null) {
                        currentMD.setModuleName(newValue);
                    }
                }
            });
        }

        {// 初始化项目面板
            detailProjectController.getProjectName().textProperty().addListener(new ChangeListener<String>() {
                @Override
                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    currentProject.setProjectName(newValue);
                }
            });
            detailProjectController.getProjectBasePackage().textProperty().addListener(new ChangeListener<String>() {
                @Override
                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    currentProject.setProjectBasePackage(newValue);
                }
            });
            detailProjectController.getProjectAuthor().textProperty().addListener(new ChangeListener<String>() {
                @Override
                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    currentProject.setProjectAuthor(newValue);
                }
            });
            detailProjectController.getMultiDB().selectedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    currentProject.setNeedMoreDB(newValue);
                }
            });
            detailProjectController.getSysMD().selectedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    currentProject.setNeedSys(newValue);
                }
            });
            detailProjectController.getCloud().selectedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    currentProject.setNeedCloud(newValue);
                }
            });
            detailProjectController.getAsync().selectedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    currentProject.setNeedAsync(newValue);
                }
            });
            detailProjectController.getDatabase().selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
                @Override
                public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
                    RadioButton radioButton = (RadioButton) newValue;
                    currentProject.setDatabase(DataBase.valueOf(radioButton.getText()));
                }
            });
        }

        {//初始化表面板
            detailTableController.getTablename().textProperty().addListener(new ChangeListener<String>() {
                @Override
                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    if (currentTable != null) {
                        currentTable.setTableName(newValue);
//                        invalidateLeft();
                    }
                }
            });
            detailTableController.getTablecomment().textProperty().addListener(new ChangeListener<String>() {
                @Override
                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    if (currentTable != null) {
                        currentTable.setTableComment(newValue);
                    }
                }
            });
            detailTableController.getCreate().selectedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    currentTable.setCreate(newValue);
                }
            });
            detailTableController.getDelete().selectedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    currentTable.setDelete(newValue);
                }
            });
            detailTableController.getUpdate().selectedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    currentTable.setUpdate(newValue);
                }
            });
            detailTableController.getFind().selectedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    currentTable.setFind(newValue);
                }
            });
            detailTableController.getGet().selectedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    currentTable.setGet(newValue);
                }
            });
            detailTableController.getSearch().selectedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    currentTable.setSearch(newValue);
                }
            });
            detailTableController.getHtml().selectedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    currentTable.setHtml(newValue);
                }
            });
            detailTableController.getSys().selectedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    currentTable.setSys(newValue);
                    loadTable();
                }
            });
        }
        ManagerFactory.getReflashManager(this).start();

        loadProjectTree();
        super.init();
    }

    private void addField() {
        if (currentTable != null && mFields != null) {
            List<Field> fields = currentTable.getFields();
            String fieldName = projectManager.getNewFieldName(fields);
            System.out.println("新增字段-" + fieldName + "成功!");
            this.mFields.getSelectionModel().select(fields.size() - 1);
            loadTable();
        }
    }

    private void subField() {
        if (currentTable != null) {
            int selectedIndex = mFields.getSelectionModel().getSelectedIndex();
            if (selectedIndex > -1 && !currentTable.getFields().get(selectedIndex).getIsSystem()) {
                currentTable.getFields().remove(selectedIndex);
                mFields.getSelectionModel().clearSelection();
                loadTable();
            }

        }
    }

    public void loadProjectTree() {
        currentProject = projectManager.getProject();
        currentProject.setValue(currentProject.getProjectName());
        currentProject.setExpanded(true);

        mTree.setRoot(currentProject);

        for (int i = 0; i < currentProject.getModules().size(); i++) {
            Module module = currentProject.getModules().get(i);
            module.setExpanded(true);

            for (Table table : module.getTables()) {
                table.setExpanded(true);
                module.getChildren().add(table);
            }
        }

    }

    private int getLevel(TreeItem treeItem) {
        TreeItem root = mTree.getRoot();
        int level = 0;
        if (treeItem == root) {
            level = 0;
        } else if (treeItem.getParent() == root) {
            level = 1;
        } else if (treeItem.getParent().getParent() == root) {
            level = 2;
        }
        return level;
    }

    private void loadingModule() {
        if (currentMD == null) {
            return;
        }

        GridPane gridPane = mModuleLoader.getRoot();
        mdDetailController.getModuleComment().setText(currentMD.getModuleComment());
        mdDetailController.getModulePrefix().setText(currentMD.getModulePrefix());
        mdDetailController.getModuleName().setText(currentMD.getModuleName());

        if (gridPane != null) {
            mDetail.getChildren().clear();
            mDetail.getChildren().add(gridPane);
        }
        if (mFields != null && mFields.getItems() != null) {
            mFields.getItems().clear();
        }
    }

    /**
     * 加载表项目面板
     */
    public void loadProject() {
        GridPane gridPane = mProjectLoader.getRoot();
        detailProjectController.getProjectName().setText(currentProject.getProjectName());
        detailProjectController.getProjectBasePackage().setText(currentProject.getProjectBasePackage());
        detailProjectController.getProjectAuthor().setText(currentProject.getProjectAuthor());
        detailProjectController.getMultiDB().setSelected(currentProject.isNeedMoreDB());
        detailProjectController.getSysMD().setSelected(currentProject.isNeedSys());
        ObservableList<Toggle> toggles = detailProjectController.getDatabase().getToggles();
        for (Toggle tog : toggles) {
            RadioButton radioButton = (RadioButton) tog;
            if (radioButton.getText().equals(currentProject.getDatabase().name())) {
                radioButton.setSelected(true);
            }
        }
        detailProjectController.getCloud().setSelected(currentProject.isNeedCloud());
        detailProjectController.getAsync().setSelected(currentProject.isNeedAsync());

        if (gridPane != null) {
            mDetail.getChildren().clear();
            mDetail.getChildren().add(gridPane);
        }
        if (mFields != null && mFields.getItems() != null) {
            mFields.getItems().clear();
        }
    }

    /**
     * 加载表对象面板
     */
    private void loadTable() {
        GridPane gridPane = mTableLoader.getRoot();
        if (currentTable != null) {
            detailTableController.getTablename().setText(currentTable.getTableName());
            detailTableController.getTablecomment().setText(currentTable.getTableComment());
            detailTableController.getCreate().setSelected(currentTable.getCreate());
            detailTableController.getDelete().setSelected(currentTable.getDelete());
            detailTableController.getUpdate().setSelected(currentTable.getUpdate());
            detailTableController.getFind().setSelected(currentTable.getFind());
            detailTableController.getGet().setSelected(currentTable.getGet());
            detailTableController.getSearch().setSelected(currentTable.getSearch());
            detailTableController.getHtml().setSelected(currentTable.getHtml());
            detailTableController.getSys().setSelected(currentTable.getSys());
        }

        if (gridPane != null) {
            mDetail.getChildren().clear();
            mDetail.getChildren().add(gridPane);
        }
        ObservableList<TableColumn> columns = mFields.getColumns();
        columns.get(0).setCellValueFactory(new PropertyValueFactory<Field, String>("fieldName"));
        columns.get(0).setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn param) {

                param.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent>() {
                    @Override
                    public void handle(TableColumn.CellEditEvent event) {
                        int row = event.getTablePosition().getRow();
                        Field field = currentTable.getFields().get(row);

                        String newValue = (String) event.getNewValue();
                        if (mainController.getMcdx().isSelected()) {
                            newValue = Tool.any2Underline(newValue);
                        }

                        field.setFieldName(newValue);
                        if (newValue.toUpperCase().endsWith("_ID")) {
                            field.setFieldType(FieldType.Long);
                        }
                        if (newValue.toUpperCase().endsWith("_TIME") || newValue.toUpperCase().endsWith("_DATE")) {
                            field.setFieldType(FieldType.Date);
                        }
                        if (newValue.toUpperCase().endsWith("_TYPE")) {
                            field.setFieldType(FieldType.Dict);
                        }
                        loadTable();
                    }
                });

                TextFieldTableCell textFieldTableCell = new TextFieldTableCell(new DefaultStringConverter()) {

                    @Override
                    public void updateItem(Object item, boolean empty) {
                        super.updateItem(item, empty);
                        if (currentTable != null) {
                            int index = this.getTableRow().getIndex();
                            if (index >= 0 && index <= currentTable.getFields().size() - 1) {
                                Field field = currentTable.getFields().get(index);
                                if (field.getIsSystem()) {
                                    ignoreField(this);
                                    this.setDisable(true);
                                } else {
                                    recoveryField(this);
                                    this.setDisable(false);
                                }
                            }
                        }
                    }
                };

                textFieldTableCell.setOnDragDetected(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        TableCell source = (TableCell) event.getSource();
                        Dragboard md = source.startDragAndDrop(TransferMode.ANY);
                        ClipboardContent content = new ClipboardContent();
                        content.putString((String) source.getItem());
                        md.setContent(content);
//                        System.out.println("Dragging: " + db.getString());
                        event.consume();
                    }
                });
                textFieldTableCell.setOnDragOver(new EventHandler<DragEvent>() {
                    @Override
                    public void handle(DragEvent event) {
                        Dragboard md = event.getDragboard();

                        if (md.hasString()) {
                            double y = event.getY();
                            double height = textFieldTableCell.getHeight();

                            if (y >= height / 4 && y < height * 3 / 4) {
                                event.acceptTransferModes(TransferMode.MOVE);
                            } else {
                                event.acceptTransferModes(TransferMode.COPY);
                            }
                        }

//                        System.out.println("DragOver: " + db.getString());
                        event.consume();
                    }
                });
                textFieldTableCell.setOnDragDropped(new EventHandler<DragEvent>() {
                    @Override
                    public void handle(DragEvent event) {
                        Dragboard md = event.getDragboard();
                        String f1 = md.getString();

                        TableCell source = (TableCell) event.getSource();
                        String f2 = ((TableCell) event.getGestureTarget()).getText();

                        if (currentTable != null) {
                            int i1 = 0, i2 = 0;
                            Field t1 = null, t2 = null;
                            for (int i = 0; i < currentTable.getFields().size(); i++) {
                                if (currentTable.getFields().get(i).getFieldName().equals(f1)) {
                                    i1 = i;
                                    t1 = currentTable.getFields().get(i);
                                }
                                if (currentTable.getFields().get(i).getFieldName().equals(f2)) {
                                    i2 = i;
                                    t2 = currentTable.getFields().get(i);
                                }
                            }

                            if (event.getTransferMode().equals(TransferMode.COPY)) {//插入
                                double y = event.getY();
                                double height = textFieldTableCell.getHeight();

                                if (y < height / 2) {
                                    currentTable.getFields().add(i2, t1);
                                } else {
                                    currentTable.getFields().add(i2 + 1, t1);
                                }
                                if (i1 < i2) {
                                    currentTable.getFields().remove(i1);
                                } else {
                                    currentTable.getFields().remove(i1 + 1);
                                }

                            } else if (event.getTransferMode().equals(TransferMode.MOVE)) {//交换
                                currentTable.getFields().add(i1, t2);
                                currentTable.getFields().remove(i1 + 1);
                                currentTable.getFields().add(i2, t1);
                                currentTable.getFields().remove(i2 + 1);
                            }
                            loadTable();
                        }
                        event.setDropCompleted(true);
                        event.consume();
                    }
                });
                return textFieldTableCell;
            }
        });
        columns.get(1).setCellValueFactory(new PropertyValueFactory("fieldType"));
        columns.get(1).setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn param) {
                ObservableList<FieldType> objects = FXCollections.observableArrayList();

                Class clz = FieldType.class;
                for (Object obj : clz.getEnumConstants()) {
                    if (obj instanceof FieldType) {
                        objects.add((FieldType) obj);
                    }
                }
                ChoiceBoxTableCell choiceBoxTableCell = new ChoiceBoxTableCell(objects) {
                    @Override
                    public void updateItem(Object item, boolean empty) {
                        super.updateItem(item, empty);
                        if (currentTable != null) {
                            int index = this.getTableRow().getIndex();
                            if (index >= 0 && index <= currentTable.getFields().size() - 1) {
                                Field field = currentTable.getFields().get(index);
                                FieldType fieldType = field.getFieldType();
                                if (fieldType.isFix() && fieldType.getDefaultLength() != field.getFieldLength()) {
                                    field.setFieldLength(fieldType.getDefaultLength());
                                    mFields.refresh();
                                }
                                if (field.getIsSystem()) {
                                    ignoreField(this);
                                    this.setDisable(true);
                                } else {
                                    recoveryField(this);
                                    this.setDisable(false);
                                }
                            }
                        }
                    }
                };

                param.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent>() {
                    @Override
                    public void handle(TableColumn.CellEditEvent event) {
                        int row = event.getTablePosition().getRow();
                        Field field = currentTable.getFields().get(row);

                        FieldType oldFieldType = field.getFieldType();

                        FieldType newValue = (FieldType) event.getNewValue();
                        field.setFieldLength(newValue.getDefaultLength());
                        field.setFieldType(newValue);
                        mFields.refresh();
                    }
                });
                return choiceBoxTableCell;
            }
        });
        columns.get(2).setCellValueFactory(new PropertyValueFactory("fieldLength"));
        columns.get(2).setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn param) {
                param.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent>() {
                    @Override
                    public void handle(TableColumn.CellEditEvent event) {
                        int row = event.getTablePosition().getRow();
                        Field field = currentTable.getFields().get(row);
                        if ("String_var".equals(field.getFieldType().toString()) || "String_super".equals(field.getFieldType().toString())) {
                            try {
                                field.setFieldLength((Integer) event.getNewValue());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        mFields.refresh();
                    }
                });
                TextFieldTableCell textFieldTableCell = new TextFieldTableCell(new IntegerStringConverter()) {
                    @Override
                    public void updateItem(Object item, boolean empty) {
                        super.updateItem(item, empty);
                        if (currentTable != null) {
                            int index = this.getTableRow().getIndex();
                            if (index >= 0 && index <= currentTable.getFields().size() - 1) {
                                Field field = currentTable.getFields().get(index);
                                if (field.getIsSystem()) {
                                    ignoreField(this);
                                    this.setDisable(true);
                                } else {
                                    recoveryField(this);
                                    this.setDisable(false);
                                }
                            }
                        }
                    }
                };
                return textFieldTableCell;
            }
        });
        columns.get(3).setCellValueFactory(new PropertyValueFactory("defaultValue"));
        columns.get(3).setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn param) {
                param.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent>() {
                    @Override
                    public void handle(TableColumn.CellEditEvent event) {
                        int row = event.getTablePosition().getRow();
                        Field field = currentTable.getFields().get(row);
                        field.setDefaultValue((String) event.getNewValue());
                    }
                });
                return new TextFieldTableCell(new DefaultStringConverter()) {
                    @Override
                    public void updateItem(Object item, boolean empty) {
                        super.updateItem(item, empty);
                        if (currentTable != null) {
                            int index = this.getTableRow().getIndex();
                            if (index >= 0 && index <= currentTable.getFields().size() - 1) {
                                Field field = currentTable.getFields().get(index);
                                if (field.getIsSystem()) {
                                    ignoreField(this);
                                    this.setDisable(true);
                                } else {
                                    recoveryField(this);
                                    this.setDisable(false);
                                }
                            }
                        }
                    }
                };
            }
        });
        columns.get(4).setCellValueFactory(new PropertyValueFactory("isPrimaryKey"));
        columns.get(4).setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn param) {
                DBCheckBoxTableCell checkBoxTableCell = new DBCheckBoxTableCell();

                DBCheckBoxTableCell.sCallback sCallback = checkBoxTableCell.new sCallback() {
                    @Override
                    public ObservableValue<Boolean> call(Integer param) {
                        super.call(param);
                        List<Field> fields = currentTable.getFields();

                        if (fields.get(param).getIsSystem()) {
                            checkBoxTableCell.setInvalid(true);
                        } else {
                            checkBoxTableCell.setInvalid(false);
                        }
                        if (fields.get(param).getIsPrimaryKey()) {
                            return new SimpleBooleanProperty(true);
                        } else {
                            return new SimpleBooleanProperty(false);
                        }
                    }
                };
                checkBoxTableCell.setSelectedStateCallback(sCallback);
                checkBoxTableCell.setOnChangeListener(new ChangeListener<Boolean>() {
                    @Override
                    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                        int param1 = checkBoxTableCell.getParam();
                        Field field = currentTable.getFields().get(param1);
                        field.setIsPrimaryKey(newValue);
                    }
                });
                return checkBoxTableCell;
            }
        });
        columns.get(5).setCellValueFactory(new PropertyValueFactory("isMust"));
        columns.get(5).setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn param) {
                final DBCheckBoxTableCell checkBoxTableCell = new DBCheckBoxTableCell();
                DBCheckBoxTableCell.sCallback sCallback = checkBoxTableCell.new sCallback() {
                    @Override
                    public ObservableValue<Boolean> call(Integer param) {
                        super.call(param);
                        List<Field> fields = currentTable.getFields();
                        if (fields.get(param).getIsSystem()) {
                            checkBoxTableCell.setInvalid(true);
                        } else {
                            checkBoxTableCell.setInvalid(false);
                        }
                        if (fields.get(param).getIsMust()) {
                            return new SimpleBooleanProperty(true);
                        } else {
                            return new SimpleBooleanProperty(false);
                        }
                    }
                };
                checkBoxTableCell.setSelectedStateCallback(sCallback);
                checkBoxTableCell.setOnChangeListener(new ChangeListener<Boolean>() {
                    @Override
                    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                        int param1 = checkBoxTableCell.getParam();
                        Field field = currentTable.getFields().get(param1);
                        field.setIsMust(newValue);
                    }
                });
                return checkBoxTableCell;
            }
        });
        columns.get(6).setCellValueFactory(new PropertyValueFactory("isQuery"));
        columns.get(6).setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn param) {
                final DBCheckBoxTableCell checkBoxTableCell = new DBCheckBoxTableCell();
                DBCheckBoxTableCell.sCallback sCallback = checkBoxTableCell.new sCallback() {
                    @Override
                    public ObservableValue<Boolean> call(Integer param) {
                        super.call(param);
                        List<Field> fields = currentTable.getFields();
                        if (fields.get(param).getIsSystem()) {
                            checkBoxTableCell.setInvalid(true);
                        } else {
                            checkBoxTableCell.setInvalid(false);
                        }
                        if (fields.get(param).getIsQuery()) {
                            return new SimpleBooleanProperty(true);
                        } else {
                            return new SimpleBooleanProperty(false);
                        }
                    }
                };
                checkBoxTableCell.setSelectedStateCallback(sCallback);
                checkBoxTableCell.setOnChangeListener(new ChangeListener<Boolean>() {
                    @Override
                    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                        int param1 = checkBoxTableCell.getParam();
                        Field field = currentTable.getFields().get(param1);
                        field.setIsQuery(newValue);
                    }
                });
                return checkBoxTableCell;
            }
        });
        columns.get(7).setCellValueFactory(new PropertyValueFactory("isSearch"));
        columns.get(7).setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn param) {
                final DBCheckBoxTableCell checkBoxTableCell = new DBCheckBoxTableCell();
                DBCheckBoxTableCell.sCallback sCallback = checkBoxTableCell.new sCallback() {
                    @Override
                    public ObservableValue<Boolean> call(Integer param) {
                        super.call(param);
                        List<Field> fields = currentTable.getFields();
                        if (fields.get(param).getIsSystem()) {
                            checkBoxTableCell.setInvalid(true);
                        } else {
                            checkBoxTableCell.setInvalid(false);
                        }
                        if (fields.get(param).getIsSearch()) {
                            return new SimpleBooleanProperty(true);
                        } else {
                            return new SimpleBooleanProperty(false);
                        }
                    }
                };
                checkBoxTableCell.setSelectedStateCallback(sCallback);
                checkBoxTableCell.setOnChangeListener(new ChangeListener<Boolean>() {
                    @Override
                    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                        int param1 = checkBoxTableCell.getParam();
                        Field field = currentTable.getFields().get(param1);
                        field.setIsSearch(newValue);
                    }
                });
                return checkBoxTableCell;
            }
        });
        columns.get(8).setCellValueFactory(new PropertyValueFactory("fieldComment"));
        columns.get(8).setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn param) {

                param.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent>() {
                    @Override
                    public void handle(TableColumn.CellEditEvent event) {
                        int row = event.getTablePosition().getRow();
                        Field field = currentTable.getFields().get(row);
                        field.setFieldComment((String) event.getNewValue());
                    }
                });
                return new TextFieldTableCell(new DefaultStringConverter()) {
                    @Override
                    public void updateItem(Object item, boolean empty) {
                        super.updateItem(item, empty);
                        if (currentTable != null) {
                            int index = this.getTableRow().getIndex();
                            if (index >= 0 && index <= currentTable.getFields().size() - 1) {
                                Field field = currentTable.getFields().get(index);
                                if (field.getIsSystem()) {
                                    ignoreField(this);
                                    this.setDisable(true);
                                } else {
                                    recoveryField(this);
                                    this.setDisable(false);
                                }
                            }
                        }
                    }
                };
            }
        });

        ObservableList<Field> data = null;
        if (currentTable != null) {
            List<Field> fields = currentTable.getFields();
            data = FXCollections.observableArrayList();
            data.addAll(fields);
        }

        mFields.setItems(data);
    }

    void ignoreField(TableCell tableCell) {
        tableCell.setTextFill(Color.gray(0.8));
    }

    void recoveryField(TableCell tableCell) {
        tableCell.setTextFill(Color.BLACK);
    }


    /**
     * 双击目录树编辑处理器
     */
    private class YEventHandler implements EventHandler<TreeView.EditEvent> {

        @Override
        public void handle(TreeView.EditEvent event) {
            TreeItem treeItem = event.getTreeItem();
            int level = getLevel(treeItem);
            System.out.println("原值:" + event.getOldValue());
            System.out.println("现值:" + event.getNewValue());

            switch (level) {
                case 0: {//编辑项目名称
                    projectManager.getProject().setProjectName((String) event.getNewValue());
                    loadProject();
                }
                break;
                case 1: {//编辑模块
                    Module md = projectManager.findDBByDBName((String) event.getOldValue());
                    md.setModuleName((String) event.getNewValue());
                    loadingModule();
                }
                break;
                case 2: {//编辑对象
                    TreeItem parent = treeItem.getParent();
                    Module md = projectManager.findDBByDBName((String) parent.getValue());
                    Table table = projectManager.findTableByTableName(md, (String) event.getOldValue());
                    if (table != null) {
                        table.setTableName((String) event.getNewValue());
                        loadTable();
                    }
                    break;
                }
                default:
                    break;
            }
        }
    }

    /**
     * 目录树右击菜单处理器
     */
    private class XEventHandler implements EventHandler {

        @Override
        public void handle(Event event) {
            MenuItem target = (MenuItem) event.getTarget();

            String text = target.getText();
            Project project = (Project) mTree.getRoot();

            TreeItem targetItem = (TreeItem) mTree.getFocusModel().getFocusedItem();
            int index = -1;
            index = mTree.getFocusModel().getFocusedIndex();
            if (targetItem == null) {
                targetItem = (TreeItem) mTree.getSelectionModel().getSelectedItem();
            }
            if (index == -1) {
                index = mTree.getSelectionModel().getSelectedIndex();
            }
            if (text != null && targetItem != null) {
                switch (text) {

                    case "向上调整":
                        if (index > 0) {
                            Tool.exchange(projectManager.getProject().getModules(), index - 1, index);
                            Tool.exchange(projectManager.getProject().getChildren(), index - 1, index);
                        }
                        break;
                    case "向下调整":
                        List<Module> mds = projectManager.getMds();
                        if (index < mds.size() - 1) {
                            Tool.exchange(projectManager.getProject().getModules(), index, index + 1);
                            Tool.exchange(projectManager.getProject().getChildren(), index, index + 1);
                        }
                        break;
                    case "新增模块":
                        projectManager.getNewModuleName();
                        break;
                    case "删除模块":
                        if (targetItem != null && targetItem.getParent() == project) {
                            currentProject.getChildren().remove(targetItem);
                            boolean b = projectManager.removeDBByDBName((String) targetItem.getValue());
                            if (b) {
                                System.out.println("删除模块" + targetItem.getValue() + "成功!");
                            }
                        }
                        break;
                    case "新增对象":
                        int level = getLevel(targetItem);

                        switch (level) {
                            case 1: {//对模块右击
                                System.out.println("模块:" + targetItem.getValue());
                                Module md = projectManager.findDBByDBName((String) targetItem.getValue());
                                if (md != null) {
                                    Table newTableName = projectManager.getNewTableName(md);
                                    targetItem.getChildren().add(newTableName);
                                }
                            }
                            break;
                            case 2: {//对对象右击
                                TreeItem parent = targetItem.getParent();
                                System.out.println("对象:" + parent.getValue());
                                Module md = projectManager.findDBByDBName((String) parent.getValue());
                                Table newTableName = projectManager.getNewTableName(md);
                                parent.getChildren().add(new TreeItem<>(newTableName.getTableName()));
                                break;
                            }
                            default:
                                break;
                        }
                        break;
                    case "删除对象":
                        level = 0;
                        if (targetItem != null && targetItem.getParent() == project) {
                            level = 0;
                        } else if (targetItem != null && targetItem.getParent().getParent() == project) {
                            level = 1;
                        }
                        switch (level) {
                            case 0: {//对模块右击
                                System.out.println("模块:" + targetItem.getValue());
                                Module md = projectManager.findDBByDBName((String) targetItem.getValue());
                                Table newTableName = projectManager.getNewTableName(md);
                                targetItem.getChildren().add(new TreeItem<>(newTableName.getTableName()));
                            }
                            break;
                            case 1: {//对对象右击
                                TreeItem parent = targetItem.getParent();
                                System.out.println("对象:" + parent.getValue());
                                Module md = projectManager.findDBByDBName((String) parent.getValue());

                                for (Table table : md.getTables()) {
                                    if (table.getTableName().equals(targetItem.getValue())) {
                                        md.getTables().remove(table);
                                        md.getChildren().remove(targetItem);
                                        System.out.println("移除'" + targetItem.getValue() + "'对象成功!");
                                        break;
                                    }
                                }
                                break;
                            }
                            default:
                                break;
                        }
                    default:
                }
            } else if (text != null && targetItem == null) {
                switch (text) {
                    case "新增模块":
                        projectManager.getNewModuleName();
                        break;
                    default:
                        break;
                }
            }
        }
    }

    private void updateDbTree(List<Module> mds) {
        TreeItem root = mTree.getRoot();
        for (Module md : mds) {
            TreeItem<String> treeItem = new TreeItem<>(md.getModuleName());
            treeItem.setExpanded(true);
            for (Table table : md.getTables()) {
                TreeItem<String> tree = new TreeItem<>(table.getTableName());
                treeItem.getChildren().add(tree);
            }
            root.getChildren().add(treeItem);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
