let JV = require('./jpc_value_define');
let JE = require('./jpc_rte');
let JpcFieldHelper = require('./helper/jpc_helper_field');
let JpcBandHelper = require('./helper/jpc_helper_band');
let JpcCommonHelper = require('./helper/jpc_helper_common');
let JpcDiscreteHelper = require('./helper/jpc_helper_discrete');
let JpcTextHelper = require('./helper/jpc_helper_text');
let JpcCommonOutputHelper = require('./helper/jpc_helper_common_output');
let JpcAreaHelper = require('./helper/jpc_helper_area');

let JpcBillTabSrv = function(){};
JpcBillTabSrv.prototype.createNew = function(){
    let JpcBillTabResult = {};
    JpcBillTabResult.initialize = function() {
        let me = this;
        me.disp_fields_idx = [];
    };
    JpcBillTabResult.sorting = function(rptTpl) {
        let me = this;
        JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, rptTpl[JV.NODE_BILL_INFO][JV.NODE_BILL_CONTENT][JV.PROP_BILL_FIELDS], null, me.disp_fields_idx);
    };
    JpcBillTabResult.paging = function(rptTpl, dataObj) {
        let rst = 0;
        function getDataLength(fields_str) {
            let dataFields = dataObj[fields_str];
            if (dataFields && dataFields.length > 0) {
                rst = dataFields[0].length;
            }
        }
        if (rptTpl[JV.NODE_FIELD_MAP]) {
            if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS]) {
                getDataLength(JV.DATA_DETAIL_DATA);
            } else if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS]) {
                getDataLength(JV.DATA_MASTER_DATA);
            } else if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS]) {
                getDataLength(JV.DATA_DISCRETE_DATA);
            } else if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_PARAMS]) {
                rst = 1;
            }
        }
        return rst;
    };
    JpcBillTabResult.outputAsPreviewPage = function (rptTpl, bands, controls, $CURRENT_RPT) {
        let me = this, rst = [], vIdx = [];
        //只预览第一页的数据
        let pageStatus = [true, true, false, true, false, false, false, false];
        JpcBandHelper.setBandArea(bands, rptTpl, pageStatus, true, false);
        let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
        rst = rst.concat(me.outputPreviewContent(rptTpl, bands, unitFactor, controls, pageStatus));
        rst = rst.concat(JpcDiscreteHelper.outputPreviewDiscreteInfo(rptTpl[JV.NODE_BILL_INFO][JV.NODE_DISCRETE_INFO], bands, unitFactor, pageStatus));
        return rst;
    };
    JpcBillTabResult.outputAsSimpleJSONPage = function (rptTpl, dataObj, page, bands, controls, $CURRENT_RPT, customizeCfg) {
        let me = this, rst = [], tabRstLst = [];
        //1 calculate the band position
        let pageStatus = [true, false, false, false, false, false, false, false];
        JpcBandHelper.setBandArea(bands, rptTpl, pageStatus);
        //2. start to output detail-part
        let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
        //2.1 output content
        tabRstLst.push(me.outputContent(rptTpl, dataObj, page, bands, unitFactor, controls, pageStatus, $CURRENT_RPT, customizeCfg));
        //2.2 output discrete
        tabRstLst.push(JpcDiscreteHelper.outputDiscreteInfo(rptTpl[JV.NODE_BILL_INFO][JV.NODE_DISCRETE_INFO], bands, dataObj, unitFactor, pageStatus, page - 1, 1, 0, $CURRENT_RPT, customizeCfg));
        for (let i = 0; i < tabRstLst.length; i++) {
            rst = rst.concat(tabRstLst[i]);
            tabRstLst[i] = null;
        }
        return rst;
    };
    JpcBillTabResult.outputPreviewContent = function(rptTpl, bands, unitFactor, controls, pageStatus) {
        let rst = [];
        let tab = rptTpl[JV.NODE_BILL_INFO][JV.NODE_BILL_CONTENT];
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]]) {
                let tab_fields = tab[JV.PROP_BILL_FIELDS];
                for (let i = 0; i < tab_fields.length; i++) {
                    let tab_field = tab_fields[i];
                    if (!(tab_field[JV.PROP_HIDDEN])) {
                        let cellItem = JpcCommonOutputHelper.createCommonOutput(tab_field, "", controls);
                        cellItem[JV.PROP_AREA] = JpcAreaHelper.outputArea(tab_field[JV.PROP_AREA], band, unitFactor, 1, 0, 1, 0, 1, 0, false, false);
                        rst.push(cellItem);
                    }
                }
                if (tab[JV.PROP_TEXT]) {
                    rst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXT], band, unitFactor, 1, 0, 1, 0, 1, 0));
                }
                if (tab[JV.PROP_TEXTS]) {
                    for (let j = 0; j < tab[JV.PROP_TEXTS].length; j++) {
                        rst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXTS][j], band, unitFactor, 1, 0, 1, 0, 1, 0));
                    }
                }
                if (tab[JV.NODE_DISCRETE_INFO]) {
                    rst = rst.concat(JpcDiscreteHelper.outputPreviewDiscreteInfo(tab[JV.NODE_DISCRETE_INFO], bands, unitFactor, pageStatus));
                }
            }
        }
        return rst;
    };
    JpcBillTabResult.outputContent = function(rptTpl, dataObj, page, bands, unitFactor, controls, pageStatus, $CURRENT_RPT, customizeCfg) {
        let me = this, rst = [];
        let tab = rptTpl[JV.NODE_BILL_INFO][JV.NODE_BILL_CONTENT];
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]]) {
                let tab_fields = tab[JV.PROP_BILL_FIELDS];
                let data_details = null;
                if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS]) {
                    data_details = dataObj[JV.DATA_DETAIL_DATA];
                } else if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS]) {
                    data_details = dataObj[JV.DATA_MASTER_DATA];
                }
                let flexiblePrecisionRefObj = null, flexibleRefField = null, precision_ref_data = null;
                for (let i = 0; i < tab_fields.length; i++) {
                    let tab_field = tab_fields[i];
                    let data_field = null, map_data_field = JE.F(tab_field[JV.PROP_FIELD_ID], $CURRENT_RPT);
                    if (me.disp_fields_idx.length > i && me.disp_fields_idx[i] !== JV.BLANK_FIELD_INDEX && (typeof me.disp_fields_idx[i] !== 'object')) {
                        data_field = data_details[me.disp_fields_idx[i]];
                    } else {
                        if (map_data_field) {
                            data_field = map_data_field[JV.PROP_AD_HOC_DATA];
                        }
                    }
                    if (map_data_field && map_data_field[JV.PROP_PRECISION] && map_data_field.flexiblePrecisionRefObj) {
                        if (flexiblePrecisionRefObj === null) {
                            flexiblePrecisionRefObj = {};
                            flexibleRefField = JE.F(map_data_field[JV.PROP_PRECISION][JV.PROP_FLEXIBLE_REF_FILED_ID], $CURRENT_RPT);
                            precision_ref_data = dataObj[map_data_field.DataNodeName][flexibleRefField.DataSeq];
                            for (let decimalObj of map_data_field.flexiblePrecisionRefObj) {
                                flexiblePrecisionRefObj["refUnit_" + decimalObj.unit] = decimalObj.decimal;
                            }
                        }
                        JpcFieldHelper.resetFlexibleFormat(tab_field, precision_ref_data, flexiblePrecisionRefObj, page - 1, customizeCfg);
                    } else {
                        if (page === 1) JpcFieldHelper.resetFormat(tab_field, map_data_field, customizeCfg);
                    }
                    if (!(tab_field[JV.PROP_HIDDEN])) {
                        let val = JpcFieldHelper.getValue(data_field, page - 1);
                        let cellItem = JpcCommonOutputHelper.createCommonOutput(tab_field, val, controls);
                        cellItem[JV.PROP_AREA] = JpcAreaHelper.outputArea(tab_field[JV.PROP_AREA], band, unitFactor, 1, 0, 1, 0, 1, 0, false, false);
                        rst.push(cellItem);
                    }
                }
                if (tab[JV.PROP_TEXT]) {
                    rst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXT], band, unitFactor, 1, 0, 1, 0, 1, 0));
                }
                if (tab[JV.PROP_TEXTS]) {
                    for (let j = 0; j < tab[JV.PROP_TEXTS].length; j++) {
                        rst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXTS][j], band, unitFactor, 1, 0, 1, 0, 1, 0));
                    }
                }
                if (tab[JV.NODE_DISCRETE_INFO]) {
                    rst = rst.concat(JpcDiscreteHelper.outputDiscreteInfo(tab[JV.NODE_DISCRETE_INFO], bands, dataObj, unitFactor, pageStatus, page - 1, 1, 0, customizeCfg));
                }
            }
        }
        return rst;
    };
    return JpcBillTabResult;
};

module.exports = new JpcBillTabSrv();