
import UserModel from "../game_module/UserModel";
import Vector3=Laya.Vector3;
export default class Game_Tool {

    public static GetObjKey(obj: any) {
        if (obj == null) return null;
        for (const key in obj) {
            if (obj.hasOwnProperty(key)) {
                return key;
            }
        }
    }
    //返回一个随机整数[min,max]
    public static randomInt(min: any, max: any): number {
        return parseInt(Math.random() * (max - min + 1) + min);
    }
    //p1 左向量 p2
    public static getAng(xx: number, yy: number, obl: number): number {
        return 180 / Math.PI * this.getRad(xx, yy, obl);
    }
    //返回角度
    public static getRad(xx: number, yy: number, obl: number): number {
        // 方法一：asin()
        // var rad:Number = xx > 0 ? ((Math.PI * 3)/2  + Math.asin(-yy/obl)) : (Math.PI / 2 -  Math.asin(-yy/obl));
        // 方法二：acos()
        var rad: number = yy < 0 ? Math.acos(xx / obl) : (Math.PI * 2 - Math.acos(xx / obl));
        // 方法三：atan2()
        // var rad:Number = yy < 0 ? Math.atan2(-yy, xx) : Math.PI * 2 + Math.atan2(-yy, xx);
        return rad;
    }

    public static getM_Vector(v1: Laya.Vector3, v2: Laya.Vector3): Laya.Vector3 {
        let temp = new Laya.Vector3();
        Laya.Vector3.add(v1, v2, temp);
        return new Laya.Vector3(temp.x * 0.5, temp.y * 0.5, temp.z * 0.5);
    }


    public static getReflectAng(dir:Vector3){
        var xx=Math.abs(dir.x);
        var zz=Math.abs(dir.z);
        var obl= Math.sqrt(Math.pow(dir.x, 2) + Math.pow(dir.z, 2));
        return 180 / Math.PI * Math.acos(xx / obl);
    }

    public static random_1to1():number{ 
        var iszheng=Math.random()>0.5?-1:1;
        return Math.random()*iszheng;
    }

    public static get64Encode(str): string {
        var c1, c2, c3;
        var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        var i = 0, len = str.length, string = '';
        while (i < len) {
            c1 = str.charCodeAt(i++) & 0xff;
            if (i == len) {
                string += base64EncodeChars.charAt(c1 >> 2);
                string += base64EncodeChars.charAt((c1 & 0x3) << 4);
                string += "==";
                break;
            }
            c2 = str.charCodeAt(i++);
            if (i == len) {
                string += base64EncodeChars.charAt(c1 >> 2);
                string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
                string += base64EncodeChars.charAt((c2 & 0xF) << 2);
                string += "=";
                break;
            }
            c3 = str.charCodeAt(i++);
            string += base64EncodeChars.charAt(c1 >> 2);
            string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
            string += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
            string += base64EncodeChars.charAt(c3 & 0x3F)
        }
        return string;
    }

    // public static SetHuTuiList(list:Laya.List,data?,scrol:number=1,type:number=0){ //scrol 1:竖的  2横的
    //     var arr=data?data:FYHD_Sdk.pushData;
    //     list.selectEnable = false;
    //     scrol==1?list.vScrollBarSkin = "":list.hScrollBarSkin = "";
    //     list.renderHandler = Laya.Handler.create(this, this.updateItem, [type], false);
    //     list.array = arr;
    //     list.refresh();
    //  }
    //  private static updateItem(type,cell: Laya.Box, index: number){
    //      var aditem=cell.getComponent(HuiTuiItem)as HuiTuiItem;
    //      aditem.Refresh(type);
    //  }
    //  public static makeRandomArr(arrList,num){
    //     if(num>arrList.length){
    //        return;
    //     }
    //     var tempArr=arrList.slice(0);
    //     var newArrList=[];    
    //     for(var i=0;i<num;i++){
    //         var random=Math.floor(Math.random()*(tempArr.length-1));
    //         var arr=tempArr[random];
    //         tempArr.splice(random, 1);
    //         newArrList.push(arr);    
    //     }
    //     return newArrList;
    // }
    // public static CopyArr(o):any{
    //     var arr=[];
    //     for (let index = 0; index < o.length; index++) {
    //         const element = o[index];
    //         arr.push(element);
    //     }
    //     return arr;
    // }


    // public static ShowQianDao(): boolean {
    //     let todayZero = new Date(UserModel.Instance.CurModel.ServerTime * 1000);
    //     todayZero.setHours(0, 0, 0, 0);
    //     if(!PlayerManager.Instance.sign)return true;
    //     var lastTime = Number(PlayerManager.Instance.sign["sign_time"]) * 1000;
    //     let lastSignTime = new Date(lastTime);
    //     if (lastTime == 0) {
    //         return true;
    //     }
    //     if (lastSignTime.valueOf() < todayZero.valueOf()) {
    //         return true;
    //     }
    //     return false;
    // }
}