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

public class GameController : MonoBehaviour {

    public static GameController Instance;

    public bool gameStarted = false;
    public enum FloodState {COMING = 0, STARTED = 1, ENDED = 3};
    public FloodState floodState;
    public float timeToStart = 60;
    public float maxEnergy;
    public float energyRechargeSpeed = 1f;
    public Transform selectionOutline;

    //GAME STATS
    [Header("Game Stats")]
    public int startingMoney;
    public int money;
    private List<int> moneyValues;
    public float energy;
    private List<int> energyValues;

    public int housesNormal;
    public int housesFlooded;
    public int housesDestroyed;
    private List<int> housesValues;

    public int peopleNormal;
    public int peopleEvacuated;
    public int peopleInjured;
    public int peopleKilled;
    private List<int> peopleValues;

    //FLOOD PARAMETERS
    [Header("Flood parameters")]
    public float maxModifier = 100f;
    public float floodDuration = 60f;
    public float floodElapsed = 0;
    public AnimationCurve floodCurve;

    void Awake()
    {
        Instance = this;

        money = startingMoney;
        energy = maxEnergy;
        ActivateSelectionOutline(false);

        moneyValues = new List<int>(2){ 0, 0 };
        energyValues = new List<int>(2){ 0, 0 };
        housesValues = new List<int>(3){0,0,0};
        peopleValues = new List<int>(4){0,0,0,0};

        UIController.Instance.energyBar.gameObject.SetActive(false);
        UIController.Instance.moneyBar.gameObject.SetActive(true);
    }

    void Update()
    {
        if (timeToStart == 0 && floodState == FloodState.COMING)
        {
            FloodStart();
        }

        if (floodState == FloodState.COMING)
        {
            timeToStart = Mathf.Max(0, timeToStart - Time.deltaTime);
            UIController.time = Mathf.FloorToInt(timeToStart);
        } else if (floodState == FloodState.STARTED)
        {
            energy = Mathf.Clamp(energy + Time.deltaTime * energyRechargeSpeed, 0, maxEnergy);

        }

        moneyValues [0] = Mathf.FloorToInt(money);
        moneyValues [1] = 0;

        energyValues [0] = Mathf.FloorToInt(energy);
        energyValues [1] = 0;

        housesValues [0] = housesNormal;
        housesValues [1] = housesFlooded;
        housesValues [2] = housesDestroyed;

        peopleValues [0] = peopleNormal;
        peopleValues [1] = peopleEvacuated;
        peopleValues [2] = peopleInjured;
        peopleValues [3] = peopleKilled;

        UIController.Instance.moneyBar.UpdateValues(moneyValues, Mathf.FloorToInt(startingMoney));
        UIController.Instance.energyBar.UpdateValues(energyValues, Mathf.FloorToInt(maxEnergy));
        UIController.Instance.buildingBar.UpdateValues(housesValues, housesNormal + housesFlooded + housesDestroyed);
        UIController.Instance.peopleBar.UpdateValues(peopleValues, peopleNormal + peopleEvacuated + peopleInjured + peopleKilled);
    }

    public void FloodStart()
    {
        Debug.Log("Flood started");
        timeToStart = 0;
        UIController.time = Mathf.FloorToInt(timeToStart);
        floodState = FloodState.STARTED;

		TileManager.Instance.FloodStart();
        UIController.Instance.energyBar.gameObject.SetActive(true);
        UIController.Instance.moneyBar.gameObject.SetActive(false);
        StartCoroutine(FloodCoroutine());

        UIController.Instance.skipToFlood.gameObject.SetActive(false);
    }

    public void ActivateSelectionOutline(bool value, Vector3 position = default(Vector3))
    {
        selectionOutline.gameObject.SetActive(value);
        selectionOutline.position = position;
    }

    IEnumerator FloodCoroutine()
    {
        while (floodElapsed < floodDuration)
        {
            floodElapsed = Mathf.Clamp(floodElapsed + Time.deltaTime, 0, floodDuration);
            TileManager.Instance.sourceModifier = floodCurve.Evaluate(floodElapsed / floodDuration) * maxModifier;
			UIController.time = (int)(floodDuration - floodElapsed);
            yield return new WaitForEndOfFrame();
        }
		floodState = FloodState.ENDED;
		UIController.Instance.endScreen.gameObject.SetActive(true);
    }
}
