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

let JpcFlowTabHelper = {
    getMaxRowsPerPage: function(bands, rptTpl, isEx) {
        let rst = 1;
        let FLOW_INFO_STR = (!isEx)?JV.NODE_FLOW_INFO:JV.NODE_FLOW_INFO_EX;
        let tab = rptTpl[FLOW_INFO_STR][JV.NODE_FLOW_CONTENT];
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            let maxFieldMeasure = 1.0;
            if (JV.CAL_TYPE_ABSTRACT === JpcCommonHelper.getPosCalculationType(tab[JV.PROP_CALCULATION])) {
                let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
                maxFieldMeasure = 1.0 * rptTpl[FLOW_INFO_STR][JV.NODE_FLOW_CONTENT][JV.PROP_CMN_HEIGHT] * unitFactor;
            } else {
                maxFieldMeasure = (band.Bottom - band.Top) * rptTpl[FLOW_INFO_STR][JV.NODE_FLOW_CONTENT][JV.PROP_CMN_HEIGHT] / JV.HUNDRED_PERCENT;
            }
            rst = Math.floor((band.Bottom - band.Top) / maxFieldMeasure);
        }
        return rst;
    },
    getActualContentAreaHeight: function(bands, rptTpl, segments, page, isEx) {
        let rst = 1;
        let FLOW_INFO_STR = (!isEx)?JV.NODE_FLOW_INFO:JV.NODE_FLOW_INFO_EX;
        let tab = rptTpl[FLOW_INFO_STR][JV.NODE_FLOW_CONTENT];
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            let maxFieldMeasure = 1.0;
            if (JV.CAL_TYPE_ABSTRACT === JpcCommonHelper.getPosCalculationType(tab[JV.PROP_CALCULATION])) {
                let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
                maxFieldMeasure = 1.0 * rptTpl[FLOW_INFO_STR][JV.NODE_FLOW_CONTENT][JV.PROP_CMN_HEIGHT] * unitFactor;
            } else {
                maxFieldMeasure = (band.Bottom - band.Top) * rptTpl[FLOW_INFO_STR][JV.NODE_FLOW_CONTENT][JV.PROP_CMN_HEIGHT] / JV.HUNDRED_PERCENT;
            }
            if (segments.length >= page) {
                rst = segments[page - 1].length * maxFieldMeasure;
            }
        }
        return rst;
    },
    chkSegEnd: function (bands, rptTpl, segmentLength, preRec, pageRecAmt, isEx) {
        let me = this, rst = false;
        let remainAmt = preRec + pageRecAmt - segmentLength;
        rst = me.hasEnoughSpace(rptTpl, bands, remainAmt, isEx);
        return rst;
    },
    hasEnoughSpace: function (rptTpl, bands, remainAmt, isEx) {
        if (remainAmt < 0) return false;
        let rst = true, measurement = 1.0, douDiffForCompare = 0.00001;
        let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
        let FLOW_INFO_STR = (!isEx)?JV.NODE_FLOW_INFO:JV.NODE_FLOW_INFO_EX;
        let tab = rptTpl[FLOW_INFO_STR][JV.NODE_FLOW_CONTENT];
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band !== null && band !== undefined) {
            measurement = 1.0 * tab[JV.PROP_CMN_HEIGHT] * unitFactor;
            let spareHeight = measurement * remainAmt;
            let douH = band.Bottom - band.Top;
            rst = ((spareHeight >= douH) || ((spareHeight - douH) <= douDiffForCompare));
        }
        return rst;
    }
};

module.exports = JpcFlowTabHelper;