/**
 * Created by Zhong on 2018/1/16.
 */
let gljClassTreeObj = {
    treeData: null,
    cache: null,//ref to tree.items
    tree: null,
    controller: null,
    workBook: null,
    sheet: null,
    setting: {
        sheet: {
            cols:[
                {
                    head: {
                        titleNames: ['名称'],
                        spanCols: [1],
                        spanRows: [2],
                        vAlign: [1, 1],
                        hAlign: [1, 1],
                        font: 'Arial'
                    },
                    data: {
                        field: 'Name',
                        vAlign: 1,
                        hAlign: 0,
                        font: 'Arial'
                    },
                    width: 400
                }
            ],
            headRows: 1,
            headRowHeight: [47],
            emptyRows: 0,
            treeCol: 0
        },
        tree: {
            id: 'ID',
            pid: 'ParentID',
            nid: 'NextSiblingID',
            rootId: -1
        },
        options: {
            tabStripVisible:  false,
            allowContextMenu: false,
            allowCopyPasteExcelStyle : false,
            allowExtendPasteRange: false,
            allowUserDragDrop : false,
            allowUserDragFill: false,
            scrollbarMaxAlign : true
        }
    },

    isDef: function (v) {
        return v !== undefined && v !== null;
    },
    isFunc: function (v) {
        return this.isDef(v) && typeof v === 'function';
    },
    //sheet things
    setOptions: function (workbook, opts) {
        for(let opt in opts){
            workbook.options[opt] = opts[opt];
        }
    },

    renderFunc: function (sheet, func) {
        sheet.suspendPaint();
        sheet.suspendEvent();
        if(this.isFunc(func)){
            func();
        }
        sheet.resumePaint();
        sheet.resumeEvent();
    },

    buildSheet: function () {
        if(!this.isDef(this.workBook)){
            this.workBook = new GC.Spread.Sheets.Workbook($('#gljClassSpread')[0], {sheetCount: 1});
            sheetCommonObj.spreadDefaultStyle(this.workBook);
            this.sheet = this.workBook.getActiveSheet();
            this.setOptions(this.workBook, this.setting.options);
            this.sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
            this.bindEvents(this.sheet);
        }
    },

    bindEvents: function (sheet) {
        let me = gljClassTreeObj;
        const Events = GC.Spread.Sheets.Events;
        sheet.bind(Events.SelectionChanging, me.onSelectionChanged);
        sheet.bind(Events.EditStarting, me.onEditStarting);
        sheet.bind(Events.ClipboardPasting, me.onClipboardPasting);
    },

    onSelectionChanged: function (sender, info) {
        let me = gljClassTreeObj;
        if(info.oldSelections.length === 0 && info.newSelections.length > 0 || info.oldSelections[0].row !== info.newSelections[0].row){
            let row = info.newSelections[0].row;
            let node = me.cache[row];
            me.initSelection(node);
        }
    },

    onEditStarting: function (sender, args) {
        args.cancel = true;
    },

    onClipboardPasting: function (sender, info) {
        info.cancel = true;
    },

    getGljClassTree: function (gljLibId, callback) {
        let me = gljClassTreeObj;
        let re = repositoryGljObj;
        let url = '/complementartGlj/api/getMixedTree';
        let postData = {gljLibId: gljLibId};
        let sucFunc = function (rstData) {
            me.treeData = rstData;
            let compleTreeData = me.treeData.comple;
            if (compleTreeData && compleTreeData.length > 0) {
                me.gljCurTypeId = compleTreeData[0].ID;
            }
            //init
            me.buildSheet();
            me.initTree(compleTreeData);
            me.cache = me.tree.items;
            re.updateParentNodeIds(me.cache, re);
            me.initController(me.tree, me.sheet, me.setting.sheet);
            me.controller.showTreeData();
            me.sheet.setFormatter(-1, 0, '@');
            if(callback){
                callback();
            }
        };
        CommonAjax.post(url, postData, sucFunc);
    },

    initTree: function (datas) {
        this.tree = idTree.createNew(this.setting.tree);
        this.tree.loadDatas(datas);
        this.tree.selected = this.tree.items.length > 0 ? this.tree.items[0] : null;
    },

    initController: function (tree, sheet, setting) {
        this.controller = TREE_SHEET_CONTROLLER.createNew(tree, sheet, setting);
    },
    //模仿默认点击
    initSelection: function (node) {
        let me = this,
            re = repositoryGljObj,
            that = gljComponentOprObj;
        if(!re.isDef(node)){
            return;
        }

        let gljTypeId = node.data.ID;
        re.gljCurTypeId = node.data.ID;
        re.addGljObj = null;
        sheetOpr.cleanSheet(that.workBook.getSheet(0), that.setting, 5);
        if (re.parentNodeIds["_pNodeId_" + gljTypeId]) {
            re.currentOprParent = 1;
            re.currentCache = re.getParentCache(re.parentNodeIds["_pNodeId_" + gljTypeId]);
            re.workBook.getSheet(0).setRowCount(re.currentCache.length);
        } else {
            re.currentOprParent = 0;
            re.currentCache = re.getCache();
        }
        re.showGljItems(re.complementaryGljList, gljTypeId);
        re.initSel(re.workBook.getSheet(0).getActiveRowIndex());
        me.workBook.focus(true);
    }
}