using UnityEngine;
using System.Collections.Generic;

public class WatcherEnemy : MonoBehaviour {

    //private GameObject go;
    //private string name;
    //private int level;
    //private int health;
    //private int attack;
    //private int id;
    //private bool loaded = false;
    //private string strM = "";
    //private bool message = false;
    //private BoxCollider col;

    //private int cooldown = 20;
    //private int minDistance = 1;
    //private int wait = 0;

    //private AnimationWatcher animator;

    //// IA
    //private Vector3 initialPosition;
    //private Transform transformPlayer;
    //private float distance;
    //private int state;
    //private bool focused = false;
    //private bool comingBack = false;
    //private bool attacking0 = false;
    //private bool attacking1 = false;
    //private bool transition = false;
    //private Vector3 colSize;

    //// Labels for damage info
    //private Transform target;
    //private List<string> labelsS;
    //private List<int> labelsT;
    //private List<int> labelsX;
    //GUIStyle style;

    //int idleState = Animator.StringToHash("Base Layer.Armature|Idle 0");
    //int attack1State = Animator.StringToHash("Base Layer.Armature|Attack1 0");
    //int attack2State = Animator.StringToHash("Base Layer.Armature|Attack2 0");
    //int dieState = Animator.StringToHash("Base Layer.Armature|Die");

    //public Vector3 off = new Vector3(0, 1.5f, 0);

    //void Awake() {
    //    this.labelsS = new List<string>();
    //    this.labelsT = new List<int>();
    //    this.labelsX = new List<int>();
    //    style = new GUIStyle();
    //    style.richText = true;
    //}

    //void Start()  { }


    //public void setData(int level, int health, string name, int attack, int id, 
    //    GameObject mesh, Transform player, AnimationWatcher animator)
    //{
    //    this.level = level;
    //    this.name = name;
    //    this.attack = attack;
    //    this.health = health;
    //    this.id = id;
    //    this.go = mesh;
    //    this.target = this.transform;
    //    this.initialPosition = this.transform.position;
    //    this.transformPlayer = player;
    //    this.animator = animator;
    //    this.col = this.go.transform.Find("Cube").GetComponent <BoxCollider>();
    //    this.colSize = this.col.size;
    //    this.loaded = true;
    //}

    //public void setDamage(int value) {
    //    Debug.Log("Health: " + this.health + " Damage: " + value);
    //    this.health -= value;
    //    strM = "" + value;

    //    this.labelsS.Add(strM);
    //    this.labelsT.Add(90);
    //    this.labelsX.Add(Random.Range(-5, 5));
    //    this.message = true;


    //    if (health <= 0) {
    //        transition = true;
    //        this.animator.beginDeath();
    //    }
    //}

    //void OnTriggerStay(Collider other) {
    //    GameObject coll = other.gameObject;
        
    //    if (coll.tag == "Player")  {
    //        Player p = (Player) coll.GetComponent<Player>();

    //        if (this.attacking0) {
    //            p.receiveDamage(10);
    //        } else {
    //            p.receiveDamage(5);
    //        }

    //    }
    //}

    //void defeated() {
    //    Debug.Log("Enemy defeated!");
    //    if (!messagesRemaining()) Destroy(this.go);
    //    else Invoke("defeated", 2);
    //}

    //bool messagesRemaining()
    //{
    //    foreach (int i in this.labelsT)
    //    {
    //        if (i != 0) return true;
    //    }

    //    return false;

    //}

    //void disableGUI() { this.message = false; }


    //// Show the damage labels on the GUI
    //void OnGUI() {
    //    if (Camera.current == null) return;
        
    //    if (message) {
    //        Vector3 point = Camera.current.WorldToScreenPoint(this.target.position + off);

    //        for (int i = 0; i < this.labelsS.Count; i++)  {
    //            if (this.labelsT[i] > 0)   {
    //                Rect t = new Rect(point.x + this.labelsX[i], Screen.height - point.y - 100, 300, 300);
    //                GUI.Label(t, "<size=" + ((this.labelsT[i] + 1)) + ">" + this.labelsS[i] + "</size>");
    //                this.labelsT[i]--;
    //            }
    //        }
    //    }
    //}

    //// Get ready for the chaos <3
    //void Update() {

    //    // Try to prevent to play upload before the mesh and other data are loaded.
    //    if (loaded) {
            
    //        // State from the animator.
    //        state = this.animator.getAnimationStatus();

    //        // DISCLAIMER
    //        // Not using a switch cause it sucks with var comparation.

    //        // IDLE STATE
    //        if (state == idleState) {
    //            Debug.Log("IdleState");

    //            // This will be true when is returning from the attack0State. There are some
    //            // things to fix before coming back to idle...
    //            if (attacking0) {
    //                this.animator.beginIdle();
    //                this.col.size = this.colSize;
    //                this.attacking0 = false;
    //            } 

