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 JpcDiscreteHelper = {
    outputDiscreteInfo: function (discreteArray, bands, dataObj, unitFactor, pageStatus, segIdx, multiCols, multiColIdx, $CURRENT_RPT) {
        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]) {
                        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 field = JE.F(df[JV.PROP_FIELD_ID], $CURRENT_RPT);
                                if (field.DataSeq != JV.BLANK_FIELD_INDEX) {
                                    let data = dataObj[field.DataNodeName][field.DataSeq];
                                    if (data && data.length > 0) {
                                        if (data.length > segIdx) {
                                            value = data[segIdx];
                                        } else {
                                            value = data[0];
                                        }
                                    }
                                } else {
                                    if (field[JV.PROP_AD_HOC_DATA] && field[JV.PROP_AD_HOC_DATA].length > 0) value = field[JV.PROP_AD_HOC_DATA][0]
                                    else value = "";
                                }
                            } 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, null);
                            //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;
    }
}

module.exports = JpcDiscreteHelper;