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

public class SpawnManager : MonoBehaviour
{
    // Color of the gizmo
    public Color gizmoColor = Color.red;


    //-----------------------------------
    // All the Enums
    //-----------------------------------
    // Spawn types
    public enum SpawnTypes
    {
        Normal,
        Once,
        Wave,
        TimedWave
    }
    // The different Enemy levels
    public enum EnemyLevels
    {
        Pepsi,
        Hard,
        Boss
    }
    //---------------------------------
    // End of the Enums
    //---------------------------------

    // Enemy level to be spawnedEnemy
    public EnemyLevels enemyLevel = EnemyLevels.Pepsi;

    //----------------------------------
    // Enemy Prefabs
    //----------------------------------
    public GameObject Pepsi;
    public GameObject CocaCola;
    public GameObject PepsiBoss;
    public GameObject CocaColaBoss;
    private Dictionary<EnemyLevels, GameObject> Enemies = new Dictionary<EnemyLevels, GameObject>(4);
    //----------------------------------
    // End of Enemy Prefabs
    //----------------------------------

    //----------------------------------
    // Enemies and how many have been created and how many are to be created
    //----------------------------------
    public int totalEnemy = 10;
    private int numEnemy = 0;
    private int spawnedEnemy = 0;
    int storeTotalEnemy = 0;
    
    //----------------------------------
    // End of Enemy Settings
    //----------------------------------

    // The ID of the spawner
    private int SpawnID;

    //----------------------------------
    // Different Spawn states and ways of doing them
    //----------------------------------
    private bool waveSpawn = false;
    public bool Spawn = true;
    public SpawnTypes spawnType = SpawnTypes.Normal;
    // timed wave controls
    public float waveTimer = 30.0f;
    public static float timeTillWave = 0.0f;
    //Wave controls
    public int totalWaves = 5;
    public static int numWaves = 0;
    //----------------------------------
    // End of Different Spawn states and ways of doing them
    //----------------------------------


    public static bool isPepsi, isPepsiBoss;
    int random, randomBoss;

    void Start()
    {
		numWaves = 0;
        storeTotalEnemy = totalEnemy;
        // sets a random number for the id of the spawner
        SpawnID = Random.Range(1, 500);
        Debug.Log("True or not? : " + isPepsi);
        if(isPepsi == false)
        {
            Enemies.Add(EnemyLevels.Pepsi, Pepsi); //moet in start
        } else if(isPepsi == true)
        {
            Enemies.Add(EnemyLevels.Pepsi, CocaCola); //moet in start
        }
        Enemies.Add(EnemyLevels.Boss, CocaColaBoss);
        //Enemies.Add(EnemyLevels.CocaCola, CocaCola);
        Enemies.Add(EnemyLevels.Hard, PepsiBoss);
    }
    // Draws a cube to show where the spawn point is... Useful if you don't have a object that show the spawn point
    void OnDrawGizmos()
    {
        // Sets the color to red
        Gizmos.color = gizmoColor;
        //draws a small cube at the location of the gam object that the script is attached to
        Gizmos.DrawCube(transform.position, new Vector3(0.5f, 0.5f, 0.5f));
    }

