'use strict';

import { IBandDetail, IBands, IOrgBandDetail, IRptTpl } from '../../interface/basic';
import { JV } from '../jpc_value_define';
import { JpcCommonHelper } from './jpc_helper_common';

export const JpcBandHelper = {
    getBandTypeValByString: function (bandType: string) {
        let rst = JV.PAGE_STATUS.indexOf(bandType);
        if (rst < 0) rst = JV.STATUS_NORMAL;
        return rst;
    },
    //
    setBandArea: function (bands: IBands, rptTpl: IRptTpl, pageStatus: boolean[], isOnlyNormalStatus: boolean, isOnlyExStatus: boolean) {
        let me = this;
        if (rptTpl.布局框_集合) {
            isOnlyNormalStatus = isOnlyNormalStatus || false;
            isOnlyExStatus = isOnlyExStatus || false;
            // 把模板中的单位转化为px数值
            let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
            // 用上面求出的单位重新计算位置
            let orgArea = JpcCommonHelper.getReportArea(rptTpl, unitFactor);
            for (let i = 0; i < rptTpl.布局框_集合.length; i++) {
                me.setBandPos(bands, rptTpl.布局框_集合[i], orgArea, unitFactor, pageStatus, isOnlyNormalStatus, isOnlyExStatus);
            }
        }
    },
    setBandPos: function (bands: IBands, bandNode: IOrgBandDetail, orgArea: number[], unitFactor: number, pageStatus: boolean[], isOnlyNormalStatus: boolean, isOnlyExStatus: boolean) {
        let me = this, band = bands[bandNode.Name];
        //0. for multi flow purpose
        if (isOnlyNormalStatus) {    //？？
            if (bandNode.exOnly && JpcCommonHelper.getBoolean(bandNode.exOnly)) {
                return;
            }
        }
        if (isOnlyExStatus) {    //？？
            if (bandNode.normalOnly && !(JpcCommonHelper.getBoolean(bandNode.normalOnly))) {
                return;
            }
        }
        //1. initialize
        band.Left = orgArea[0];
        band.Top = orgArea[1];
        band.Right = orgArea[2];
        band.Bottom = orgArea[3];
        //2. set this band
        if (pageStatus[(band.DisplayType) as number]) {
            switch (band.Alignment) {
                case JV.LAYOUT_TOP:
                    if (band.CalculationType === JV.CAL_TYPE_ABSTRACT) {
                        band.Bottom = band.Top + unitFactor * band.Height;
                    } else {
                        band.Bottom = band.Top + (band.Bottom - band.Top) * band.Height / 100;
                    }
                    orgArea[JV.IDX_TOP] = band.Bottom;
                    break;
                case JV.LAYOUT_BOTTOM:
                    if (band.CalculationType === JV.CAL_TYPE_ABSTRACT) {
                        band.Top = band.Bottom - unitFactor * band.Height;
                    } else {
                        band.Top = band.Bottom - (band.Bottom - band.Top) * band.Height / 100;
                    }
                    orgArea[JV.IDX_BOTTOM] = band.Top;
                    break;
                case JV.LAYOUT_LEFT:
                    if (band.CalculationType === JV.CAL_TYPE_ABSTRACT) {
                        band.Right = band.Left + unitFactor * band.Width;
                    } else {
                        band.Right = band.Left + (band.Right - band.Left) * band.Width / 100;
                    }
                    orgArea[JV.IDX_LEFT] = band.Right;
                    break;
                case JV.LAYOUT_RIGHT:
                    if (band.CalculationType === JV.CAL_TYPE_ABSTRACT) {
                        band.Left = band.Right - unitFactor * band.Width;
                    } else {
                        band.Left = band.Right - (band.Right - band.Left) * band.Width / 100;
                    }
                    orgArea[JV.IDX_RIGHT] = band.Left;
                    break;
            }
            //3. set sub-bands
            if (bandNode.band_s) {
                let bandArea = [band.Left, band.Top, band.Right, band.Bottom];
                for (let i = 0; i < bandNode.band_s.length; i++) {
                    me.setBandPos(bands, bandNode.band_s[i], bandArea, unitFactor, pageStatus, isOnlyNormalStatus, isOnlyExStatus);
                }
            }
        }
    },
    resetBandPos: function (bandCollection: IOrgBandDetail[], bands: IBands, contentBand: IBandDetail, offsetX: number, offsetY: number) {
        let orgX = contentBand.Right, orgY = contentBand.Bottom;
        function _chkAndResetPos(targetBand: IOrgBandDetail) {
            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.Le && band.Right >= orgX && band.Le < orgX) {
                        band.Right += offsetX;
                    }
                }
                if (targetBand.band_s) {
                    for (let i = 0; i < targetBand.band_s.length; i++) {
                        _chkAndResetPos(targetBand.band_s[i]);
                    }
                }
            }
        }
        for (let i = 0; i < bandCollection.length; i++) {
            _chkAndResetPos(bandCollection[i]);
        }
    }
};
