'use strict';

import { IPositionProps } from '../../index';
import { IArea, IBandDetail } from '../../interface/basic';
import { JV } from '../jpc_value_define';

export const JpcAreaHelper = {
    outputArea: function (areaNode: IArea, band: IBandDetail, unitFactor: number, rowAmount: number, rowIdx: number, colAmount: number, colIdx: number,
        multipleDispCol: number, multipleColIdx: number, syncHeight: boolean, syncWidth: boolean) {
        let rst = {
            Left: 0,
            Right: 0,
            Top: 0,
            Bottom: 0,
        },
            maxMultiColumns = 3;
        if (multipleDispCol > 0 && multipleDispCol <= maxMultiColumns) {
            //1. calculate left/right
            let areaWidth = 1.0 * (band.Right - band.Left) / multipleDispCol;
            areaWidth = areaWidth / colAmount;
            let innerLeft = 0.0, innerRight = areaWidth;
            //
            if (typeof areaNode.H_CalculationType === "string") {
                switch (areaNode.H_CalculationType) {
                    case JV.CAL_TYPE[JV.CAL_TYPE_PERCENTAGE]:
                        innerLeft = (1.0 * areaNode.Left * areaWidth / JV.HUNDRED_PERCENT);
                        innerRight = (1.0 * areaNode.Right * areaWidth / JV.HUNDRED_PERCENT);
                        break;
                    case JV.CAL_TYPE[JV.CAL_TYPE_ABSTRACT]:
                        innerLeft = 1.0 * areaNode.Left * unitFactor;
                        innerRight = 1.0 * areaNode.Right * unitFactor;
                        break;
                }
            } else {
                //颗粒度更加细化的控制，可能左右两边的计算坐标方式都不同
                if (areaNode.H_CalculationType && areaNode.H_CalculationType.Left === JV.CAL_TYPE[JV.CAL_TYPE_PERCENTAGE]) {
                    innerLeft = (1.0 * areaNode.Left * areaWidth / JV.HUNDRED_PERCENT);
                } else {
                    innerLeft = 1.0 * areaNode.Left * unitFactor;
                }
                if (areaNode.H_CalculationType && areaNode.H_CalculationType.Right === JV.CAL_TYPE[JV.CAL_TYPE_PERCENTAGE]) {
                    innerRight = (1.0 * areaNode.Right * areaWidth / JV.HUNDRED_PERCENT);
                } else {
                    innerRight = 1.0 * areaNode.Right * unitFactor;
                }
            }
            //2. calculate top/bottom
            let areaHeight = 1.0 * (band.Bottom - band.Top);
            areaHeight = areaHeight / rowAmount;
            let innerTop = 0.0, innerBottom = areaHeight;
            if (typeof areaNode.V_CalculationType === "string") {
                switch (areaNode.V_CalculationType) {
                    case JV.CAL_TYPE[JV.CAL_TYPE_PERCENTAGE]:
                        innerTop = (1.0 * areaNode.Top * areaHeight / JV.HUNDRED_PERCENT);
                        innerBottom = (1.0 * areaNode.Bottom * areaHeight / JV.HUNDRED_PERCENT);
                        break;
                    case JV.CAL_TYPE[JV.CAL_TYPE_ABSTRACT]:
                        innerTop = 1.0 * areaNode.Top * unitFactor;
                        innerBottom = 1.0 * areaNode.Bottom * unitFactor;
                        break;
                }
            } else {
                //颗粒度更加细化的控制，可能上下两边的计算坐标方式都不同
                if (areaNode.V_CalculationType && areaNode.V_CalculationType.Top === JV.CAL_TYPE[JV.CAL_TYPE_PERCENTAGE]) {
                    innerTop = (1.0 * areaNode.Top * areaHeight / JV.HUNDRED_PERCENT);
                } else {
                    innerTop = 1.0 * areaNode.Top * unitFactor;
                }
                if (areaNode.V_CalculationType && areaNode.V_CalculationType.Bottom === JV.CAL_TYPE[JV.CAL_TYPE_PERCENTAGE]) {
                    innerBottom = (1.0 * areaNode.Bottom * areaHeight / JV.HUNDRED_PERCENT);
                } else {
                    innerBottom = 1.0 * areaNode.Bottom * unitFactor;
                }
            }
            //
            let rstLeft = 0.0, rstRight = 0.0, rstTop = 0.0, rstBottom = 0.0;
            if (syncHeight) {
                rstBottom = Math.round(1.0 * band.Top + areaHeight * (rowIdx + 1) + innerTop);
            } else {
                rstBottom = Math.round(1.0 * band.Top + areaHeight * rowIdx + innerBottom);
            }
            if (syncWidth) {
                rstRight = Math.round(1.0 * band.Left + areaWidth * (colIdx + 1) + innerLeft + multipleColIdx * areaWidth);
            } else {
                rstRight = Math.round(1.0 * band.Left + areaWidth * colIdx + innerRight + multipleColIdx * areaWidth);
            }
            rstLeft = Math.round(1.0 * band.Left + areaWidth * colIdx + innerLeft + multipleColIdx * areaWidth);
            rstTop = Math.round(1.0 * band.Top + areaHeight * rowIdx + innerTop);
            rst.Left = rstLeft;
            rst.Right = rstRight;
            rst.Top = rstTop;
            rst.Bottom = rstBottom;
        }
        return rst;
    }
};
