using UnityEngine;
using System.Collections.Generic;

public class Enemy : 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;
    
    // IA
    private Vector3 initialPosition;


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


    public Vector3 off = new Vector3(0, 1.5f, 0);
	// Use this for initialization
	void Start () {
        this.labelsS = new List<string>();
        this.labelsT = new List<int>();
        this.labelsX = new List<int>();
        style = new GUIStyle();    
        style.richText = true;    
	}
	

    void setData(int id) {


        this.loaded = true;
    }

    public void setGameObject(GameObject go) { this.go = go; }

    public void setData(int level, int health, string name, int attack, int id, GameObject mesh)
    {
        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;

        if (name == "Watcher")
        {
            AnimationWatcher a = (AnimationWatcher) this.go.AddComponent<AnimationWatcher>();
            a.setData(this.go);
        
        }
        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) {
            this.defeated();
        }
    }

    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 setInPlane(string name) {

    }

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

    void OnGUI() {
        if (Camera.current == null) return;
        
        Vector3 point = Camera.current.WorldToScreenPoint(this.target.position + off);
        
        if (message) {

            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]--;
                }
            }


        }
            
    }

    void Update()
    {
        if (loaded)
        {



        }

    }



}
