let JV = require('./jpc_value_define');
let JpcBand = require('./jpc_band');
let JpcFlowTab = require('./jpc_flow_tab');
let JpcBillTab = require('./jpc_bill_tab');
let JpcCrossTab = require('./jpc_cross_tab');
let JpcField = require('./jpc_field');
let JpcParam = require('./jpc_param');
let JpcFunc = require('./jpc_function');
let JpcData = require('./jpc_data');
let JpcCommonHelper = require('./helper/jpc_helper_common');
let $JE = require('./jpc_rte'); //Important: for self-define function execution purpose

let JpcExSrv = function(){};
JpcExSrv.prototype.createNew = function(){
    function private_buildDftItems(rptTpl, dftCollection, propArray, nodeName) {
        let rst = {};
        if (dftCollection) {
            for (let i = 0; i < dftCollection.length; i++) {
                let item = {};
                for (let j = 0; j < propArray.length; j++) {
                    item[propArray[j]] = dftCollection[i][propArray[j]];
                }
                rst[dftCollection[i][JV.PROP_ID]] = item;
            }
            if (rptTpl && rptTpl[nodeName] && rptTpl[nodeName].length > 0) {
                for (let i = 0; i < rptTpl[nodeName].length; i++) {
                    let rptDftItem = rptTpl[nodeName][i];
                    if (rst[rptDftItem[JV.PROP_ID]] == undefined) {
                        let item = {};
                        for (let j = 0; j < propArray.length; j++) {
                            item[propArray[j]] = rptDftItem[propArray[j]];
                        }
                        rst[rptDftItem[JV.PROP_ID]] = item;
                    }
                }
            }
        }
        return rst;
    }
    function private_buildDftControls(rptTpl, dftControlCollection) {
        let rst = private_buildDftItems(rptTpl,dftControlCollection, JV.CONTROL_PROPS, JV.NODE_CONTROL_COLLECTION);
        return rst;
    }
    function private_buildDftFonts(rptTpl, dftFontCollection) {
        let rst = private_buildDftItems(rptTpl,dftFontCollection, JV.FONT_PROPS, JV.NODE_FONT_COLLECTION);
        return rst;
    }
    function private_buildDftStyles(rptTpl, dftStyleCollection) {
        let rst = {};
        if (dftStyleCollection) {
            for (let i = 0; i < dftStyleCollection.length; i++) {
                let item = {};
                if (dftStyleCollection[i][JV.PROP_BORDER_STYLE] && dftStyleCollection[i][JV.PROP_BORDER_STYLE].length > 0) {
                    for (let j = 0; j < dftStyleCollection[i][JV.PROP_BORDER_STYLE].length; j++) {
                        let borderItem = {};
                        for (let k = 0; k < JV.BORDER_STYLE_PROPS.length; k++) {
                            borderItem[JV.BORDER_STYLE_PROPS[k]] = dftStyleCollection[i][JV.PROP_BORDER_STYLE][j][JV.BORDER_STYLE_PROPS[k]];
                        }
                        item[dftStyleCollection[i][JV.PROP_BORDER_STYLE][j][JV.PROP_POSITION]] = borderItem;
                    }
                }
                rst[dftStyleCollection[i][JV.PROP_ID]] = item;
            }
            if (rptTpl && rptTpl[JV.NODE_STYLE_COLLECTION] && rptTpl[JV.NODE_STYLE_COLLECTION].length > 0) {
                for (let i = 0; i < rptTpl[JV.NODE_STYLE_COLLECTION].length; i++) {
                    let rptDftItem = rptTpl[JV.NODE_STYLE_COLLECTION][i];
                    if (rst[rptDftItem[JV.PROP_ID]] == undefined) {
                        let item = {};
                        for (let j = 0; j < rptDftItem[JV.PROP_BORDER_STYLE].length; j++) {
                            let borderItem = {};
                            for (let k = 0; k < JV.BORDER_STYLE_PROPS.length; k++) {
                                borderItem[JV.BORDER_STYLE_PROPS[k]] = rptDftItem[JV.PROP_BORDER_STYLE][j][JV.BORDER_STYLE_PROPS[k]];
                            }
                            item[rptDftItem[JV.PROP_BORDER_STYLE][j][JV.PROP_POSITION]] = borderItem;
                        }
                        rst[rptDftItem[JV.PROP_ID]] = item;
                    }
                }
            }
        }
        return rst;
    }
    let JpcResult = {};
    //JpcResult.report_title
    JpcResult.initialize = function(rptTpl) {
        let me = this;
        if (rptTpl[JV.NODE_FLOW_INFO]) {
            me.flowTab = JpcFlowTab.createNew();
            me.flowTab.initialize();
        }
        if (rptTpl[JV.NODE_BILL_INFO]) {
            me.billTab = JpcBillTab.createNew();
            me.billTab.initialize();
        }
        //let dt1 = new Date();
        if (rptTpl[JV.NODE_CROSS_INFO]) {
            me.crossTab = JpcCrossTab.createNew();
            me.crossTab.initialize();
        }
        me.totalPages = 0;
        me.runTimePageData = {};
        me.fields = JpcField.createNew(rptTpl);
        me.params = JpcParam.createNew(rptTpl);
        me.formulas = JpcFunc.createNew(rptTpl);
    };

    JpcResult.analyzeData = function(rptTpl, dataObj, defProperties) {
        let me = this;
        //1. data object
        let dataHelper = JpcData.createNew();
        dataHelper.analyzeData(rptTpl, dataObj);
        //2. tab object
        //pre-condition: the data should be sorted in SQL/NoSQL level!
        //let dt1 = new Date();
        if (me.flowTab) {
            me.flowTab.sorting(rptTpl, dataObj, dataHelper.dataSeq.slice(0));
        }
        if (me.crossTab) {
            me.crossTab.sorting(rptTpl, dataObj, dataHelper.dataSeq.slice(0));
        }
        //let dt2 = new Date();
        //alert(dt2 - dt1);
        //3. formulas
        me.executeFormulas(JV.RUN_TYPE_BEFORE_PAGING, rptTpl, dataObj, me);
        //4. paging
        me.paging(rptTpl, dataObj, defProperties);
        //alert('analyzeData was completed!');
        //for garbage collection:
        dataHelper = null;
    };
    JpcResult.paging = function(rptTpl, dataObj, defProperties) {
        let me = this;
        if (me.flowTab) {
            me.totalPages = me.flowTab.preSetupPages(rptTpl, dataObj, defProperties);
        } else if (me.crossTab) {
            me.totalPages = me.crossTab.preSetupPages(rptTpl, defProperties);
        } else if (me.billTab) {
            //me.totalPages = billTab.paging();
        }
    };
    JpcResult.executeFormulas = function(runType, $CURRENT_TEMPLATE, $CURRENT_DATA, $CURRENT_RPT) {
        let me = this;
        for (let i = 0; i < me.formulas.length; i++) {
            if (me.formulas[i][JV.PROP_RUN_TYPE] === runType) {
                let expression = me.formulas[i][JV.PROP_EXPRESSION];
                if (expression) {
                    let $ME = me.formulas[i];
                    eval(expression);
                }
            }
        }
    };
    JpcResult.outputAsSimpleJSONPageArray = function(rptTpl, dataObj, startPage, endPage, defProperties) {
        let me = this, rst = {};
        if ((startPage > 0) && (startPage <= endPage) && (endPage <= me.totalPages)) {
            rst[JV.NODE_CONTROL_COLLECTION] = private_buildDftControls(rptTpl, (defProperties == null)?null:defProperties.ctrls);
            rst[JV.NODE_STYLE_COLLECTION] = private_buildDftStyles(rptTpl, (defProperties == null)?null:defProperties.styles);
            rst[JV.NODE_FONT_COLLECTION] = private_buildDftFonts(rptTpl, (defProperties == null)?null:defProperties.fonts);
            rst[JV.NODE_PAGE_INFO] = JpcCommonHelper.getPageSize(rptTpl);
            rst.items = [];
            let bands = JpcBand.createNew(rptTpl, defProperties);
            try {
                for (let page = startPage; page <= endPage; page++) {
                    me.runTimePageData.currentPage = page;
                    me.executeFormulas(JV.RUN_TYPE_BEFORE_OUTPUT, rptTpl, dataObj, me);
                    rst.items.push(me.outputAsSimpleJSONPage(rptTpl, dataObj, bands, page, rst[JV.NODE_CONTROL_COLLECTION]));
                }
                if (bands[JV.BAND_PROP_MERGE_BAND]) {
                    let mergedBand = {}, band = bands[JV.BAND_PROP_MERGE_BAND];
                    mergedBand[JV.PROP_LEFT] = band[JV.PROP_LEFT].toFixed(0);
                    mergedBand[JV.PROP_RIGHT] = band[JV.PROP_RIGHT].toFixed(0);
                    mergedBand[JV.PROP_TOP] = band[JV.PROP_TOP].toFixed(0);
                    mergedBand[JV.PROP_BOTTOM] = band[JV.PROP_BOTTOM].toFixed(0);
                    mergedBand[JV.BAND_PROP_STYLE] = band[JV.BAND_PROP_STYLE];
                    rst[JV.BAND_PROP_MERGE_BAND] = mergedBand;
                }
            } finally {
                bands = null;
            }
        }
        return rst;
    };
    JpcResult.outputAsSimpleJSONPage = function(rptTpl, dataObj, bands, page, controls) {
        let me = this, rst = null;
        if (me.totalPages >= page) {
            rst = {};
            rst[JV.PROP_PAGE_SEQ] = page;
            //rst.cells = [];
            if (me.flowTab) {
                rst.cells = me.flowTab.outputAsSimpleJSONPage(rptTpl, dataObj, page, bands, controls, me);
            } else if (me.crossTab) {
                rst.cells = me.crossTab.outputAsSimpleJSONPage(rptTpl, dataObj, page, bands, controls, me);
            } else if (me.billTab) {
                //
            }
        }
        return rst;
    };
    //JpcEx.rte.currentRptObj = JpcResult;
    return JpcResult;
}

module.exports = new JpcExSrv();