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

ProjectController = {
    /* sc: tree_sheet_controller */
    syncDisplayNewNode: function (sc, newNode) {
        TREE_SHEET_HELPER.massOperationSheet(sc.sheet, function () {
            var sels = sc.sheet.getSelections();
            sc.sheet.addRows(newNode.serialNo(), 1);
            TREE_SHEET_HELPER.refreshTreeNodeData(sc.setting, sc.sheet, [newNode], false);
            sc.setTreeSelected(newNode);
            sc.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
            sc.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
        });
    },
    addBills: function (project, sheetController, std) {
        if (!project || !sheetController) { return null; }

        var target = project.getParentTarget(project.mainTree.selected, 'sourceType', project.Bills.getSourceType());
        var newSource = null, newNode = null;

        if (std) {
            newSource = project.Bills.insertStdBills(target ? target.source.getParentID() : project.Bills.tree.setting.rootId, target ? target.source.getNextSiblingID() : project.Bills.tree.setting.rootId, std.data);
        } else {
            newSource = project.Bills.insertBills(target ? target.source.getParentID() : project.mainTree.setting.rootId, target ? target.source.getNextSiblingID() : project.mainTree.setting.rootId);
        }
        newNode = project.mainTree.insert(target.getParentID(), target.getNextSiblingID());

        if (newNode) {
            newNode.source = newSource;
            newNode.sourceType = project.Bills.getSourceType();
            newNode.data = newSource.data;

            this.syncDisplayNewNode(sheetController, newNode);
        }
    },
    addRation: function (project, sheetController, std) {
        if (!project || !sheetController) { return null; }

        var selected = project.mainTree.selected;
        var newSource = null, newNode = null;
        if(selected==null){
            return;
        }
        if (selected.sourceType === project.Bills.getSourceType() && selected.source.children.length === 0) {
            if (std) {
                newSource = project.Ration.insertStdRation(selected.source.getID(), null, std);
                project.ration_glj.addRationGLJ(newSource,std);
            } else {
                newSource = project.Ration.insertRation(selected.source.getID());
            }
            newNode = project.mainTree.insert(selected.getID(), selected.tree.rootID());
        } else if (selected.sourceType === project.Ration.getSourceType()) {
            if (std) {
                newSource = project.Ration.insertStdRation(selected.source[project.masterField.ration], selected.source, std);
                project.ration_glj.addRationGLJ(newSource,std);
            } else {
                newSource = project.Ration.insertRation(selected.source[project.masterField.ration], selected.source);
            }
            newNode = project.mainTree.insert(selected.getParentID(), selected.getNextSiblingID());
        }
        if (newNode) {
            newNode.source = newSource;
            newNode.sourceType = project.Ration.getSourceType();
            newNode.data = newSource;

            this.syncDisplayNewNode(sheetController, newNode);
        }
    },
    calculateAll: function (project, sheetController, CalcType) {
        let date1 = new Date();
        let calc = new BillsCalc(project, CalcType);
        calc.calcAll();
        calc = null;
        let date2 = new Date();
        console.log(date2 - date1);
        sheetController.showTreeData();
        let date3 = new Date();
        console.log(date3 - date1);
    }
}
