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

public class GameController : MonoBehaviour
{

    public GameObject Neutral, IdleMania, GameEndPanel, MetalWin, PopWin;
    public GameObject[] Rocks;
    public GameObject[] bombs;
    public Transform[] SpawnTransforms;
    public RuntimeAnimatorController[] NeutralAnims;
    public Animator CameAnim;
    public static SoundBank sounds;
    public static bool P2IdleMania = false;
    public static bool P1IdleMania = false;
    public static int neutralCount = 30;
    int counter = 0;

    private void Awake()
    {
        PlayerPrefs.SetFloat("Player1Count", 1);
        PlayerPrefs.SetFloat("Player2Count", 1);
        P2IdleMania = false;
        P1IdleMania = false;
    }

    void Start()
    {
        sounds = GameObject.FindGameObjectWithTag("SoundBank").GetComponent<SoundBank>();
        CreateRandomNeutrals();
        GenerateWorld();
    }

    private void GenerateWorld()
    {
        for (int i = 0; i < Rocks.Length; i++)
        {
            GameObject.Instantiate(Rocks[i], new Vector2(UnityEngine.Random.Range(-2.5f, 2.5f), UnityEngine.Random.Range(-1.5f, 1.5f)), Quaternion.identity);
        }

    }

    private void CreateRandomNeutrals()
    {
        StartCoroutine(WaitABit());
        StartCoroutine(WaitIdleMania());
        StartCoroutine(WaitForBomb());
    }

    private IEnumerator WaitForBomb()
    {
        bool isHitRock = false;
        yield return new WaitForSeconds(UnityEngine.Random.Range(4, 8));
        Vector2 coords;
        while (true)
        {
            isHitRock = false;
            coords = new Vector2(UnityEngine.Random.Range(-2.5f, 2.5f), UnityEngine.Random.Range(-1.5f, 1.5f));
            Collider[] hitColliders = Physics.OverlapSphere(coords, 0.75f);
            foreach (Collider col in hitColliders)
            {
                if (col.tag == "Rocks")
                {
                    isHitRock = true;
                    break;
                }
            }
            if (isHitRock)
                continue;
            else
                break;



        }
        GameObject go = GameObject.Instantiate(bombs[UnityEngine.Random.Range(0, 2)], coords, Quaternion.identity) as GameObject;
        sounds.PlayMeteor();
        Destroy(go, 4f);
        yield return new WaitForSeconds(1.45f);
        CameAnim.SetTrigger("Shake");


        StartCoroutine(WaitForBomb());
    }

    private IEnumerator WaitIdleMania()
    {
        yield return new WaitForSeconds(8);
        bool isHit = false;
        Vector2 coords;
        while (true)
        {
            isHit = false;
            coords = new Vector2(UnityEngine.Random.Range(-2.5f, 2.5f), UnityEngine.Random.Range(-1.5f, 1.5f));
            Collider[] hitColliders = Physics.OverlapSphere(coords, 0.2f);
            foreach (Collider col in hitColliders)
            {
                if (col.tag == "Rocks" || col.tag == "Player1" || col.tag == "Player2" || col.tag == "Varil")
                {
                    isHit = true;
                    break;
                }
            }
            if (isHit)
                continue;
            else
                break;
        }
        GameObject go = GameObject.Instantiate(IdleMania, coords, Quaternion.identity) as GameObject;
        Destroy(go, 5);
        StartCoroutine(WaitIdleMania());
    }
    public void CheckEndGame()
    {
        PlayerPrefs.SetFloat("Player1Count", GameObject.FindGameObjectsWithTag("Player1").Length);
        PlayerPrefs.SetFloat("Player2Count", GameObject.FindGameObjectsWithTag("Player2").Length);
        if (PlayerPrefs.GetFloat("Player1Count") == 0)
        {
            PopWin.SetActive(true);
            GameEndPanel.SetActive(true);
            Time.timeScale = 0;
            sounds.PlayPopEnding();
            enabled = false;

        }
        else if (PlayerPrefs.GetFloat("Player2Count") == 0)
        {
            MetalWin.SetActive(true);
            GameEndPanel.SetActive(true);
            Time.timeScale = 0;
            sounds.PlayMetalEnding();
            enabled = false;

        }
    }
    private void Update()
    {
        CheckEndGame();
    }
    private IEnumerator WaitABit()
    {
        if (counter <= neutralCount)
        {
            yield return new WaitForSeconds(1);
            counter += 2;
            for (int i = 0; i < UnityEngine.Random.Range(1, 4); i++)
            {
                bool isHit = false;
                Vector2 coords;
                while (true)
                {
                    isHit = false;
                    coords = new Vector2(UnityEngine.Random.Range(-3.1f, 3.1f), UnityEngine.Random.Range(-1.5f, 1.9f));
                    Collider[] hitColliders = Physics.OverlapSphere(coords, 0.08f);

                    foreach (Collider col in hitColliders)
                    {
                        if (col.tag == "Rocks" || col.tag == "Player1" || col.tag == "Player2" || col.tag == "Varil" || col.tag == "Neutral" || col.tag == "Idle")
                        {
                            isHit = true;
                            break;
                        }
                    }
                    if (isHit)
                        continue;
                    else
                        break;
                }
                GameObject go = GameObject.Instantiate(Neutral, coords, Quaternion.identity) as GameObject;
                go.GetComponent<Animator>().runtimeAnimatorController = NeutralAnims[UnityEngine.Random.Range(0, 5)];
            }
            StartCoroutine(WaitABit());
        }

    }

    public void RetryButton()
    {
        Time.timeScale = 1;
        SceneManager.LoadScene(1);
    }
    public void HomePage()
    {
        Time.timeScale = 1;
        SceneManager.LoadScene(0);
    }

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