using UnityEngine;
using System.Collections;

public class RoundManager : MonoBehaviour {

    public delegate void RoundStart();
    public static event RoundStart OnRoundStart;

    private VoteManager voteManager;
    public string[] eventVoteTypes;
    public string[] appeasedTypes;

    private int roundNum = 0;
    private enum RoundState { Play, Altar, Dead };
    private RoundState roundState = RoundState.Play;
    private bool appeased = false;
    private int eventNum = 0;
    public int eventCount = 3;

    private EventManager eventManager;

    public float eventVoteTime = 180.0f;
    public float appeasedVoteTime = 10.0f;
    public float eventTime = 60.0f;

    private float peacefulRoundTime;

    private IEnumerator voteCoroutine;
    private IEnumerator peacefulRoundCoroutine;

    private ArtefactSpawnManager arteSpawnManager;

    // Use this for initialization
    void Start() {
        voteManager = GetComponent<VoteManager>();
        eventManager = GetComponent<EventManager>();
        arteSpawnManager = GetComponent<ArtefactSpawnManager>();
        //StartCoroutine(TempVoteTester());
        peacefulRoundTime = eventVoteTime * eventCount + eventTime;
        Altar.OnUse += AltarStart;
        StartRound();
    }

    // Update is called once per frame
    void Update() {

    }

    /**
    START ROUND
    Increments round num.
    Set round state to 'play'
    trigger a bunch of other stuff in other components
    **/
    void StartRound()
    {
        Debug.Log("Starting Round");
        roundState = RoundState.Play;
        roundNum++;
        Debug.Log("Round num: " + roundNum.ToString());
        if (roundNum > 1)
        {
            voteCoroutine = StartEventVote();
            StartCoroutine(voteCoroutine);
        }
        else
        {
            peacefulRoundCoroutine = StartPeacefulRound();
            StartCoroutine(peacefulRoundCoroutine);
        }
    }

    /**
    START PEACEFUL ROUND
    A round that does absolutely nothing.
    **/
    IEnumerator StartPeacefulRound()
    {
        Debug.Log("Start Peaceful Round");
        yield return new WaitForSeconds(peacefulRoundTime);
        Debug.Log("End Peaceful Round");
        EndGame();
    }

    /**
    START EVENT VOTE
    Start a vote for the event to happen and trigger it
    **/
    IEnumerator StartEventVote()
    {
        Debug.Log("Start Event Vote");
        eventNum++;
        voteManager.StartVote(eventVoteTypes);
        yield return new WaitForSeconds(eventVoteTime);
        string eventToTrigger = voteManager.GetVoteResult();
        // To implement when Event Manager made
        eventManager.StartEvent(eventToTrigger, appeased);
        Debug.Log(eventToTrigger);
        if (eventNum < eventCount)
        {
            voteCoroutine = StartEventVote();
            StartCoroutine(voteCoroutine);
        }
        else
        {
            Debug.Log("Last Event Timeout");
            yield return new WaitForSeconds(eventTime);
            Debug.Log("END THE GAME ALREADY");
            EndGame();
        }
    }

    /**
    END GAME
    Do stuff to end the game
    **/
    public void EndGame()
    {
        Debug.Log("End Game");
        roundState = RoundState.Dead;
    }

    /**
    ALTAR START
    Set the round state to 'altar'.
    Start appeased vote
    **/
    public void AltarStart()
    {
        Debug.Log("Altar Start");
        roundState = RoundState.Altar;
        if(voteCoroutine != null)
        {
            StopCoroutine(voteCoroutine);
        }
        if(peacefulRoundCoroutine != null)
        {
            StopCoroutine(peacefulRoundCoroutine);
        }
        voteManager.StopVote();
        StartCoroutine(StartAppeaseVote());
    }

    /**
    START APPEASE VOTE
    Starts vote to see if twitch is appeased.
    Then end the round.
    **/
    IEnumerator StartAppeaseVote()
    {
        Debug.Log("Appease Vote Start");
        voteManager.StartVote(appeasedTypes);
        yield return new WaitForSeconds(appeasedVoteTime);
        string appeasedVoteResult = voteManager.GetVoteResult();
        appeased = (appeasedVoteResult == "A");
        Debug.Log("Appeased:" + appeased.ToString());
        StartCoroutine(EndRound());
    }

    /**
    END ROUND
    Triggers a bunch of stuff and starts the round again
    **/
    IEnumerator EndRound()
    {
        Debug.Log("Ending Round");
        yield return new WaitForSeconds(1.0f);
        eventNum = 0;
        arteSpawnManager.SpawnArtefact();
        if (OnRoundStart != null)
        {
            OnRoundStart();
        }
        StartRound();
    }

    IEnumerator TempVoteTester()
    {
        while (true)
        {
            voteManager.StartVote(eventVoteTypes);
            yield return new WaitForSeconds(5.0f);
            print(voteManager.GetVoteResult());
        }
    }
}
