import Vector3 = Laya.Vector3;
import Tween = Laya.Tween;

export class TextData
{
    constructor(_message:string,_pos:Laya.Vector2,_size:Laya.Vector2,_fontSize:number,_color:string = null)
    {
        this.size = new Laya.Vector2(_size.x,_size.y);
        this.fontSize = _fontSize;
        this.color = _color;
        this.message = _message;
        this.pos = _pos;
    }
    size:Laya.Vector2;
    fontSize = 30;
    color:string;
    message = "";
    pos:Laya.Vector2;
}

export class GameUtils
{
    public static Vector3MulNumber(v3:Vector3, f:number):Vector3
    {
        return new Vector3(v3.x * f, v3.y * f, v3.z * f);
    }

    public static angleToEangle(_angle:number)
    {
        return _angle*Math.PI/180;
    }

    public static absEangle(_angle:Vector3)
    {
        var x = _angle.x;
        if(x == -0)
        {
            x = 0;
        }
        var y = _angle.y;
        if(y == -0)
        {
            y = 0;
        }
        var z = _angle.z;
        if(z == -0)
        {
            z = 0;
        }
        return new Vector3(x,y,z);
    }

    public static debugVector3(_vector3)
    {
        console.log(_vector3.x + " " + _vector3.y + " " + _vector3.z);
    }

    public static getConverseVector3(_vector3:Vector3)
    {
        return new Vector3(-_vector3.x,-_vector3.y,-_vector3.z)
    }

    public static getRandomInt(left:number,right:number):number
    {
        if(left >= right)
        {
            return 0;
        }
        var size = right - left + 1;
        var curNumber = Math.random()*size + left;
        //console.log("in:"+left+"..."+right + "out..." + curNumber);
        return Math.floor(curNumber);
    }
    public static getRandom(left:number,right:number):number
    {
        if(left >= right)
        {
            return 0;
        }
        var size = right - left + 1;
        var curNumber = Math.random()*size + left;
        //console.log("in:"+left+"..."+right + "out..." + curNumber);
        return curNumber;
    }
    public static getRandoms(left:number,right:number):number
    {
        if(left >= right)
        {
            return 0;
        }
        var size = right - left;
        var curNumber = Math.random()*size + left;
        //console.log("in:"+left+"..."+right + "out..." + curNumber);
        return curNumber;
    }

    public static TweenMove(targetTransform:Laya.Transform3D,TargetPos:Laya.Vector3,duration:number,_completeHandle:Laya.Handler = null):Laya.Tween
    {
        var curPos = targetTransform.position;
        var tween = Laya.Tween.to(curPos,{x:TargetPos.x,y:TargetPos.y,z:TargetPos.z,update:new Laya.Handler(this,function()
            {
                targetTransform.position = curPos;
            })},duration*1000,null,Laya.Handler.create(this,function()
        {
            tween.clear();
            if(_completeHandle != null)
            {
                _completeHandle.run();
            }
        }));
        return tween;
    }

    public static TweenProgress(_progress:Laya.ProgressBar,_fromValue:number,_tovalue:number,_duration:number,_completeHandle:Laya.Handler = null):Laya.Tween
    {
        //console.log("tweenProgress:from-"+_fromValue+"-to-"+_tovalue);
        _progress.value = _fromValue;
        var tween = Laya.Tween.to(_progress,{value:_tovalue},_duration*1000,null,_completeHandle);
        return tween;
    }

    //插值number
    public static lerp(form:number, to:number, t:number)
    {
        if (t <= 0) {
            return form;
        }else if (t >= 1) {
            return to;
        }
        var a = t * to + (1 - t) * form
        return a;
    }

    public static LogVector3(name :string, Pos:Laya.Vector3)
    {
        console.log(name + ":" + Pos.x + " " + Pos.y + " " + Pos.z);
    }
    //logVector2
    public static LogVector2(name :string, Pos:Laya.Vector2
    )
    {
        console.log(name + ":" + Pos.x + " " + Pos.y);
    }
    public static LogRotioion(name :string, Rot:Laya.Quaternion)
    {
        console.log(name + ":" + Rot.x + " " + Rot.y + " "+Rot.z+" "+ Rot.w);
    }

    public static DelayToDo(_delayDuration:number,_doFunction:Function,agrs = null)
    {
        Laya.timer.once(_delayDuration*1000,this,_doFunction,[agrs],false);
    }

    public static CopyDic(_obj:Object):Object
    {
        var curObj = {};
        for(var key in _obj)
        {
            curObj[key] = _obj[key];
        }
        return curObj;
    }

    public static TweenLookAtMove(targetTransform:Laya.Transform3D,TargetPos:Laya.Vector3,lookPos:Laya.Vector3,duration:number,_completeHandle:Function = null):Laya.Tween
    {
        var curPos = targetTransform.position;

        var tween = Laya.Tween.to(curPos,{x:TargetPos.x,y:TargetPos.y,z:TargetPos.z,update:new Laya.Handler(this,()=>
            {
                //targetTransform.lookAt(lookPos,Laya.Vector3._Up);
                targetTransform.position = curPos;
                //Vector3_Up;
                var quaternion1 = new Laya.Quaternion();
                Laya.Quaternion.lookAt(curPos,lookPos,new Vector3(0,1,0), quaternion1);
                quaternion1.invert(quaternion1);
                Laya.Quaternion.slerp(targetTransform.rotation,quaternion1,0.3,quaternion1);
                targetTransform.rotation = quaternion1;

            })},duration*1000,null,Laya.Handler.create(this,function()
        {
            tween.clear();
            if(_completeHandle != null)
            {
                _completeHandle();
            }
        }));
        return tween;
    }

