'use strict';

import { IPositionProps } from '../../index';
import { IArea, IControlCollection, IControlSubCollection, IRptTpl, IStyles } from '../../interface/basic';
import { JV } from '../jpc_value_define';

const RESOLUTION = [96, 96];

export const JpcCommonHelper = {
    getResultByID(KeyID: string, collectionList: IStyles[][] | IControlSubCollection[][]) {
        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].ID === KeyID) {
                            rst = collection[j];
                            break;
                        }
                    }
                    if (rst) break;
                }
            }
        }
        return rst;
    },
    //by lish ,多余参数，
    // getFont(fontName: string, dftFonts: any[], rptTpl: IRptTpl) {
    getFont(fontName: string, dftFonts: any[]) {    //暂定
        let me = this, list = [];
        // if (rptTpl) list.push(rptTpl.control_collection); //by lish ,多余参数，
        list.push(dftFonts);
        return me.getResultByID(fontName, list);
    },
    //by lish ,多余参数，
    // getStyle(styleName: string, dftStyles: IStyles[], rptTpl?: IRptTpl) {
    getStyle(styleName: string, dftStyles: IStyles[]) {
        let me = this, list = [];
        // if (rptTpl) list.push(rptTpl.control_collection);   //by lish ,多余参数，
        list.push(dftStyles);
        return me.getResultByID(styleName, list);
    },
    getControl(controlName: string, dftControls: IControlSubCollection[], rptTpl?: IRptTpl) {
        let me = this, list: IControlSubCollection[][] = [];
        //  list.push(rptTpl.control_collection);
        list.push(dftControls);
        return me.getResultByID(controlName, list);
    },
    getLayoutAlignment(alignStr: string) {
        let rst = JV.LAYOUT.indexOf(alignStr);
        if (rst < 0) rst = 4; //JV.LAYOUT_FULFILL;
        return rst;
    },
    getPosCalculationType(typeStr: string) {
        let rst = JV.CAL_TYPE.indexOf(typeStr);
        if (rst < 0) rst = 1;// JV.CAL_TYPE_ABSTRACT;
        return rst;
    },
    getBoolean(bStr: any) {  //暂定
        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') as boolean;;
            } else if (valType === 'number') {
                rst = (bStr === 1);
            }
        }
        return rst;
    },
    getScreenDPI() {
        return RESOLUTION.slice(0);
    },
    getUnitFactor(rptTpl: IRptTpl) {
        let me = this;
        return me.translateUnit(rptTpl.主信息.单位);
    },
    translateUnit(unitStr: string) {
        let me = this, rst = 1.0;
        if (unitStr) {
            let sdpi = me.getScreenDPI();
            if (JV.MEASUREMENT.PIXEL.indexOf(unitStr) >= 0) {
                rst = 1.0;
            } else if (JV.MEASUREMENT.CM.indexOf(unitStr) >= 0) {
                rst = 1.0 * sdpi[0] / 2.54;
            } else if (JV.MEASUREMENT.INCH.indexOf(unitStr) >= 0) {
                rst = 1.0 * sdpi[0];
            }
        }
        return rst;
    },
    getPageSize(rptTpl: IRptTpl) {
        let size = JV.SIZE_A4.slice(0);
        let sizeStr = rptTpl.主信息.打印页面_信息.页规格;
        let sizeIdx = JV.PAGES_SIZE_STR.indexOf(sizeStr);
        if (sizeIdx >= 0) {
            size = JV.PAGES_SIZE[sizeIdx].slice(0);
        } else if (sizeStr === '自定义') {
            //自定义规格，如实际业务需要则后期补充
        }
        let page_orientation = rptTpl.主信息.打印页面_信息.方向;
        if (page_orientation === 'LANDSCAPE' || page_orientation === '横向') {
            //swap x,y
            let tmp = size[0];
            size[0] = size[1];
            size[1] = tmp;
        }
        return size;
    },
    getReportArea(rptTpl: IRptTpl, unitFactor: number) {
        let me = this, sdpi = me.getScreenDPI(), rst = [], size = me.getPageSize(rptTpl);
        size[0] = sdpi[0] * size[0];
        size[1] = sdpi[0] * size[1];
        rst.push(unitFactor * rptTpl.主信息.页边距.Left);
        rst.push(unitFactor * rptTpl.主信息.页边距.Top);
        rst.push(size[0] - unitFactor * rptTpl.主信息.页边距.Right);
        rst.push(size[1] - unitFactor * rptTpl.主信息.页边距.Bottom);
        return rst;
    },
    getSegIdxByPageIdx(page: number, page_seg_map: number[][]) {
        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;
    },
    getStringLinesInArea(area: IArea, strVal: string, chnW: number, otherW: number) {
        //备注： 因后台的pdf kit判断字符串长度与前端的不一样，需要做些调整，不一次性地判断字符串长度。
        //      分2种字符：中文与非中文，按照各种字符的数量分别乘以相关一个字符的宽度再累计。
        //      另判断行数还不能直接用总长度除以宽度来计算，因每一行都会有不同的余量，所以得一行行走过来判断。
        let rst = 0;
        if (strVal) {
            let areaWidth = area.Right - area.Left - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT] - 1;
            let txtWidth = 0, currentW = 0;
            // 这里是一个一个字获取宽度，然后在对比区域宽度，算出最后有多少行
            for (let sIdx = 0; sIdx < strVal.length; sIdx++) {
                //大于127为中文字
                currentW = (strVal.charCodeAt(sIdx) > 127) ? chnW : otherW;
                txtWidth += currentW;
                if (txtWidth > (areaWidth - 4)) {
                    //减4个像素是考虑到导出excel的情况
                    rst++;
                    txtWidth = currentW;
                }
                if (sIdx === strVal.length - 1) {
                    rst++;
                }
            }
        }
        if (rst === 0) rst = 1; //即使是空字符串，也得有一行啊
        return rst;
    },
    splitString(area: IArea, strVal: string, chnW: number, otherW: number) {
        let rst = [];
        if (strVal) {
            let areaWidth = area.Right - area.Left - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_RIGHT] - JV.OUTPUT_OFFSET[JV.OFFSET_IDX_LEFT] - 1;
            let preSIdx = 0, txtWidth = 0;
            let currentW = 0;
            for (let sIdx = 0; sIdx < strVal.length; sIdx++) {
                currentW = (strVal.charCodeAt(sIdx) > 127) ? chnW : otherW;
                txtWidth += currentW;
                if (txtWidth > (areaWidth - 4)) {
                    //减4个像素是考虑到导出excel的情况
                    if (preSIdx < sIdx) {
                        rst.push(strVal.substr(preSIdx, sIdx - preSIdx));
                        preSIdx = sIdx;
                    } else {
                        rst.push(strVal.substr(preSIdx, 1));
                        preSIdx = sIdx + 1;
                    }
                    txtWidth = currentW;
                }
                if (sIdx === strVal.length - 1) {
                    rst.push(strVal.substr(preSIdx, strVal.length - preSIdx));
                }
            }
        }
        if (rst.length === 0) rst.push(''); //什么都没有，也得整个空串
        return rst;
    }
};

