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

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

module.exports = JpcFlowTabHelper;