using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class GameController : MonoBehaviour
{
    public GameObject enemyTarget;
    public List<EnemyWave> enemyWaveList;
    private List<DemonScript> demonList;
    public GameObject demonPrefab;
	public ShamanScript shamanPrefab;
	public DirectionContainerScript directionContainerPrefab;
	public Sprite[] demons;

	public GameObject startPanel;
	public GameObject gameOverPanel;

    void Awake()
    {
		GlobalObjects.directionContainerPrefab = directionContainerPrefab;
        demonList = new List<DemonScript>();
        enemyWaveList = new List<EnemyWave>();
        enemyWaveList.Add(new EnemyWave(3, 0.5f, 3));
        enemyWaveList.Add(new EnemyWave(3, 0.5f, 4));
        enemyWaveList.Add(new EnemyWave(4, 0.7f, 3));
        enemyWaveList.Add(new EnemyWave(4, 1f, 4));
        enemyWaveList.Add(new EnemyWave(5, 1.5f, 4));
        enemyWaveList.Add(new EnemyWave(4, 1.5f, 5));
    }


    void OnEnable() {
        DemonScript.EnemyDied += enemyCount;
		DemonScript.OnGameEnd += ShamanScript_OnGameEnd;

    }
    
    void OnDisable()
    {
        DemonScript.EnemyDied -= enemyCount;
		DemonScript.OnGameEnd -= ShamanScript_OnGameEnd;
    }

	void ShamanScript_OnGameEnd ()
	{
		gameOverPanel.SetActive(true);
	}

	public void StartGame(int index)
	{
		if((GameType)index == GameType.single)
		{
			ShamanScript ss = GameObject.Instantiate(shamanPrefab,new Vector3(0,-3.5f,0),Quaternion.identity) as ShamanScript;
			Debug.Log(ss);
			ss.SetData(new KeyCode[]{KeyCode.UpArrow,KeyCode.RightArrow,KeyCode.DownArrow,KeyCode.LeftArrow},0);
		}
		else
		{
			ShamanScript ss = Instantiate(shamanPrefab,new Vector3(-6,-3.5f,0),Quaternion.identity) as ShamanScript;
			ss.SetData(new KeyCode[]{KeyCode.UpArrow,KeyCode.RightArrow,KeyCode.DownArrow,KeyCode.LeftArrow},0);
			ss = Instantiate(shamanPrefab,new Vector3(6,-3.5f,0),Quaternion.identity) as ShamanScript;
			ss.SetData(new KeyCode[]{KeyCode.W,KeyCode.D,KeyCode.S,KeyCode.A},1);
			ss = GameObject.Instantiate(shamanPrefab,new Vector3(0,-3.5f,0),Quaternion.identity) as ShamanScript;
			Debug.Log(ss);
			ss.SetData(new KeyCode[]{KeyCode.U,KeyCode.K,KeyCode.J,KeyCode.H},2);

		}
		enemyDecrease();
		startPanel.SetActive(false);
	}

	public void Restart()
	{
		UnityEngine.SceneManagement.SceneManager.LoadScene("Game");

	}

    int wavenumber = 0;

    void enemyDecrease() {
        if (enemyWaveList[wavenumber] != null)
        {
            if (enemyWaveList[wavenumber].getEnemyCount() != 0)
            {
                enemycount = enemyWaveList[wavenumber].getEnemyCount();

                CreateWave(enemyWaveList[wavenumber]);
            }
        }

        else {
           
            Debug.Log("enemylist bitti");
            Debug.Break();
             }
    }
    void enemyCount(int x)
    {
        enemycount --;
        if (enemycount == 0) {
            wavenumber++;
            enemyDecrease();
        }

    }
    int enemycount = 0;
 

    void CreateWave(EnemyWave wave)
    {
        List<List<Direction>> dir = new List<List<Direction>>();
        int randomCount = wave.getEnemyCount() ;
        for (int i = 0; i < randomCount; i++) {
            dir.Add(new List<Direction>());
            for (int j = 0; j < wave.getSignCount(); j++)
            {
                dir[i].Add((Direction)Random.Range(0, 4));
                
            }
            
            GameObject go = GameObject.Instantiate(demonPrefab,new Vector3(Random.Range(-9.3f,9.3f),Random.Range(6.5f,9f),0),Quaternion.identity) as GameObject;
			go.GetComponent <SpriteRenderer>().sprite = demons[Random.Range (0,2)];
            go.AddComponent<DemonScript>().setData(i, enemyTarget, dir[i], wave.enemySpeed + Random.Range(0.1f, 0.5f));
            DemonScript ds = go.GetComponent<DemonScript>();
            demonList.Add(ds);
        }


    }
   
}
