using UnityEngine;
using System.Collections.Generic;

public class DemonScript : MonoBehaviour {

	public delegate void EndGame();
	public static event EndGame OnGameEnd; 

    public GameObject position1;
    public float speed = 1;
    private int id;

	public delegate void combo(int enemyId);
	public static event combo EnemyDied;
	public delegate void firstMatch(int firstMatch, int enemyComboSize,int shamanId);
	public static event firstMatch matches;

	bool died;
	string comboText;

	List<Direction> movesList;
	List<Direction> enemy;
	DirectionContainerScript container; 

	// Use this for initialization
	void OnEnable(){
		movesList = null;
		enemy = new List<Direction>();
		/*enemy.Add (Direction.Down);
		enemy.Add (Direction.Up);
		enemy.Add (Direction.Up);
		enemy.Add (Direction.Up);
		enemy.Add (Direction.Up);
		foreach (Direction comboDirection in enemy) {
			comboText += comboDirection + " ";
		}*/
		ShamanScript.OnListUpdated += ShamanScript_OnListUpdated;
	}

	void OnDisable(){
		ShamanScript.OnListUpdated -= ShamanScript_OnListUpdated;
	}

	void ShamanScript_OnListUpdated (List<Direction> directionList,int shamanId)
	{
		movesList = directionList;
		if (movesList != null) {
			Kill (movesList, enemy,shamanId);
			if (died)
				Debug.Log ("Enemy GG");
		}
	}
	
	// Update is called once per frame
	void Update () {

		transform.position = Vector3.MoveTowards(transform.position, position1.transform.position, speed * Time.deltaTime);
    }

    public void setData(int id,GameObject shaman, List<Direction> enemy,float speed)
    {
        position1 = shaman;
        this.id = id;
        this.enemy = enemy;
        this.speed = speed;
		CreateDirectionObjects();
    }

	private void CreateDirectionObjects()
	{
		container = (DirectionContainerScript)Instantiate(GlobalObjects.directionContainerPrefab,Vector3.zero,Quaternion.identity);
		container.transform.SetParent(transform);
		container.transform.localPosition = Vector3.back;
		foreach(Direction dir in enemy)
			container.AddDirection(dir);
	}

	public void Kill(List<Direction> combo, List<Direction> enemy,int shamanId){
		//Debug.Log ("kill func works");
		int enemySize = enemy.Count;
		int comboSize = combo.Count;
		died=false;
		int i, j;
		if (comboSize >= enemySize) {
			for (i = 0; i < comboSize-enemySize+1; i++) {
				for (j = 0; j < enemySize; j++){
					//Debug.Log ("nested loop");
					if (combo [i + j] != enemy [j])
						break;
					
				}
				if (j == enemySize) {
					died = true;
					Debug.Log ("first match: " + i + "size: " + enemySize);
					if (matches != null) {
						matches (i, enemySize,shamanId);

					}
					break;
				}
			}
		}
   

        if (died) {
			if (EnemyDied != null)
				EnemyDied (id);
			Destroy (container.gameObject);
			Destroy (gameObject);
		}
    
    }

	void OnTriggerEnter2D(Collider2D coll){

		if (coll.gameObject.tag.Equals("Shaman"))
		{
			Debug.Log("End Game!!!!!!!");
			if (OnGameEnd != null)
				OnGameEnd();
			this.enabled = false;
		}


	}
}
