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

var JpcDiscreteHelper = {
    outputDiscreteInfo: function (discreteArray, bands, dataObj, unitFactor, pageStatus, segIdx, multiCols, multiColIdx, $CURRENT_RPT) {
        var rst = [];
        if (discreteArray && dataObj) {
            for (var i = 0; i < discreteArray.length; i++) {
                var 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 (var 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 (var j = 0; j < discreteArray[i][JV.PROP_DISCRETE_FIELDS].length; j++) {
                            var df = discreteArray[i][JV.PROP_DISCRETE_FIELDS][j];
                            var value = "";
                            if (df[JV.PROP_FIELD_ID]) {
                                var field = JE.F(df[JV.PROP_FIELD_ID], $CURRENT_RPT);
                                if (field.DataSeq != JV.BLANK_FIELD_INDEX) {
                                    var 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]) {
                                var param = JE.P(df[JV.PROP_PARAM_ID], $CURRENT_RPT);
                                value = param[JV.PROP_DFT_VALUE];
                            }
                            var 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;