import Transform3D = Laya.Transform3D;
import Sprite3D = Laya.Sprite3D;
import Vector3 = Laya.Vector3;
import Quaternion = Laya.Quaternion;
import { MathTool } from "../Util/MathTool";
import { GlobalParam } from "../Net/GlobalParam";
import { PlayerManager } from "./PlayerManager";
import {RoleManager} from "../Manager/RoleManager";
import {PlaneManager} from "../Manager/PlaneManager";
import { GamePool } from "./GamePool";
import { ParticleManager } from "./ParticleManager";
import {GameManager} from "./GameManager";
import {ResourcesManager} from "./ResourcesManager";


import Animation = Laya.Animation;
import Stage = Laya.Stage;
import Browser = Laya.Browser;
import Handler = Laya.Handler;
import Tween = Laya.Tween;
import WebGL = Laya.WebGL;

export class Opponent extends Laya.Script3D {

    public selfobj: Sprite3D;
    private tran: Transform3D;
    private rayPoint: Transform3D;
    public forW: Vector3;
    private quaternion1: Quaternion;
    private speed: number;
    private _up = new Vector3(0, 1, 0);
    //private mat:Laya.PBRStandardMaterial;
    onAwake() {
        this.selfobj = this.owner as Sprite3D;
        this.tran = this.selfobj.transform;
        this.forW = MathTool.VecZero;
        this.speed = GlobalParam.enemySpeed;
        this.quaternion1 = new Quaternion();
    }

    private tarPos: Vector3;
    private hp: number;
    private tarSp: Sprite3D;
    private tar1: Vector3;
    private isArrive1 = false;

    Run() {
        if (this.isArrive1) {
            if (this.tarSp == null) return;
            if (!this.tarSp.active) {
                this.tarSp = RoleManager.Instance.GetTar();
                if (this.tarSp == null) return;
                this.tarPos = this.tarSp.transform.position.clone();
            }
            Vector3.subtract(this.tarPos, this.tran.position, this.forW);
            Vector3.normalize(this.forW, this.forW);
            this.forW = new Vector3(this.forW.x * this.speed, this.forW.y * this.speed, this.forW.z * this.speed);
            this.tran.translate(this.forW, false);
            if (Vector3.distance(this.tran.position, this.tarPos) < 0.1) {
                return;
            }
            Quaternion.lookAt(this.tarPos, this.tran.position, this._up, this.quaternion1);
            this.quaternion1.invert(this.quaternion1);
            this.tran.rotation = this.quaternion1.clone();
        } else {
            Vector3.subtract(this.tarPos, this.tran.position, this.forW);
            Vector3.normalize(this.forW, this.forW);
            this.forW = new Vector3(this.forW.x * this.speed, this.forW.y * this.speed, this.forW.z * this.speed);
            this.tran.translate(this.forW, false);
            if (Vector3.distance(this.tran.position, this.tarPos) < 0.1) {


                this.tarSp = RoleManager.Instance.GetTar();
                this.isArrive1 = true;
                if (this.tarSp == null) return;
                this.tarPos = this.tarSp.transform.position.clone();
                return;
            }
            Quaternion.lookAt(this.tarPos, this.tran.position, this._up, this.quaternion1);
            this.quaternion1.invert(this.quaternion1);
            this.tran.rotation = this.quaternion1.clone();
        }
    }

    /****************************************************** */
            // 一只凤凰的分辨率是550 * 400
            private phoenixWidth: number = 550;
            private phoenixHeight: number = 400;
    
            private bgColorTweener: Tween = new Tween();
            private gradientInterval: number = 2000;
            private bgColorChannels: any = { r: 99, g: 0, b: 0xFF };
    private createPhoenixes(): void {
        var scaleFactor: number = Math.min(
            Laya.stage.width / (this.phoenixWidth * 2),
            Laya.stage.height / this.phoenixHeight);

        // 加了混合模式的凤凰
        var blendedPhoenix: Animation = this.createAnimation();
        blendedPhoenix.blendMode = "lighter";
        blendedPhoenix.scale(scaleFactor, scaleFactor);
        blendedPhoenix.y = (Laya.stage.height - this.phoenixHeight * scaleFactor) / 2;

        // 正常模式的凤凰
        var normalPhoenix: Animation = this.createAnimation();
        normalPhoenix.scale(scaleFactor, scaleFactor);
        normalPhoenix.x = this.phoenixWidth * scaleFactor;
        normalPhoenix.y = (Laya.stage.height - this.phoenixHeight * scaleFactor) / 2;
    }

    private createAnimation(): Animation {
        var frames: Array<string> = [];
        for (var i: number = 1; i <= 25; ++i) {
        }

        var animation: Animation = new Animation();
        animation.loadImages(frames);
        Laya.stage.addChild(animation);

        var clips: Array<string> = animation.frames.concat();
        // 反转帧
        clips = clips.reverse();
        // 添加到已有帧末尾
        animation.frames = animation.frames.concat(clips);

        animation.play();

        return animation;
    }
    /****************************************************** */


    Recovery() {
        Laya.timer.clearAll(this);
        RoleManager.Instance.RecoverEnemy(this.selfobj);
        GamePool.Instance.RecoverModel(this.selfobj);
    }

    InitModel(tar, hp,cloth) {
        this.tarSp = tar;
        this.isArrive1 = false;
        if (this.tarSp == null) return;
        this.tarPos = this.tarSp.transform.position.clone();
        if (this.tarPos.x > 0) {
            this.tarPos = PlaneManager.Instance.toured1.clone();
        } else {
            this.tarPos = PlaneManager.Instance.toured2.clone();
        }
        this.hp =  hp;
        Laya.timer.frameLoop(1, this, this.Run);
    }

    //被攻击
    Injured(hitpos) {
        this.hp -= PlayerManager.bulletAttack;
        if (this.hp <= 0) {
            this.Dead();
            return;
        }
        ParticleManager.Instance.AttackEffect(hitpos);
    }

    Dead() {
        ParticleManager.Instance.BloodEffect(this.tran.position);
        this.Recovery();
        RoleManager.Instance.CheckGameOver();
        GameManager.Instance.roleCount++;
    }

}