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

let JpcCommonHelper = {
    commonConstant: {},
    getResultByID: function (KeyID, collectionList) {
        let rst = null;
        if (KeyID) {
            for (let i = 0; i < collectionList.length; i++) {
                let collection = collectionList[i];
                if (collection && collection instanceof Array) {
                    for (let 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) {
        let 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) {
        let 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) {
        let 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) {
        let rst = JV.LAYOUT.indexOf(alignStr);
        if (rst < 0) rst = JV.LAYOUT_FULFILL;
        return rst;
    },
    getPosCalculationType: function (typeStr) {
        let rst = JV.CAL_TYPE.indexOf(typeStr);
        if (rst < 0) rst = JV.CAL_TYPE_ABSTRACT;
        return rst;
    },
    getBoolean: function(bStr) {
        let rst = false;
        if (bStr !== null && bStr !== undefined) {
            let valType = typeof(bStr);
            if (valType === "boolean") {
                rst = bStr;
            } else if (valType === "string") {
                let tS = bStr.toUpperCase();
                rst = (tS === "T" || tS === "TRUE" || tS === "YES" || tS === "Y");
            } else if (valType === "number") {
                rst = (bStr === 1);
            }
        }
        return rst;
    },
    getScreenDPI: function() {
        let me = this, arrDPI = [];
        if (!me.commonConstant.resolution) {
            arrDPI = [96,96];
            me.commonConstant.resolution = arrDPI;
        } else {
            arrDPI = me.commonConstant.resolution;
        }
        return arrDPI;
    },
    getScreenDPI_bk: function() {
        let 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 {
                    let 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) {
        let me = this;
        return me.translateUnit(rptTpl[JV.NODE_MAIN_INFO][JV.PROP_UNITS]);
    },
    translateUnit: function(unitStr) {
        let me = this, rst = 1.0;
        if (unitStr) {
            let 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) {
        let me = this, size = null;
        let sizeStr = rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE];
        let 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);
        }
        let 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
            let tmp = size[0];
            size[0] = size[1];
            size[1] = tmp;
        }
        return size;
    },
    getReportArea: function(rptTpl, unitFactor) {
        let 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) {
        let rst = -1;
        for (let 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;