    public static TweenGoldAward(_num:number,_pos:Laya.Vector2 = null,_completeHandle:Laya.Handler = null)
    {
        if(_num == 0)return;
        var parent = new Laya.Box;
        parent.width = 400;
        parent.height = 100;
        parent.pivotX = parent.width/2;
        parent.pivotY = parent.height/2;
        if(_pos == null)
        {
            _pos = new Laya.Vector2(Laya.stage.width/2,Laya.stage.height/2);
        }
        parent.pos(_pos.x,_pos.y);

        var _Image = new Laya.Image;
        _Image.skin = "localview/shoujiemian_1_6_1.png";
        _Image.width = 60;
        _Image.height = 60;
        _Image.left = 50;
        _Image.y = 10;
        parent.addChild(_Image);

        var curText = new Laya.Text;
        curText.width = 300;
        curText.height = 50;
        
        curText.align = "center";
        curText.valign = "middle";
        curText.font = "Microsoft YaHei";
        curText.fontSize = 35;
        curText.color = "#ff8e00";
        curText.bold = true;
        curText.font = "Microsoft YaHei";
        curText.text = "金币 +" + _num;
        curText.x = 60;
        curText.y = 10;
        parent.addChild(curText);
        
        
        Laya.stage.addChild(parent);

        var tween = Laya.Tween.to(parent,{y:parent.y - 200},1000,Laya.Ease.expoInOut,Laya.Handler.create(this,()=>
        {
            tween.clear();
            parent.destroy();
            if(_completeHandle)
            {
                _completeHandle.run();
            }
        }),100);

    }

    public static TweenText(textData:TextData,_hight:number,_completeHandle:Laya.Handler = null,hide = true):Laya.Text
    {
        var curText = new Laya.Text();
        
        curText.width = textData.size.x;
        curText.height = textData.size.y;
        curText.align = "center";
        curText.valign = "middle";
        curText.font = "Microsoft YaHei";
        curText.fontSize = textData.fontSize;
        curText.pivotX = curText.width/2;
        curText.pivotY =  curText.height/2;
        curText.color = "#5ad122";
        curText.bold = true;
        curText.stroke = 5;
        curText.strokeColor = "#ffffff";
        if(textData.color)
        {
            textData.color = textData.color;
        }
        curText.text = textData.message;
        curText.zOrder = 1;
        Laya.stage.addChild(curText);
        curText.pos(textData.pos.x,textData.pos.y);
        var tween = Laya.Tween.to(curText,{y:curText.y - _hight,update:new Laya.Handler(this,function()
            {

            })},500,Laya.Ease.expoInOut,Laya.Handler.create(this,function()
        {
            tween.clear();
            if(hide)
            {
                curText.destroy();
            }
            if(_completeHandle)
            {
                _completeHandle.run();
            }
        }));

        if(hide)
        {
            Laya.Tween.to(curText,{alpha:0.4},500);
        }
        

        return curText;
    }

    public static GetMoney(_num:number,_pos:Laya.Vector2 = null,_completeHandle:Laya.Handler = null)
    {
        if(_num == 0)return;
        var fafaafsfa = new Laya.Box;
        fafaafsfa.width = 400;
        fafaafsfa.height = 100;
        fafaafsfa.pivotX = fafaafsfa.width/2;
        fafaafsfa.pivotY = fafaafsfa.height/2;
        if(_pos == null)
        {
            _pos = new Laya.Vector2(Laya.stage.width/2,Laya.stage.height/2);
        }
        fafaafsfa.pos(_pos.x,_pos.y);

        var aafafaffa = new Laya.Image;
        aafafaffa.skin = "sign/qiandao_1_5_4.png";
        aafafaffa.width = 60;
        aafafaffa.height = 60;
        aafafaffa.left = 50;
        aafafaffa.y = 10;
        fafaafsfa.addChild(aafafaffa);

        var faasfafsafs = new Laya.Text;
        faasfafsafs.width = 300;
        faasfafsafs.height = 50;
        
        faasfafsafs.align = "center";
        faasfafsafs.valign = "middle";
        faasfafsafs.font = "Microsoft YaHei";
        faasfafsafs.fontSize = 35;
        faasfafsafs.color = "#ff8e00";
        faasfafsafs.bold = true;
        faasfafsafs.font = "Microsoft YaHei";
        faasfafsafs.text = "金币 +" + _num;
        faasfafsafs.x = 60;
        faasfafsafs.y = 10;
        fafaafsfa.addChild(faasfafsafs);
        
        
        Laya.stage.addChild(fafaafsfa);

        var faasfsaf = Laya.Tween.to(fafaafsfa,{y:fafaafsfa.y - 200},1000,Laya.Ease.expoInOut,Laya.Handler.create(this,()=>
        {
            faasfsaf.clear();
            fafaafsfa.destroy();
            if(_completeHandle)
            {
                _completeHandle.run();
            }
        }),100);

    }
}