'use strict';

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

export let JpcFlowTabHelper = {
    getRowHeight: function(bands:IBands, rptTpl:IRptTpl, isEx:boolean) {
        let rst = 0;
        let FLOW_INFO_STR = (!isEx)?JV.NODE_FLOW_INFO:JV.NODE_FLOW_INFO_EX;
        let tab = rptTpl.流水式表_信息.流水式表_数据;
        let band = bands[tab.BandName];
        if (band) {
            let maxFieldMeasure = 1.0;
            if (JV.CAL_TYPE_ABSTRACT === JpcCommonHelper.getPosCalculationType(tab.CalculationType)) {
                let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
                maxFieldMeasure = 1.0 * rptTpl.流水式表_信息.流水式表_数据.CommonHeight * unitFactor;
            } else {
                maxFieldMeasure = (band.Bottom - band.Top) * rptTpl.流水式表_信息.流水式表_数据.CommonHeight / JV.HUNDRED_PERCENT;
            }
            rst = maxFieldMeasure;
        }
        return rst;
    },
    getMaxRowsPerPage: function (bands: IBands, rptTpl: IRptTpl, isEx: boolean) {
        let rst = 1;
        let tab;
        (!isEx) ? tab = rptTpl.流水式表_信息.流水式表_数据 : tab = rptTpl.流水式表_拓展信息.流水式表_数据;
        let band = bands[tab.BandName];
        if (band) {
            let maxFieldMeasure = 1.0;
            if (JV.CAL_TYPE_ABSTRACT === JpcCommonHelper.getPosCalculationType(tab.CalculationType)) {
                let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
                maxFieldMeasure = 1.0 * tab.CommonHeight * unitFactor;
            } else {
                maxFieldMeasure = (band.Bottom - band.Top) * tab.CommonHeight / JV.HUNDRED_PERCENT;
            }
            rst = Math.floor((band.Bottom - band.Top) / maxFieldMeasure);
        }
        return rst;
    },
    getActualContentAreaHeight: function (bands: IBands, rptTpl: IRptTpl, segments: number[][], rowAmt: number, page: number, isEx: boolean) {
        let rst = 1;
        let tab;
        (!isEx) ? tab = rptTpl.流水式表_信息.流水式表_数据 : tab = rptTpl.流水式表_拓展信息.流水式表_数据;
        let band = bands[tab.BandName];
        if (band) {
            let maxFieldMeasure = 1.0;
            if (JV.CAL_TYPE_ABSTRACT === JpcCommonHelper.getPosCalculationType(tab.CalculationType)) {
                let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
                maxFieldMeasure = 1.0 * tab.CommonHeight * unitFactor;
            } else {
                maxFieldMeasure = (band.Bottom - band.Top) * tab.CommonHeight / JV.HUNDRED_PERCENT;
            }
            if (segments.length >= page) {
                rst = rowAmt * maxFieldMeasure;
            }
        }
        return rst;
    },
    chkSegEnd: function (bands: IBands, rptTpl: IRptTpl, segmentLength: number, preRec: number, pageRecAmt: number, isEx: boolean) {
        let me = this, rst = false;
        let remainAmt = preRec + pageRecAmt - segmentLength;
        rst = me.hasEnoughSpace(rptTpl, bands, remainAmt, isEx);
        return rst;
    },
    hasEnoughSpace: function (rptTpl: IRptTpl, bands: IBands, remainAmt: number, isEx: boolean) {
        if (remainAmt < 0) return false;
        let rst = true, measurement = 1.0, douDiffForCompare = 0.00001;
        let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
        let FLOW_INFO_STR = (!isEx) ? '流水式表_信息' : '流水式表_拓展信息';
        let tab;
        (!isEx) ? tab = rptTpl.流水式表_信息.流水式表_数据 : tab = rptTpl.流水式表_拓展信息.流水式表_数据;
        let band = bands[tab.BandName];
        if (band !== null && band !== undefined) {
            measurement = 1.0 * tab.CommonHeight * unitFactor;
            let spareHeight = measurement * remainAmt;
            let douH = band.Bottom - band.Top;
            rst = ((spareHeight >= douH) || ((spareHeight - douH) <= douDiffForCompare));
        }
        return rst;
    }
};

