let JV = require('../jpc_value_define');
let JE = require('../jpc_rte');
let JpcTextHelper = require('./jpc_helper_text');
let JpcCommonOutputHelper = require('./jpc_helper_common_output');
let JpcAreaHelper = require('./jpc_helper_area');
let JpcFieldHelper = require('./jpc_helper_field');

let JpcDiscreteHelper = {
    outputDiscreteInfo: function (discreteArray, bands, dataObj, unitFactor, pageStatus, segIdx, multiCols, multiColIdx, $CURRENT_RPT, customizeCfg) {
        let rst = [];
        if (discreteArray && dataObj) {
            for (let i = 0; i < discreteArray.length; i++) {
                let band = bands[discreteArray[i][JV.PROP_BAND_NAME]];
                if (band && pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]] === true) {
                    if (discreteArray[i][JV.PROP_TEXT]) {
                        rst.push(JpcTextHelper.outputText(discreteArray[i][JV.PROP_TEXT], band, unitFactor, 1, 0, 1, 0, multiCols, multiColIdx));
                    }
                    if (discreteArray[i][JV.PROP_TEXTS]) {
                        for (let j = 0; j < discreteArray[i][JV.PROP_TEXTS].length; j++) {
                            rst.push(JpcTextHelper.outputText(discreteArray[i][JV.PROP_TEXTS][j], band, unitFactor, 1, 0, 1, 0, multiCols, multiColIdx));
                        }
                    }
                    if (discreteArray[i][JV.PROP_DISCRETE_FIELDS]) {
                        let flexiblePrecisionRefObj = null, flexibleRefField = null;
                        for (let j = 0; j < discreteArray[i][JV.PROP_DISCRETE_FIELDS].length; j++) {
                            let df = discreteArray[i][JV.PROP_DISCRETE_FIELDS][j];
                            let value = "";
                            if (df[JV.PROP_FIELD_ID]) {
                                // let map_data_field = JE.F(df[JV.PROP_FIELD_ID], $CURRENT_RPT);
                                let map_data_field = JE.F(df[JV.PROP_FIELD_ID], $CURRENT_RPT);
                                if (map_data_field.DataSeq !== JV.BLANK_FIELD_INDEX) {
                                    let data = dataObj[map_data_field.DataNodeName][map_data_field.DataSeq];
                                    if (data && data.length > 0) {
                                        if (data.length > segIdx) {
                                            value = data[segIdx];
                                        } else {
                                            value = data[0];
                                        }
                                    }
                                } else {
                                    if (map_data_field[JV.PROP_AD_HOC_DATA] && map_data_field[JV.PROP_AD_HOC_DATA].length > 0) {
                                        value = map_data_field[JV.PROP_AD_HOC_DATA][0]
                                    } else value = "";
                                }
                                if (map_data_field && map_data_field[JV.PROP_PRECISION] && map_data_field.flexiblePrecisionRefObj) {
                                    let pre_ref_data = null;
                                    if (flexiblePrecisionRefObj === null) {
                                        flexiblePrecisionRefObj = {};
                                        flexibleRefField = JE.F(map_data_field[JV.PROP_PRECISION][JV.PROP_FLEXIBLE_REF_FILED_ID], $CURRENT_RPT);
                                        pre_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(df, pre_ref_data, flexiblePrecisionRefObj, segIdx, customizeCfg);
                                } else {
                                    JpcFieldHelper.resetFormat(df, map_data_field, customizeCfg);
                                }
                            } else if (df[JV.PROP_PARAM_ID]) {
                                let param = JE.P(df[JV.PROP_PARAM_ID], $CURRENT_RPT);
                                value = param[JV.PROP_DFT_VALUE];
                            }
                            let item = JpcCommonOutputHelper.createCommonOutputWithoutDecorate(df, value, true);
                            //position
                            item[JV.PROP_AREA] = JpcAreaHelper.outputArea(df[JV.PROP_AREA], band, unitFactor, 1, 0, 1, 0, multiCols, multiColIdx, false, false);
                            rst.push(item);
                        }
                    }
                }
            }
        }
        return rst;
    },
    outputPreviewDiscreteInfo: function (discreteArray, bands, unitFactor, pageStatus) {
        let rst = [];
        for (let i = 0; i < discreteArray.length; i++) {
            let band = bands[discreteArray[i][JV.PROP_BAND_NAME]];
            if (band && pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]] === true) {
                if (discreteArray[i][JV.PROP_TEXT]) {
                    rst.push(JpcTextHelper.outputText(discreteArray[i][JV.PROP_TEXT], band, unitFactor, 1, 0, 1, 0, 1, 0));
                }
                if (discreteArray[i][JV.PROP_TEXTS]) {
                    for (let j = 0; j < discreteArray[i][JV.PROP_TEXTS].length; j++) {
                        rst.push(JpcTextHelper.outputText(discreteArray[i][JV.PROP_TEXTS][j], band, unitFactor, 1, 0, 1, 0, 1, 0));
                    }
                }
                if (discreteArray[i][JV.PROP_DISCRETE_FIELDS]) {
                    for (let j = 0; j < discreteArray[i][JV.PROP_DISCRETE_FIELDS].length; j++) {
                        let df = discreteArray[i][JV.PROP_DISCRETE_FIELDS][j];
                        let value = "";
                        if (df[JV.PROP_DFT_VALUE]) {
                            value = df[JV.PROP_DFT_VALUE];
                        }
                        let item = JpcCommonOutputHelper.createCommonOutputWithoutDecorate(df, value, null);
                        //position
                        item[JV.PROP_AREA] = JpcAreaHelper.outputArea(df[JV.PROP_AREA], band, unitFactor, 1, 0, 1, 0, 1, 0, false, false);
                        rst.push(item);
                    }
                }

            }
        }
        return rst;
    },
    outputPreviewSumInfoOutput: function (band, unitFactor, tab) {
        let rst = [];
        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.PROP_SUM_FIELDS]) {
            for (let i = 0; i < tab[JV.PROP_SUM_FIELDS].length; i++) {
                let tab_field = tab[JV.PROP_SUM_FIELDS][i];
                if (!(tab_field[JV.PROP_HIDDEN])) {
                    // rst.push(me.outputTabField(band, tab_field, null, -1, -1, 1, 0, 1, 0, unitFactor, true, controls, 0));
                    let value = "";
                    let item = JpcCommonOutputHelper.createCommonOutputWithoutDecorate(tab_field, value, null);
                    //position
                    item[JV.PROP_AREA] = JpcAreaHelper.outputArea(tab_field[JV.PROP_AREA], band, unitFactor, 1, 0, 1, 0, 1, 0, false, false);
                    rst.push(item);
                }
            }
        }
        return rst;
    }
}

module.exports = JpcDiscreteHelper;