
class Util {
    /**
     * 时间相关接口
     */
    private static dateRange = [];

    public static getNowDate() {
        return this.dateFtt(new Date(), "yyyy-MM-dd hh:mm:ss");
    }

    public static getWeekTime() {
        let now = new Date();
        let y = now.getFullYear();
        let m = now.getMonth();
        let d = now.getDate();
        let day = now.getDay();
        let weekStart = new Date(y, m, d - (day ? (day - 1) : 6));
        let weekEnd = new Date(y, m, d + (day ? (8 - day) : 1))
        Util.dateRange = [this.dateFtt(weekStart), this.dateFtt(weekEnd)];
        return Util.dateRange
    }

    public static isInTimeRange(timeStr) {
        if (timeStr) {
            let recordTime = new Date(timeStr.replace(/-/g, '/')).getTime();
            return recordTime > this.dateRange[0] && recordTime < this.dateRange[1];
        } else {
            return false;
        }

    }

    public static transObj(kvData) {
        let res = {};
        kvData.map((data) => {
            res[data.key] = data.value;
        })
        return res;
    }
    /**
     * 时间格式处理
     * @param {Date} data 时间
     * @param {} fmt 格式化格式
     */
    public static dateFtt(date, fmt?) {
        var o = {
            "M+": date.getMonth() + 1,                     //月份   
            "d+": date.getDate(),                          //日   
            "h+": date.getHours(),                         //小时   
            "m+": date.getMinutes(),                       //分   
            "s+": date.getSeconds(),                       //秒   
            "q+": Math.floor((date.getMonth() + 3) / 3),   //季度   
            "S": date.getMilliseconds()                    //毫秒   
        };
        fmt || (fmt = "yyyy-MM-ddThh:mm:ss+00:00");
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    /**
     * egret引擎常用函数调用，包括创建bitmap，text，shape等
     */
    /**
     * 根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
     * @param  {string} name
     * @param  {Object} settings?
     */
    public static createSprite(settings?: Object) {
        let result = new egret.Sprite();
        if (settings) {
            for (let key in settings) {
                result[key] = settings[key];
            }
        }
        return result;
    }

    /**
     * 根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
     * @param  {string} name
     * @param  {Object} settings?
     */
    public static createBitmapByName(name: string, settings?: Object): egret.Bitmap {
        let result = new egret.Bitmap();
        let texture: egret.Texture = RES.getRes(name);
        result.texture = texture;
        if (settings) {
            for (let key in settings) {
                result[key] = settings[key];
            }
        }
        return result;
    }

    /**
     * 根据text创建TextField对象
     * @param  {string} text
     * @param  {Object} settings?
     */
    public static createText(text: string, settings?: Object): egret.TextField {
        let result = new egret.TextField();
        result.text = text;
        if (settings) {
            for (let key in settings) {
                result[key] = settings[key];
            }
        }
        return result;
    }


    /**
     * 异步根据URL获取头像
     * @param  {any=''} url
     * @param  {Object} settings?
     */
    public static async createAvatar(url: any = '', settings?: Object) {
        return new Promise((resolve, reject) => {
            if (!url) { reject('无头像') }
            RES.getResByUrl(url, function (evt: any) {
                let textTure: egret.Texture = <egret.Texture>evt;
                let bitmap: egret.Bitmap = new egret.Bitmap(textTure);
                if (settings) {
                    for (let key in settings) {
                        bitmap[key] = settings[key];
                    }
                }
                resolve(bitmap);
            }, this, RES.ResourceItem.TYPE_IMAGE)
        })
    }


    /**
     * 根据参数绘制直线段
     * @param  {Array<any>} points
     * @param  {{beginFill?:{color:number} param
     * @param  {number}} alpha
     * @param  {{thickness?:number} lineStyle?
     * @param  {number} color?
     * @param  {number} alpha?
     * @param  {boolean} pixelHinting?
     * @param  {string} scaleMode?
     * @param  {string} caps?
     * @param  {string} joints?
     * @param  {number}}} miterLimit?
     * @param  {Object} settings?
     * @returns egret
     */
    public static createLine(points: Array<any>, param: { beginFill?: { color: number, alpha: number }, lineStyle?: { thickness?: number, color?: number, alpha?: number, pixelHinting?: boolean, scaleMode?: string, caps?: string, joints?: string, miterLimit?: number } }, settings?: Object): egret.Shape {
        let shp = new egret.Shape();
        if (param.beginFill) {
            shp.graphics.beginFill(param.beginFill.color, param.beginFill.alpha);
        }
        if (param.lineStyle) {
            shp.graphics.lineStyle(param.lineStyle.thickness, param.lineStyle.color, param.lineStyle.alpha, param.lineStyle.pixelHinting, param.lineStyle.scaleMode, param.lineStyle.caps, param.lineStyle.joints, param
                .lineStyle.miterLimit);
        }
        shp.graphics.moveTo(points[0][0], points[0][1]);
        points.map((point, index) => {
            index > 0 && shp.graphics.lineTo(points[index][0], points[index][1]);
        });
        shp.graphics.endFill();
        if (settings) {
            for (let key in settings) {
                shp[key] = settings[key];
            }
        }
        return shp;
    }


    /**
     * 根据参数绘制矩形
     * @param  {Array<any>} points
     * @param  {{beginFill?:{color:number} param
     * @param  {number}} alpha
     * @param  {{thickness?:number} lineStyle?
     * @param  {number} color?
     * @param  {number} alpha?
     * @param  {boolean} pixelHinting?
     * @param  {string} scaleMode?
     * @param  {string} caps?
     * @param  {string} joints?
     * @param  {number}}} miterLimit?
     * @param  {Object} settings?
     * @returns egret
     */
    public static createRect(points: Array<any>, param: { beginFill?: { color: number, alpha: number }, lineStyle?: { thickness?: number, color?: number, alpha?: number, pixelHinting?: boolean, scaleMode?: string, caps?: string, joints?: string, miterLimit?: number } }, settings?: Object): egret.Shape {
        let shp = new egret.Shape();
        if (param.beginFill) {
            shp.graphics.beginFill(param.beginFill.color, param.beginFill.alpha);
        }
        if (param.lineStyle) {
            shp.graphics.lineStyle(param.lineStyle.thickness, param.lineStyle.color, param.lineStyle.alpha, param.lineStyle.pixelHinting, param.lineStyle.scaleMode, param.lineStyle.caps, param.lineStyle.joints, param
                .lineStyle.miterLimit);
        }
        shp.graphics.drawRect(points[0], points[1], points[2], points[3]);
        if (settings) {
            for (let key in settings) {
                shp[key] = settings[key];
            }
        }
        return shp;
    }

    /**
     * 根据参数绘制圆形
     * @param  {Array<any>} points
     * @param  {{beginFill?:{color:number} param
     * @param  {number}} alpha
     * @param  {{thickness?:number} lineStyle?
     * @param  {number} color?
     * @param  {number} alpha?
     * @param  {boolean} pixelHinting?
     * @param  {string} scaleMode?
     * @param  {string} caps?
     * @param  {string} joints?
     * @param  {number}}} miterLimit?
     * @param  {Object} settings?
     * @returns egret
     */
    public static createCircle(points: Array<any>, param: { beginFill?: { color: number, alpha: number }, lineStyle?: { thickness?: number, color?: number, alpha?: number, pixelHinting?: boolean, scaleMode?: string, caps?: string, joints?: string, miterLimit?: number } }, settings?: Object): egret.Shape {
        let shp = new egret.Shape();
        if (param.beginFill) {
            shp.graphics.beginFill(param.beginFill.color, param.beginFill.alpha);
        }
        if (param.lineStyle) {
            shp.graphics.lineStyle(param.lineStyle.thickness, param.lineStyle.color, param.lineStyle.alpha, param.lineStyle.pixelHinting, param.lineStyle.scaleMode, param.lineStyle.caps, param.lineStyle.joints, param
                .lineStyle.miterLimit);
        }
        shp.graphics.drawCircle(points[0], points[1], points[2]);
        if (settings) {
            for (let key in settings) {
                shp[key] = settings[key];
            }
        }
        return shp;
    }

    /**
     * 根据参数绘制圆弧路径
     * @param  {Array<any>} points
     * @param  {{beginFill?:{color:number} param
     * @param  {number}} alpha
     * @param  {{thickness?:number} lineStyle?
     * @param  {number} color?
     * @param  {number} alpha?
     * @param  {boolean} pixelHinting?
     * @param  {string} scaleMode?
     * @param  {string} caps?
     * @param  {string} joints?
     * @param  {number}}} miterLimit?
     * @param  {Object} settings?
     * @returns egret
     */
    public static createArc(points: Array<any>, param: { beginFill?: { color: number, alpha: number }, lineStyle?: { thickness?: number, color?: number, alpha?: number, pixelHinting?: boolean, scaleMode?: string, caps?: string, joints?: string, miterLimit?: number } }, settings?: Object): egret.Shape {
        let shp = new egret.Shape();
        if (param.beginFill) {
            shp.graphics.beginFill(param.beginFill.color, param.beginFill.alpha);
        }
        if (param.lineStyle) {
            shp.graphics.lineStyle(param.lineStyle.thickness, param.lineStyle.color, param.lineStyle.alpha, param.lineStyle.pixelHinting, param.lineStyle.scaleMode, param.lineStyle.caps, param.lineStyle.joints, param
                .lineStyle.miterLimit);
        }
        shp.graphics.drawArc(points[0], points[1], points[2], points[3], points[4], points[5]);
        if (settings) {
            for (let key in settings) {
                shp[key] = settings[key];
            }
        }
        return shp;
    }

    /**
     * 根据参数绘制圆角矩形
     * @param  {Array<any>} points
     * @param  {{beginFill?:{color:number} param
     * @param  {number}} alpha
     * @param  {{thickness?:number} lineStyle?
     * @param  {number} color?
     * @param  {number} alpha?
     * @param  {boolean} pixelHinting?
     * @param  {string} scaleMode?
     * @param  {string} caps?
     * @param  {string} joints?
     * @param  {number}}} miterLimit?
     * @param  {Object} settings?
     * @returns egret
     */
    public static createRoundRect(points: Array<any>, param: { beginFill?: { color: number, alpha: number }, lineStyle?: { thickness?: number, color?: number, alpha?: number, pixelHinting?: boolean, scaleMode?: string, caps?: string, joints?: string, miterLimit?: number } }, settings?: Object): egret.Shape {
        let shp = new egret.Shape();
        if (param.beginFill) {
            shp.graphics.beginFill(param.beginFill.color, param.beginFill.alpha);
        }
        if (param.lineStyle) {
            shp.graphics.lineStyle(param.lineStyle.thickness, param.lineStyle.color, param.lineStyle.alpha, param.lineStyle.pixelHinting, param.lineStyle.scaleMode, param.lineStyle.caps, param.lineStyle.joints, param
                .lineStyle.miterLimit);
        }
        shp.graphics.drawRoundRect(points[0], points[1], points[2], points[3], points[4], points[5]);
        if (settings) {
            for (let key in settings) {
                shp[key] = settings[key];
            }
        }
        return shp;
    }


    /**
     * 传入一个DisplayObject，将其从其父元素上移除
     * @param  {egret.DisplayObject} children
     */
    public static removeChild(children: egret.DisplayObject) {
        if (children.parent) {
            children.parent.removeChild(children);
        }
    }


    /**
     * 清空显示容器内显示元素，可输入start防止索引号之前的元素被清空
     * @param  {egret.DisplayObjectContainer} displayObjectContainer
     * @param  {number} start?
     */
    public static clearView(displayObjectContainer: egret.DisplayObjectContainer, start?: number) {
        isNaN(start) && (start = -1);
        while (displayObjectContainer.$children.length > start + 1) {
            displayObjectContainer.removeChildAt(start + 1);
        }
        return true;
    }

    /**
     * 生成某个元素的圆形mask
     * @param {egret.DisplayObject} view
     * @param {number} x 
     * @param {number} y 
     * @param {number} radius 
     */
    public static createCircleMask(view: egret.DisplayObject, x: number = 0, y: number = 0, radius: number = 0): void {
        x = x || view.x - view.anchorOffsetX + view.width / 2;
        y = y || view.y - view.anchorOffsetY + view.height / 2;
        radius = radius || view.width / 2;

        let mask = Util.createCircle([x, y, radius],
            { beginFill: { color: 0x000000, alpha: 1 } });
        view.parent.addChild(mask);
        view.mask = mask;
    }

    /**
     *添加字体阴影
     */
    public static addFontShawdow(label: eui.Label) {
        label.filters = [new egret.DropShadowFilter(3, 135, 0x000000, 0.3, 1, 1)];
    }

    /**
     * 截取字符串加省略号
     * 中文每个字长度是4，数字是2
     * */
    public static getChar(_str: string, _len: number): string {

        var _ba: egret.ByteArray = new egret.ByteArray;

        _ba.writeUTFBytes(_str);

        if (_ba.length < _len) return _str;

        _ba.position = 0;
        let str = _ba.readUTFBytes(_len);

        return str + "...";

    }

    /**
 * 判断系统||浏览器中英文
 * 对于不支持的浏览器 一律默认为 中文
 */
    static getLanguage() {
        var language = (navigator.language).toLowerCase()
        var locale = 'zh'
        if (language.indexOf('en') > -1) {
            locale = 'en'
        } else {
            locale = 'zh'
        }
        return locale
    }

    static isDev(): boolean {
        return window.location.port !== '' || /test|webdev/.test(location.host)
    }

    static toPokerColour(poker: number): number {
        return Math.ceil(poker / 13)
    }

    static toPoker(poker: number): number {
        return ((poker - 1) % 13) + 1;
    }

	/**
 * 计点
 * @param poker
 * @return
 */
    static pointPoker(poker: number): number {
        let point = this.toPoker(poker);
        if (point > 10)
            point = 10;
        return point;
    }

    static getDistanceBetweenTwoPoint(point1: { x: number, y: number }, point2: { x: number, y: number }): number {
        return Math.sqrt(Math.pow(point1.x - point2.x, 2) + Math.pow(point1.y - point2.y, 2))
    }
}