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

var JpcCommonHelper = {
    commonConstant: {},
    getResultByID: function (KeyID, collectionList) {
        var rst = null;
        if (KeyID) {
            for (var i = 0; i < collectionList.length; i++) {
                var collection = collectionList[i];
                if (collection && collection instanceof Array) {
                    for (var j = 0; j < collection.length; j++) {
                        if (collection[j][JV.PROP_ID] === KeyID) {
                            rst = collection[j];
                            break;
                        }
                    }
                    if (rst) break;
                }
            }
        }
        return rst;
    },
    getFont: function(fontName, dftFonts, rptTpl) {
        var me = this, rst = null, list = [];
        if (rptTpl) list.push(rptTpl[JV.NODE_FONT_COLLECTION]);
        list.push(dftFonts);
        rst = me.getResultByID(fontName, list);
        return rst;
    },
    getStyle: function(styleName, dftStyles, rptTpl) {
        var me = this, rst = null, list = [];
        if (rptTpl) list.push(rptTpl[JV.NODE_STYLE_COLLECTION]);
        list.push(dftStyles);
        rst = me.getResultByID(styleName, list);
        return rst;
    },
    getControl: function(controlName, dftControls, rptTpl) {
        var me = this, rst = null, list = [];
        if (rptTpl) list.push(rptTpl[JV.NODE_CONTROL_COLLECTION]);
        list.push(dftControls);
        rst = me.getResultByID(controlName, list);
        return rst;
    },
    getLayoutAlignment: function(alignStr) {
        var rst = JV.LAYOUT.indexOf(alignStr);
        if (rst < 0) rst = JV.LAYOUT_FULFILL;
        return rst;
    },
    getPosCalculationType: function (typeStr) {
        var rst = JV.CAL_TYPE.indexOf(typeStr);
        if (rst < 0) rst = JV.CAL_TYPE_ABSTRACT;
        return rst;
    },
    getScreenDPI: function() {
        var me = this, arrDPI = [];
        if (!me.commonConstant.resolution) {
            arrDPI = [96,96];
            me.commonConstant.resolution = arrDPI;
        } else {
            arrDPI = me.commonConstant.resolution;
        }
        return arrDPI;
    },
    getScreenDPI_bk: function() {
        var me = this, arrDPI = [];
        if (!me.commonConstant.resolution) {
            if (window) {
                if (window.screen.deviceXDPI != undefined) {
                    arrDPI.push(window.screen.deviceXDPI);
                    arrDPI.push(window.screen.deviceYDPI);
                } else {
                    var tmpNode = document.createElement("DIV");
                    tmpNode.style.cssText = "width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden";
                    document.body.appendChild(tmpNode);
                    arrDPI.push(parseInt(tmpNode.offsetWidth));
                    arrDPI.push(parseInt(tmpNode.offsetHeight));
                    tmpNode.parentNode.removeChild(tmpNode);
                }
            } else {
                arrDPI = [96,96];
            }
            me.commonConstant.resolution = arrDPI;
        } else {
            arrDPI = me.commonConstant.resolution;
        }
        return arrDPI;
    },
    getUnitFactor: function(rptTpl) {
        var me = this;
        return me.translateUnit(rptTpl[JV.NODE_MAIN_INFO][JV.PROP_UNITS]);
    },
    translateUnit: function(unitStr) {
        var me = this, rst = 1.0;
        if (unitStr) {
            var resolution = me.getScreenDPI();
            if (JV.MEASUREMENT.PIXEL.indexOf(unitStr) >= 0) {
                rst = 1.0;
            } else if (JV.MEASUREMENT.CM.indexOf(unitStr) >= 0) {
                rst = 1.0 * resolution[0] / 2.54;
            } else if (JV.MEASUREMENT.INCH.indexOf(unitStr) >= 0) {
                rst = 1.0 * resolution[0];
            }
        }
        return rst;
    },
    getPageSize: function (rptTpl) {
        var me = this, size = null;
        var sizeStr = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE];
        var sizeIdx = JV.PAGES_SIZE_STR.indexOf(sizeStr);
        if (sizeIdx >= 0) {
            size = JV.PAGES_SIZE[sizeIdx].slice(0);
        } else if (sizeStr === JV.PAGE_SELF_DEFINE) {
            //
        } else {
            size = JV.SIZE_A4.slice(0);
        }
        var page_orientation = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION];
        if (page_orientation === JV.ORIENTATION_LANDSCAPE || page_orientation === JV.ORIENTATION_LANDSCAPE_CHN) {
            //swap x,y
            var tmp = size[0];
            size[0] = size[1];
            size[1] = tmp;
        }
        return size;
    },
    getReportArea: function(rptTpl, unitFactor) {
        var me = this, resolution = me.getScreenDPI(), rst = [], size = me.getPageSize(rptTpl);
        size[0] = resolution[0] * size[0];
        size[1] = resolution[0] * size[1];
        rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]);
        rst.push(unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP]);
        rst.push(size[0] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT]);
        rst.push(size[1] - unitFactor * rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM]);
        return rst;
    },
    getSegIdxByPageIdx: function(page, page_seg_map) {
        var rst = -1;
        for (var pIdx = 0; pIdx < page_seg_map.length; pIdx++) {
            if (page_seg_map[pIdx][0] == page) {
                rst = page_seg_map[pIdx][1];
                break;
            }
        }
        return rst;
    }
};

module.exports = JpcCommonHelper;