﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using Photon.Pun;

public class CircleTimeDisplay : MonoBehaviour {

    public TMP_Text circleTimerGameplayText;
    public TMP_Text circleTimerSpectatorText;
    public Image backgroundColor;

    float nextCircleTime;
    bool waiting;


    public Color waitingColor;
    public Color activeColor;


    // Use this for initialization
    void Start () {
        nextCircleTime = -1f;
        waiting = true;
    }
	
	// Update is called once per frame
	void Update () {
        if (waiting)
            backgroundColor.color = waitingColor;
        else
            backgroundColor.color = activeColor;

        if(nextCircleTime != -1) {
            float currentTime = 0f;
            if (PhotonNetwork.IsConnected) {
                currentTime = (float)PhotonNetwork.Time;
            }
            else {
                //currentTime = Time.unscaledTime;
            }

            float timeLeft = nextCircleTime - currentTime;
            if (timeLeft < 0)
                timeLeft = 0;
            string min = Mathf.Floor(timeLeft / 60).ToString("0"); // I'm assuming we'll never be waiting for more than 9 minutes
            string sec = Mathf.Floor(timeLeft % 60).ToString("00");
            string clockText = min + ":" + sec;

            if (circleTimerGameplayText)
                circleTimerGameplayText.SetText(clockText);
            if(circleTimerSpectatorText)
                circleTimerSpectatorText.SetText(clockText);
        }
	}

    public void SetNextStormTime(float nextStormTime) {
        nextCircleTime = nextStormTime;
    }

    public void SetStormWaiting(bool _waiting) {
        waiting = _waiting;
    }
}
