using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using Pathfinding;
using System.Threading.Tasks;
using System;
using System.Linq;
using System.Text;

public class Sector6BossScript : MonoBehaviour
{

// Designed by Jacob Weedman

    GameObject Player;
    GameObject ChosenSpawnZone;
    GameObject Camera;

    public Dictionary<string, int> EnemyDict = new Dictionary<string, int>();
    public List<string> EnemyList;

    public float Phase = 1f;
    public float Health = 1200;

    bool canSpawn = true;
    public List<GameObject> SpawnLocations;

    float InitialSpawnCooldown = 10;
    public float SpawnCooldown;


    void Start()
    {
        // Add spawn locations to list
        SpawnLocations = GameObject.FindGameObjectsWithTag("1").ToList();

        // Setup
        SpawnCooldown = InitialSpawnCooldown;
        EnemyDict["Grunt"] = 1;

        SpawnEnemy();
        SpawnEnemy();
        SpawnEnemy();
        SpawnEnemy();
        SpawnEnemy();
        SpawnEnemy();
    }

    void FixedUpdate()
    {
        // Health
        GameObject.Find("HealthIndicator").transform.localScale = new Vector3(Health / 1200 * 30, 1, 1);

        if (Health >= 1100)
        {
            EnemyDict["Grunt"] = 1;
            InitialSpawnCooldown = 10;
        }
        else if (Health >= 1000)
        {
            EnemyDict["Grunt"] = 2;
            EnemyDict["Sniper"] = 1;
            InitialSpawnCooldown = 8;
        }
        else if (Health >= 900)
        {
            EnemyDict["Grunt"] = 5;
            EnemyDict["Sniper"] = 2;
            //EnemyDict["Brute"] = 1;
            InitialSpawnCooldown = 6;
        }
        else if (Health >= 800)
        {
            EnemyDict["Grunt"] = 6;
            EnemyDict["Sniper"] = 3;
            //EnemyDict["Brute"] = 2;
            EnemyDict["Warper"] = 1;
            InitialSpawnCooldown = 4;
        }
        else if (Health >= 700)
        {
            EnemyDict["Grunt"] = 3;
            EnemyDict["Sniper"] = 4;
            //EnemyDict["Brute"] = 3;
            EnemyDict["Warper"] = 3;
            InitialSpawnCooldown = 2;
        }
        else if (Health >= 600)
        {
            EnemyDict["Sniper"] = 1;
            EnemyDict["Warper"] = 1;
            EnemyDict["Grunt"] = 0;
            //EnemyDict["Brute"] = 0;
            InitialSpawnCooldown = 1;
        }

        switch (Phase)
        {
#region Phase1

        case 1: // Phase 1

        // Decriment Health Automatically
        Health -= Time.deltaTime * 5;

        SpawnCooldown -= Time.deltaTime;

        if (SpawnCooldown <= 0)
        {
            SpawnCooldown = InitialSpawnCooldown;
            SpawnEnemy();
            SpawnEnemy();
            SpawnEnemy();
        }

        break; 
#endregion

#region Cutscene between 1 - 2

        case 1.5f: // Cutscene between plases 1 & 2

        break;

#endregion

#region Phase2
        case 2: // Phase 2

        break;

#region Death Animation
    case 2.5f:
        Phase = 3;
        break;
#endregion

#region Death
    case 3:
        break;
#endregion
            
    default:
        break;
        }
#endregion

    }

    void SpawnEnemy()
    {
        // Put enemies into a list
        EnemyList.Clear();
        foreach (KeyValuePair<string, int> entry in EnemyDict)
        {
           int EndAmmount = entry.Value;
           for (int j = 0; j < EndAmmount; j++)
            {
                EnemyList.Add(entry.Key);
            }
        }
        // Choose spawn zone
        ChosenSpawnZone = SpawnLocations[UnityEngine.Random.Range(0, SpawnLocations.Count)];

        // Choose enemy to spawn
        GameObject ChosenEnemy = GameObject.Find("EnemiesToSpawn").transform.Find(EnemyList[UnityEngine.Random.Range(0, EnemyList.Count())]).gameObject;

        // Give life
        GameObject InstantiatedEnemy = Instantiate(ChosenEnemy, ChosenSpawnZone.transform.position, Quaternion.identity);
        InstantiatedEnemy.GetComponent<Rigidbody2D>().gravityScale = 1.5f;
        InstantiatedEnemy.GetComponent<MasterEnemyAI>().enabled = true;
        InstantiatedEnemy.GetComponent<Seeker>().enabled = true;
        InstantiatedEnemy.GetComponent<MasterEnemyAI>().AbilityPlayerESP = true;

    }
}