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

public class WorldController : MonoBehaviour {

    private float score = .5f;
    public Slider winSlider;

    public Text winText;
    public GameObject winPanel;

    private enum Transmission
    {
        none,
        green,
        red
    }
    private Transmission transmission = Transmission.none;

    public bool transmissionGreen;
    public Transform radioGreen;
    public bool transmissionRed;
    public Transform radioRed;

    public List<GameObject> stuffToRandomSpawn;
    public List<GameObject> placesToRandomSpawn;
    private float randomSpawnTimer = 0.0f;
    public float randomSpawnCooldown = 5f;
    
    public void MainMenu()
    {
        SceneManager.LoadScene("mainmenu");
    }

	// Update is called once per frame
	void Update () {
        randomSpawnTimer += Time.deltaTime;
        if(randomSpawnTimer > randomSpawnCooldown)
        {
            GameObject randomGO = Instantiate(stuffToRandomSpawn[Random.Range(0, stuffToRandomSpawn.Count)]);
            randomGO.transform.position = placesToRandomSpawn[Random.Range(0, placesToRandomSpawn.Count)].transform.position;
            randomSpawnTimer = 0;
        }

        int WHO = 0;
        if (transmissionGreen) WHO++;
        if (transmissionRed) WHO--;
        if (WHO == 1)
            transmission = Transmission.green;
        if (WHO == -1)
            transmission = Transmission.red;
        if (WHO == 0)
            transmission = Transmission.none;
        
        switch (transmission)
        {
            case Transmission.green:
                score += Time.deltaTime / 30;
                break;
            case Transmission.red:
                score -= Time.deltaTime / 30;
                break;
        }



        winSlider.value = score;
        if (score >= 1) PlayerWon(true);
        if (score <= 0) PlayerWon(false);
    }

    public void PlayerWon(bool Green)
    {
        if (Green)
        {

            winText.text = "Player Green WON";
        winPanel.GetComponent<Image>().color = Color.green;
        }
        else{

            winText.text = "Player Red WON";
        winPanel.GetComponent<Image>().color = Color.red;
        }
        winPanel.SetActive(true);
    }

    public void OpenTransmission(Transform radio)
    {
        if(radio == radioGreen)
        {
            transmissionGreen = true;
        } else
        {
            transmissionRed = true;
        }
    }

    public void CloseTransmission(Transform radio)
    {
        if (radio == radioGreen)
        {
            transmissionGreen = false;
        }
        else
        {
            transmissionRed = false;
        }
    }
}
