using UnityEngine;
using System.Collections.Generic;

public class EnemyLoader : MonoBehaviour {

    // Atributes
    private List<GameObject> enemies;
    private Dictionary<string, List<EnemyDictionary>> enemyDict;
    private Transform positionPlayer;
    private string currentPlane;
    private GameObject mesh;

	// Use this for initialization
	void Start () {
        this.enemies = new List<GameObject>();
        this.enemyDict = new Dictionary<string, List<EnemyDictionary>>();
        this.LoadEnemies();
	}

    // Creates and enemy from the model.
    void createEnemy(EnemyDictionary e) {
        LoadMesh(e.getLevel(), e.getAttack(), e.getHealth(), e.getName(), e.getId(), e.getPathToPrefab(), e.getPosition());
    }
    
    //void createEnemy(int level, int attack, int health, string name, int id, string prefab, Vector3 pos) {
    //    LoadMesh(level, attack, health, name, id, "", pos);
    //}

    void LoadMesh(int level, int attack, int health, string name, int id, string prefab, Vector3 pos) {
        Debug.Log(level + " " + attack + " " + health + " " + id + " " + prefab + " " + pos + " " + name);
        mesh = (GameObject)Instantiate(Resources.Load(prefab), pos, transform.rotation);
        Debug.Log("Mesh " + name + " instantiated.");
        mesh.name = name;

        if (name == "Watcher") {

            AnimationWatcher animationScript = mesh.transform.Find("Cube").GetComponent<AnimationWatcher>();
            animationScript.setData(mesh);

            EnemyWatcher enemyScript = mesh.transform.Find("Cube").GetComponent<EnemyWatcher>();
            enemyScript.setData(level, health, name, attack, id, mesh, positionPlayer, animationScript);
        }

        else if (name == "Demon")
        {
            AnimationDemon animationScript = mesh.transform.Find("Cube").GetComponent<AnimationDemon>();
            animationScript.setData(mesh);

            EnemyDemon enemyScript = mesh.transform.Find("Cube").GetComponent<EnemyDemon>();
            enemyScript.setData(level, health, name, attack, id, mesh, positionPlayer, animationScript);
        }

        this.enemies.Add(mesh);
    }

    // Reads the enemy CSV and link the enemies with the planes.
    void LoadEnemies() {
        TextAsset graph = (TextAsset) Resources.Load("enemyLoader");
        string plane;

        foreach (string line in graph.text.Split('\n')) {
            if (line.Contains("#")) continue;
            plane = line.Split(',')[0];
        
            // Append the enemy to a existent key o creates it.
            if (! this.enemyDict.ContainsKey(plane)){
                this.enemyDict[plane] = new List<EnemyDictionary>();    
            }

            this.enemyDict[plane].Add(new EnemyDictionary(line));
        }
    }

    // Return the enemies of a plane.
    List<EnemyDictionary> getEnemiesFromPlane(string plane) {
        if (this.enemyDict.ContainsKey(plane) )
            return this.enemyDict[plane];
        else
            return new List<EnemyDictionary>();
    }

    // Load the enemies of a plane.
    public void Load(string plane) {

        // If there is a change of plane, the alive enemies must die.
        if (currentPlane != "") {
            foreach (GameObject g in this.enemies) {
                Destroy(g);
            }
        }

        // ... and raise again.
        this.enemies = new List<GameObject>();

        currentPlane = plane;

        //Debug.Log("Loading enemies from plane " + plane);
        this.positionPlayer = GameObject.Find("PlayerBody").transform;
        
        // Load the enemies of the concrete plane.
        foreach (EnemyDictionary e in getEnemiesFromPlane(plane)) {
            //Debug.Log(e.getName());
            createEnemy(e);
        }
    }


    

}
