/**
 * Created by Mai on 2017/4/1.
 */

var TREE_SHEET_CONTROLLER = {
    createNew: function (tree, sheet, setting) {
        var controller = function () {
            this.tree = tree;
            this.sheet = sheet;
            this.setting = setting;
            this.event = {
                refreshBaseActn: null,
                treeSelectedChanged: null
            };
            TREE_SHEET_HELPER.loadSheetHeader(this.setting, this.sheet);
        };

        controller.prototype.showTreeData = function () {
            var that = this;
            TREE_SHEET_HELPER.showTreeData(this.setting, this.sheet, this.tree);
            this.sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, function (e, info) {
                that.setTreeSelected(that.tree.items[info.newSelections[0].row]);
            });
        };

        controller.prototype.insert = function () {
            var newNode = null, that = this,  sels = this.sheet.getSelections();
            if (this.tree) {
                if (this.tree.selected) {
                    newNode = this.tree.insert(this.tree.selected.getParentID(), this.tree.selected.getNextSiblingID());
                } else {
                    newNode = this.tree.insert();
                }
                if (newNode) {
                    TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
                        that.sheet.addRows(newNode.serialNo(), 1);
                        TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [newNode], false);
                        that.setTreeSelected(newNode);
                        that.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
                        that.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
                    });
                }
            }
        };
        controller.prototype.delete = function () {
            var that = this, sels = this.sheet.getSelections();
            if (this.tree.selected) {
                if (this.tree.delete(this.tree.selected)) {
                    TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
                        that.sheet.deleteRows(sels[0].row, that.tree.selected.posterityCount() + 1);
                        that.setTreeSelected(that.tree.items[sels[0].row]);
                    });
                }
            }
        };
        controller.prototype.upLevel = function () {
            var that = this;
            if (this.tree.selected) {
                if (this.tree.selected.upLevel()) {
                    TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
                        TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected], that.sheet, true);
                        that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
                        if (that.event.refreshBaseActn) {
                            that.event.refreshBaseActn(that.tree);
                        }
                    });
                }
            }
        };
        controller.prototype.downLevel = function () {
            var that = this;
            if (this.tree.selected) {
                if (this.tree.selected.downLevel()) {
                    TREE_SHEET_HELPER.massOperationSheet(that.sheet, function () {
                        TREE_SHEET_HELPER.refreshNodesVisible([that.tree.selected.parent], that.sheet, true);
                        that.sheet.showRow(that.tree.selected.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
                        if (that.event.refreshBaseActn) {
                            that.event.refreshBaseActn(that.tree);
                        }
                    });
                }
            }
        };
        controller.prototype.upMove = function () {
            var that = this, sels = this.sheet.getSelections();
            if (this.tree.selected) {
                if (this.tree.selected.upMove()) {
                    TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
                        TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.nextSibling], true);
                        that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
                        if (that.event.refreshBaseActn) {
                            that.event.refreshBaseActn(that.tree);
                        }
                    });
                }
            }
        };
        controller.prototype.downMove = function () {
            var that = this, sels = this.sheet.getSelections();
            if (this.tree.selected) {
                if (this.tree.selected.downMove()) {
                    TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
                        TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, [that.tree.selected, that.tree.selected.preSibling], true);
                        that.sheet.setSelection(that.tree.selected.serialNo(), sels[0].col, 1, 1);
                        if (that.event.refreshBaseActn) {
                            that.event.refreshBaseActn(that.tree);
                        }
                    });
                }
            }
        };

        controller.prototype.refreshTreeNode = function (nodes, recursive) {
            var that = this;
            TREE_SHEET_HELPER.massOperationSheet(this.sheet, function () {
                TREE_SHEET_HELPER.refreshTreeNodeData(that.setting, that.sheet, nodes, recursive)
            })
        }

        controller.prototype.setTreeSelected = function (node) {
            this.tree.selected = node;
            if (this.event.refreshBaseActn) {
                this.event.refreshBaseActn(this.tree);
            }
            if (this.event.treeSelectedChanged) {
                this.event.treeSelectedChanged(this.tree.selected);
            }
        }

        controller.prototype.bind = function (eventName, eventFun) {
            this.event[eventName] = eventFun;
        };

        return new controller();
    },
    eventName: {
        refreshBaseActn: 'refreshBaseActn',
        treeSelectedChanged: 'treeSelectedChanged'
    }
};
