var JV = require('./jpc_value_define');
var JpcFieldHelper = require('./helper/jpc_helper_field');
var JpcBandHelper = require('./helper/jpc_helper_band');
var JpcBand = require('./jpc_band');
var JpcFlowTabHelper = require('./helper/jpc_helper_flow_tab');
var JpcCommonHelper = require('./helper/jpc_helper_common');
var JpcDiscreteHelper = require('./helper/jpc_helper_discrete');
var JpcTextHelper = require('./helper/jpc_helper_text');
var JpcCommonOutputHelper = require('./helper/jpc_helper_common_output');
var JpcAreaHelper = require('./helper/jpc_helper_area');

var JpcBillTabSrv = function(){};
JpcBillTabSrv.prototype.createNew = function(){
    var JpcBillTabResult = {};
    JpcBillTabResult.initialize = function() {
        var me = this;
        me.disp_fields_idx = [];
    };
    JpcBillTabResult.sorting = function(rptTpl) {
        var me = this;
        JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, rptTpl[JV.NODE_BILL_INFO][JV.NODE_BILL_CONTENT][JV.PROP_BILL_FIELDS], null, me.disp_fields_idx);
    };
    JpcBillTabResult.outputAsSimpleJSONPage = function (rptTpl, dataObj, page, bands, controls, $CURRENT_RPT) {
        var me = this, rst = [], tabRstLst = [];
        //1 calculate the band position
        var pageStatus = [true, false, false, false, false, false, false, false];
        JpcBandHelper.setBandArea(bands, rptTpl, pageStatus);
        //2. start to output detail-part
        var unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
        //2.1 output content
        tabRstLst.push(me.outputContent(rptTpl, dataObj, page, bands, unitFactor, controls, pageStatus));
        //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));
    }
    JpcBillTabResult.outputContent = function(rptTpl, dataObj, page, bands, unitFactor, controls, pageStatus) {
        var me = this, rst = [];
        var tab = rptTpl[JV.NODE_BILL_INFO][JV.NODE_BILL_CONTENT];
        var band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]] == true) {
                var tab_fields = tab[JV.PROP_BILL_FIELDS];
                var data_details = dataObj[JV.DATA_MASTER_DATA];
                for (var i = 0; i < tab_fields.length; i++) {
                    var tab_field = tab_fields[i];
                    var data_field = null;
                    if (me.disp_fields_idx[i] != JV.BLANK_FIELD_INDEX) {
                        data_field = data_details[me.disp_fields_idx[i]];
                    } else {
                        data_field = JE.F(tab_field[JV.PROP_FIELD_ID]);
                        if (data_field) {
                            data_field = data_field[JV.PROP_AD_HOC_DATA];
                        }
                    }
                    if (!(tab_field[JV.PROP_HIDDEN])) {
                        var cellItem = JpcCommonOutputHelper.createCommonOutput(tab_field, JpcFieldHelper.getValue(data_field, page - 1), controls);
                        cellItem[JV.PROP_AREA] = JpcAreaHelper.outputArea(tab_field[JV.PROP_AREA], band, unitFactor, 1, 0, 1, 0, 1, 0, true, 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 (var 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));
                }
            }
        }
        return rst;
    }
    return JpcBillTabResult;
}

module.exports = new JpcBillTabSrv();