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, isOnlyNormalStatus, isOnlyExStatus) {
        let me = this;
        if (rptTpl[JV.NODE_BAND_COLLECTION]) {
            isOnlyNormalStatus = isOnlyNormalStatus||false;
            isOnlyExStatus = isOnlyExStatus||false;

            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, isOnlyNormalStatus, isOnlyExStatus);
            }
        }
    },
    setBandPos: function(bands, bandNode, orgArea, unitFactor, pageStatus, isOnlyNormalStatus, isOnlyExStatus) {
        let me = this, band = bands[bandNode[JV.BAND_PROP_NAME]];
        //0. for multi flow purpose
        if (isOnlyNormalStatus) {
            if (bandNode.hasOwnProperty(JV.PROP_BAND_EX_ONLY) && JpcCommonHelper.getBoolean(bandNode[JV.PROP_BAND_EX_ONLY])) {
                return;
            }
        }
        if (isOnlyExStatus) {
            if (bandNode.hasOwnProperty(JV.PROP_BAND_NORMAL_ONLY) && !(JpcCommonHelper.getBoolean(bandNode[JV.PROP_BAND_NORMAL_ONLY]))) {
                return;
            }
        }
        //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]]) {
            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, isOnlyNormalStatus, isOnlyExStatus);
                }
            }
        }
    },
    resetBandPos: function (bandCollection, bands, contentBand, offsetX, offsetY) {
        let orgX = contentBand.Right, orgY = contentBand.Bottom;
        function chkAndResetPos(targetBand) {
            let band = bands[targetBand.Name];
            if (band) {
                if (band === contentBand) {
                    band.Bottom += offsetY;
                    band.Right += offsetX;
                } else {
                    if (band.Top >= orgY) {
                        band.Top += offsetY;
                        band.Bottom += offsetY;
                    } else if (band.Bottom >= orgY && band.Top < orgY) {
                        band.Bottom += offsetY;
                    }
                    if (band.Left >= orgX) {
                        band.Left += offsetX;
                        band.Right += offsetX;
                    } else if (band.Right >= orgX && band.Le < orgX) {
                        band.Right += offsetX;
                    }
                }
                if (targetBand[JV.BAND_PROP_SUB_BANDS]) {
                    for (let i = 0; i < targetBand[JV.BAND_PROP_SUB_BANDS].length; i++) {
                        chkAndResetPos(targetBand[JV.BAND_PROP_SUB_BANDS][i]);
                    }
                }
            }
        }
        for (let i = 0; i < bandCollection.length; i++) {
            chkAndResetPos(bandCollection[i]);
        }
    }
};

module.exports = JpcBandHelper;