    void Update()
    {

        if (Spawn)
        {
            // Spawns enemies everytime one dies
            if (spawnType == SpawnTypes.Normal)
            {
                // checks to see if the number of spawned enemies is less than the max num of enemies
                if (numEnemy < totalEnemy)
                {
                    // spawns an enemy
                    spawnEnemy();
                }
            }
            // Spawns enemies only once
            else if (spawnType == SpawnTypes.Once)
            {
                // checks to see if the overall spawned num of enemies is more or equal to the total to be spawned
                if (spawnedEnemy >= totalEnemy)
                {
                    //sets the spawner to false
                    Spawn = false;
                }
                else
                {
                    // spawns an enemy
                    spawnEnemy();
                }
            }
            //spawns enemies in waves, so once all are dead, spawns more
            else if (spawnType == SpawnTypes.Wave)
            {
                if (numWaves < totalWaves + 1)
                {
                    Debug.Log("Enemies Left: " + numEnemy);
                    if (waveSpawn)
                    {
                        if (isPepsi == false)
                        {
                            // Enemies.Add(EnemyLevels.Pepsi, Pepsi); //moet in start
                            Enemies[EnemyLevels.Pepsi] = Pepsi;
                        }
                        else if (isPepsi == true)
                        {
                            //Enemies.Add(EnemyLevels.Pepsi, CocaCola); //moet in start
                            Enemies[EnemyLevels.Pepsi] = CocaCola;
                        }

                        random = Random.Range(1, 3);
                        Debug.Log("Random Number: " + random);
                        if (random == 1)
                        {
                            isPepsi = true;
                        }
                        else if (random == 2)
                        {
                            isPepsi = false;
                        }

                        //spawns an enemy
                        spawnEnemy();
                    }
                    if (numEnemy == 0)
                    {
                        // enables the wave spawner
                        waveSpawn = true;
                        //increase the number of waves
                        numWaves++;
                        
                    }
                    if (numEnemy == totalEnemy)
                    {
                        // disables the wave spawner
                        waveSpawn = false;
         
                    }
                }
            }
            // Spawns enemies in waves but based on time.
            else if (spawnType == SpawnTypes.TimedWave)
            {
                // checks if the number of waves is bigger than the total waves
                if (numWaves <= totalWaves)
                {
                    // Increases the timer to allow the timed waves to work
                    timeTillWave += Time.deltaTime;
                  //  Debug.Log("WaveTijd: " + timeTillWave);
                    if (waveSpawn)
                    {
                        if (isPepsi == true)
                        {
                           // Enemies.Add(EnemyLevels.Pepsi, Pepsi); //moet in start
                            Enemies[EnemyLevels.Pepsi] = Pepsi;
                        }
                        else if (isPepsi == false)
                        {
                            //Enemies.Add(EnemyLevels.Pepsi, CocaCola); //moet in start
                            Enemies[EnemyLevels.Pepsi] = CocaCola;
                        }

                        random = Random.Range(1, 3);
                        Debug.Log("Random Number: " + random);
                        if (random == 1)
                        {
                            isPepsi = true;
                        }
                        else if (random == 2)
                        {
                            isPepsi = false;
                        }
                        //spawns an enemy
                        spawnEnemy();
                    }
                    
                    // checks if the time is equal to the time required for a new wave
                    if (timeTillWave >= waveTimer && numEnemy <= 0)
                    {
                        // enables the wave spawner
                        waveSpawn = true;
                        // sets the time back to zero
                        timeTillWave = 0.0f;
                        // increases the number of waves
                        numWaves++;
                        totalEnemy++;
                        Debug.Log("TotalEnemy: " + totalEnemy);
                        // A hack to get it to spawn the same number of enemies regardless of how many have been killed
                        numEnemy = 0;
                        
                    }
                    if (numEnemy >= totalEnemy)
                    {
                        // diables the wave spawner
                        waveSpawn = false;
                    }
                    if(numWaves == 5)
                    {
                        totalEnemy = 0;
                        enemyLevel = EnemyLevels.Boss;
                        if (isPepsiBoss == true)
                        {
                            // Enemies.Add(EnemyLevels.Pepsi, Pepsi); //moet in start
                            Enemies[EnemyLevels.Boss] = PepsiBoss;
                        }
                        else if (isPepsiBoss == false)
                        {
                            //Enemies.Add(EnemyLevels.Pepsi, CocaCola); //moet in start
                            Enemies[EnemyLevels.Boss] = CocaColaBoss;
                        }
                        randomBoss = Random.Range(1, 3);
                        Debug.Log("Random Number: " + random);
                        if (randomBoss == 1)
                        {
                            isPepsiBoss = true;
                        }
                        else if (randomBoss == 2)
                        {
                            isPepsiBoss = false;
                        }

                    } else
                    {
                        
                        enemyLevel = EnemyLevels.Pepsi;
                    }

                }
                else
                {
                    Spawn = false;
                }
            }
        }
    }
    // spawns an enemy based on the enemy level that you selected
    private void spawnEnemy()
    {
        float randomY = Random.Range(-6, 6);     
        Vector2 gameObjectPosition = new Vector2(gameObject.transform.position.x, randomY);
        GameObject Enemy = (GameObject)Instantiate(Enemies[enemyLevel], gameObjectPosition, Quaternion.identity);
        Enemy.SetActive(true);
        Enemy.SendMessage("setName", SpawnID);
        // Increase the total number of enemies spawned and the number of spawned enemies
        numEnemy++;
        spawnedEnemy++;
    }
    // Call this function from the enemy when it "dies" to remove an enemy count
    public void killEnemy(int sID)
    {
        // if the enemy's spawnId is equal to this spawnersID then remove an enemy count
        if (SpawnID == sID)
        {
            numEnemy--;
        }
    }
    //enable the spawner based on spawnerID
    public void enableSpawner(int sID)
    {
        if (SpawnID == sID)
        {
            Spawn = true;
        }
    }
    //disable the spawner based on spawnerID
    public void disableSpawner(int sID)
    {
        if (SpawnID == sID)
        {
            Spawn = false;
        }
    }
    // returns the Time Till the Next Wave, for a interface, ect.
    public float TimeTillWave
    {
        get
        {
            return timeTillWave;
        }
    }
    // Enable the spawner, useful for trigger events because you don't know the spawner's ID.
    public void enableTrigger()
    {
        Spawn = true;
    }
}