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

public class TimeDisplayHandler : MonoBehaviourPunCallbacks {

	public KartController myRacer;
	public TrackManagerScript manager;
	public GameObject raceAgainButton;
	public TextMeshProUGUI currentLapTime, lastLapTime, bestLapTime, totalRaceTime, lapCount, racePlacement;
	public float startTimer = 3f, raceTime = 0f, lastTime = 0f, bestTime = 0f, raceCompleteTime = 0f;
	public List<Color> placeColors;

    // Start is called before the first frame update
    void Start () {
		if (PhotonNetwork.IsConnected && SceneManager.GetActiveScene().name != "RKR-Lobby" && SceneManager.GetActiveScene().name != "Esports-Lobby") {
			racePlacement.gameObject.SetActive (true);
		} else {
			racePlacement.gameObject.SetActive (false);
		}
    }

    // Update is called once per frame
    void Update () {
		return;
		if (SceneManager.GetActiveScene ().name != "PhotonTesting 1" && SceneManager.GetActiveScene ().name != "RKR-Lobby" && SceneManager.GetActiveScene().name != "Esports-Lobby") {

			// I'm using one because lapCount is a different variable here, so 1 needs to be the current lap the racer is on

			lapCount.SetText ((LapCount (1)));

			if (1 > manager.totalLaps) {
				raceCompleteTime = raceTime;
				totalRaceTime.SetText (FormatTime ("Race Complete Time:", raceCompleteTime));

				if (!raceAgainButton.activeInHierarchy) {
					raceAgainButton.SetActive (true);
				}
				//sphere.gameObject.SetActive (false);
				//kartNormal.GetChild (0).gameObject.SetActive (false);
				transform.RotateAround (transform.position, transform.up, 0.1f);
			}

			//if (!startRace || lapCount > manager.totalLaps) {
				//return;
			//}


			if (currentLapTime.gameObject.activeInHierarchy) {
				currentLapTime.SetText ("0:00:00");
				if (PhotonNetwork.IsConnected) {
					raceTime += (float)PhotonNetwork.Time;
				} else {
					raceTime += Time.deltaTime;
				}
			}

			currentLapTime.SetText (FormatTime ("Current Lap Time:", raceTime));
		}
    }

	void FindMyRacer () {
		KartController[] karts = FindObjectsOfType<KartController> ();

		if (PhotonNetwork.IsConnected) {
			foreach (KartController kart in karts) {
				if (kart.photonView.IsMine) {
					myRacer = kart;
					return;
				}
			}
		} else {
			myRacer = karts [0];
		}
	}

	void HandleLapDisplay (float currentTime) {
		if (currentTime != 0f) {
			lastTime = currentTime - lastTime;
			// Last Lap
			lastLapTime.SetText (FormatTime ("Last Lap Time:", currentTime));
			// Best Lap
			if ((bestTime != 0f && bestTime > currentTime) || bestTime == 0f) {
				bestLapTime.SetText (FormatTime ("Best Lap Time:", currentTime));
				bestTime = currentTime;
			}
		} else {
			if (bestTime != 0f && 1 == 1) {
				bestLapTime.SetText (FormatTime ("Best Lap Time:", bestTime));
			}
		}
	}

	string LapCount (int lap) {
		if (lap > manager.totalLaps) {
			lap = manager.totalLaps;
		}
		string lapText = "Lap " + lap.ToString () + "/" + manager.totalLaps.ToString ();
		return lapText;
	}

	string FormatTime (string timeStampText, float time) {
		int intTime = (int)time;
		int minutes = (int)time / 60;
		int seconds = (int)time % 60;
		int milliseconds = (int)(time * 1000) % 1000;
		string timeText = timeStampText + "\n" + string.Format ("{0:00}:{1:00}:{2:000}", minutes, seconds, milliseconds);
		return timeText;
	}


	public void RaceAgain () {
		SceneManager.LoadScene (SceneManager.GetActiveScene ().name, LoadSceneMode.Single);
	}
}
