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

public class AstroidSpawner : Singelton<AstroidSpawner> {


    public enum Mode { play, pause  };

    public Mode currentMode= Mode.pause;

    public int earthHP = 100;
    public int shipLives = 3;

    public GameObject player;
    public GameObject playerSpawn;

    //UI
    public GameObject menu;
    public GameObject ingameUI;
    public Slider earthHPSlider;
    float  time; 
    public GameObject[] AstroidPrefabs = new GameObject[3];

    public List<GameObject> astroids_inactiv = new List<GameObject>();
    public List<GameObject> astroids_active = new List<GameObject>();

	// Use this for initialization
	void Start ()
    {

        

	    //Pupulate Astroid Pool
        for(int i = 0; i<=100; i++)
        {
            instance.astroids_inactiv.Add(Instantiate(AstroidPrefabs[Random.Range(0, 3)]));  
        }

        
	}
	
    public void StartGame()
    {
        menu.SetActive(false);
        ingameUI.SetActive(true);
        currentMode = Mode.play;
        player.transform.position = playerSpawn.transform.position;
        player.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
        time = 0;
        earthHP = 100;
        earthHPSlider.value = earthHP;
        StartCoroutine(SpawnWave());
    }
    public void EndGame()
    {
        menu.SetActive(true);
        ingameUI.SetActive(false);
        currentMode = Mode.pause;
        StopAllCoroutines();
    }

	// Update is called once per frame
	void Update () {
        time += Time.deltaTime;
	}


    IEnumerator SpawnWave()
    {
        while(true)
        {
            float angle_center = Random.Range(0.0f, 1.0f) * Mathf.PI * 2;
            Vector3 pos_center;
            pos_center.x = Mathf.Cos(angle_center) * 200.0f;
            pos_center.y = Mathf.Sin(angle_center) * 200.0f;
            pos_center.z = 0.0f;
            yield return new WaitForSecondsRealtime(10.0f);
            for(int i = 0; i <= time/10.0f; i++)
            {
                float angle = Random.Range(0.0f, 1.0f)*Mathf.PI*2;
                Vector3 pos;
                pos.x = Mathf.Cos(angle) * 20.0f;
                pos.y = Mathf.Sin(angle) * 20.0f;
                pos.z = 0.0f;
                pos = pos_center + pos;
                instance.astroids_inactiv[0].transform.position = pos;
                instance.astroids_inactiv[0].SetActive(true);
                instance.astroids_active.Add(instance.astroids_inactiv[0]);
                instance.astroids_inactiv.RemoveAt(0);
            }
        }

        yield return null;
    }

    public void DespawnAstroid(GameObject astroid)
    {

        instance.astroids_active.Remove(astroid);
        instance.astroids_inactiv.Add(astroid);
    }


    public void EarthHit()
    {
        if(earthHP>5)
        {
            earthHP -= 5;
            earthHPSlider.value = earthHP;
        }
        else
        {
            EndGame();
            //Restart/Quit Options
        }
    }

    public void PlayerHit()
    {
        if(shipLives >1)
        {
            shipLives--;
        }
        else
        {
            EndGame();
            //Restart/Quit Options
        }
    }

    public void QuitGame()
    {
        Application.Quit();
    }
}
