let JV = require('../jpc_value_define');
let JpcCommonHelper = require('./jpc_helper_common');

let JpcBandHelper = {
    getBandTypeValByString: function(bandType) {
        let rst = JV.PAGE_STATUS.indexOf(bandType);
        if (rst < 0) rst = JV.STATUS_NORMAL;
        return rst;
    },
    setBandArea: function(bands, rptTpl, pageStatus) {
        let me = this;
        if (rptTpl[JV.NODE_BAND_COLLECTION]) {
            let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
            let orgArea = JpcCommonHelper.getReportArea(rptTpl, unitFactor);
            for (let i = 0; i < rptTpl[JV.NODE_BAND_COLLECTION].length; i++) {
                me.setBandPos(bands, rptTpl[JV.NODE_BAND_COLLECTION][i], orgArea, unitFactor, pageStatus);
            }
        }
    },
    setBandPos: function(bands, bandNode, orgArea, unitFactor, pageStatus) {
        let me = this, band = bands[bandNode[JV.BAND_PROP_NAME]];
        //1. initialize
        band[JV.PROP_LEFT] = orgArea[JV.IDX_LEFT];
        band[JV.PROP_TOP] = orgArea[JV.IDX_TOP];
        band[JV.PROP_RIGHT] = orgArea[JV.IDX_RIGHT];
        band[JV.PROP_BOTTOM] = orgArea[JV.IDX_BOTTOM];
        //2. set this band
        if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]] == true) {
            switch (band[JV.BAND_PROP_ALIGNMENT]) {
                case JV.LAYOUT_TOP:
                    if (band[JV.PROP_CALCULATION] == JV.CAL_TYPE_ABSTRACT) {
                        band.Bottom = band.Top + unitFactor * band[JV.BAND_PROP_HEIGHT];
                    } else {
                        band.Bottom = band.Top + (band.Bottom - band.Top) * band[JV.BAND_PROP_HEIGHT] / 100;
                    }
                    orgArea[JV.IDX_TOP] = band.Bottom;
                    break;
                case JV.LAYOUT_BOTTOM:
                    if (band[JV.PROP_CALCULATION] == JV.CAL_TYPE_ABSTRACT) {
                        band.Top = band.Bottom - unitFactor * band[JV.BAND_PROP_HEIGHT];
                    } else {
                        band.Top = band.Bottom - (band.Bottom - band.Top) * band[JV.BAND_PROP_HEIGHT] / 100;
                    }
                    orgArea[JV.IDX_BOTTOM] = band.Top;
                    break;
                case JV.LAYOUT_LEFT:
                    if (band[JV.PROP_CALCULATION] == JV.CAL_TYPE_ABSTRACT) {
                        band.Right = band.Left + unitFactor * band[JV.BAND_PROP_WIDTH];
                    } else {
                        band.Right = band.Left + (band.Right - band.Left) * band[JV.BAND_PROP_WIDTH] / 100;
                    }
                    orgArea[JV.IDX_LEFT] = band.Right;
                    break;
                case JV.LAYOUT_RIGHT:
                    if (band[JV.PROP_CALCULATION] == JV.CAL_TYPE_ABSTRACT) {
                        band.Left = band.Right - unitFactor * band[JV.BAND_PROP_WIDTH];
                    } else {
                        band.Left = band.Right - (band.Right - band.Left) * band[JV.BAND_PROP_WIDTH] / 100;
                    }
                    orgArea[JV.IDX_RIGHT] = band.Left;
                    break;
            }
            //3. set sub-bands
            if (bandNode[JV.BAND_PROP_SUB_BANDS]) {
                let bandArea = [band.Left, band.Top, band.Right, band.Bottom];
                for (let i = 0; i < bandNode[JV.BAND_PROP_SUB_BANDS].length; i++) {
                    me.setBandPos(bands, bandNode[JV.BAND_PROP_SUB_BANDS][i], bandArea, unitFactor, pageStatus);
                }
            }
        }
    }
};

module.exports = JpcBandHelper;