    //            // Same for attack1State....
    //            else if (attacking1) {
    //                    this.animator.beginIdle();
    //                    this.attacking1 = false;
    //                } 
                
    //            // The player is out of sight, is the enemy being lazy in the initial pos? MAYBE
    //            else if (!focused && !comingBack) {
    //                distance = Vector3.Distance(this.transformPlayer.position, this.go.transform.position);
    //                //Debug.Log("Not focused. Distance: " + distance);
            
    //                // Player incoming. Keep attention!
    //                if (distance < 20.0f) {                    
    //                    focused = true;
    //                }

    //            } 
                
    //            // Mob is following the player.
    //            else if (focused && !comingBack) {
                    
    //                //distance = Vector3.Distance(this.transformPlayer.position, this.go.transform.position);
    //                //Debug.Log("Focused. Distance: " + distance);

    //                // If the player is so far away, just turn back to the original position.
    //               if (distance > 20.0f) {
    //                    focused = false;
    //                    comingBack = true;
    //               } else {
    //                    // If the distance is less than the minDistance, do the attack0 ALWAYS.
    //                    if (distance < minDistance) {
    //                        if (this.animator.getWait() > 0) return;
    //                        this.animator.beginAttack0();
    //                        transition = true;
    //                        this.attacking0 = true;
    //                        this.focused = false;

    //                    } 
                        
    //                    // Else, the random factor joins into the battle.
    //                    else {
    //                        int random = Random.Range(0, 11);

    //                        // 0-7 means attack.
    //                        // 8-10 means just keep moving.
    //                        //Debug.Log("Random: " + random);
    //                        if (random > 7) {
    //                            if (this.animator.getWait() > 0) return;
    //                            this.animator.beginAttack1();
    //                            transition = true;
    //                            this.attacking1 = true;
    //                            this.focused = false;

    //                        } else {

    //                            // Move following the player.
    //                            this.go.transform.position = Vector3.MoveTowards(this.go.transform.position,
    //                                                              this.transformPlayer.position,
    //                                                              Time.deltaTime * 1.5f);

    //                            // Rotates for be looking at the player. (Inverse, because i'm a disaster
    //                            // modelling: the model is looking to negative axis and I don't want 
    //                            // to touch the prefab) :D
    //                            Vector3 targetRot = this.go.transform.position - this.transformPlayer.position;
                                
    //                            this.go.transform.rotation = Quaternion.LookRotation(
    //                                Vector3.RotateTowards(  this.go.transform.forward,
    //                                                        targetRot, 
    //                                                        Time.deltaTime * 10.0f, 0.0f)
    //                                                     );
    //                        }

                        
    //                    }
    //                }

    //            } 
    //            // Mob is tired of being following the player and comes back to the original position.
    //            else if (!focused && comingBack) {
                    
    //                // Player incoming. Keep attention!
    //                if (distance < 20.0f) {                    
    //                    focused = true;
    //                    comingBack = false;
    //                    return;
    //                } 

    //                    this.go.transform.position = Vector3.MoveTowards(this.go.transform.position,
    //                                      this.initialPosition,
    //                                      Time.deltaTime * 1.5f);

    //                    Vector3 targetRot = this.go.transform.position - this.transformPlayer.position;

    //                    this.go.transform.rotation = Quaternion.LookRotation(
    //                        Vector3.RotateTowards(this.go.transform.forward,
    //                                                targetRot,
    //                                                Time.deltaTime * 10.0f, 0.0f)
    //                                             );

    //                // Arrives to the original position. 
    //                if (this.go.transform.position == this.initialPosition) comingBack = false;
    //            }

    //        }
    //        // Mob is attacking the player with the attack0.
    //        else if (state == attack1State) {

    //            // The Attack0 needs to increase the size of the collider.
    //            this.col.size = new Vector3(this.col.size.x, this.col.size.y, this.col.size.z * 1.05f);
    //            Debug.Log("Attack1State");
    //        }

    //        // Mob is attacking the player with the attack1.
    //        else if (state == attack2State) {
    //            if (transition) {
    //                GameObject bullet = (GameObject)Instantiate(Resources.Load("Enemies/WatcherBullet"), this.transform.position, this.transform.rotation);
    //                bullet.GetComponent<Rigidbody>().AddRelativeForce(-Vector3.forward * 1000.0f);
    //                transition = false;
    //            }
    //            // The attack1 shoots a kind of capsule to the player.
                
    //        }
    //        else if (state == dieState)
    //        {
    //            if (transition) {
    //                defeated();
    //                transition = false;
    //            }
                
    //        }
    //        else
    //        {
    //            //WTF
    //        }
            

    //    }

    //}



}
