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

public class LobbyUI : MonoBehaviour {

    public GameObject hostStartButton;
    public GameObject hostCancelButton;

    public float countdownLength = 20f;

    public TMP_Text coutdownTimer;
    public TMP_Text playerCount;

    public Launcher launcher;

    public bool cancelCountdown;
    public bool countingDown;
    public LobbyPanelHandler handler;
    public GameGenerator gameGenerator;

    public GameObject StaticLoadingScreen;

    float loadingTime;
    float timeToWaitForLoading = 1f;


    public bool wasHost;

    void Start() {
        loadingTime = Time.unscaledTime;
        wasHost = PhotonNetwork.IsMasterClient;
    }

    void Update() {
        if(PhotonNetwork.IsMasterClient && countingDown && !wasHost) {
            PhotonDebugger.IncrementNumberOfMessages("match start time");
            // send out the random seed again 
            gameGenerator.SendRandomSeed(GameGenerator.GetOldRandomSeed());
            launcher.photonView.RPC("SetMatchStartTime", RpcTarget.AllBuffered, (float)PhotonNetwork.Time + countdownLength);
            wasHost = true;
        }
        if (PhotonNetwork.IsMasterClient && !countingDown) {
            if(Input.GetKeyDown(KeyCode.M)) {
                Host();
            }
            hostStartButton.SetActive(true);
            hostCancelButton.SetActive(false);
        }
        else if (PhotonNetwork.IsMasterClient && countingDown) {
            if (Input.GetKeyDown(KeyCode.M)) {
                Cancel();
            }
            hostStartButton.SetActive(false);
            hostCancelButton.SetActive(true);
        }
        else {
            hostStartButton.SetActive(false);
            hostCancelButton.SetActive(false);
        }
        if(PhotonNetwork.InRoom)
            playerCount.SetText("Players " + PhotonNetwork.CurrentRoom.PlayerCount + "/" + launcher.maxPlayersPerRoom);
        if (StaticLoadingScreen.activeInHierarchy && Time.unscaledTime > loadingTime) {
            StaticLoadingScreen.SetActive(false);
        }
    }

    public void Host() {
        PhotonDebugger.IncrementNumberOfMessages("match start time");
        launcher.photonView.RPC("SetMatchStartTime", RpcTarget.AllBuffered, (float)PhotonNetwork.Time + countdownLength);
        cancelCountdown = false;
    }

    public void Cancel() {
        cancelCountdown = true;
        launcher.ResetMatchStartTime();
        PhotonNetwork.CurrentRoom.IsOpen = true;
        PhotonNetwork.CurrentRoom.IsVisible = true;
    }

    public IEnumerator CountdownToStart() {
        if (countingDown)
            yield break;
        PhotonNetwork.CurrentRoom.IsOpen = false;
        PhotonNetwork.CurrentRoom.IsVisible = false;
        countingDown = true;
        coutdownTimer.gameObject.SetActive(true);
        while (!cancelCountdown && launcher.GetMatchStartTime() > -1 && (float)PhotonNetwork.Time < launcher.GetMatchStartTime()) {
            coutdownTimer.SetText((launcher.GetMatchStartTime() - (float)PhotonNetwork.Time).ToString("F0"));
            yield return null;
        }
        countingDown = false;
        coutdownTimer.gameObject.SetActive(false);
        if (cancelCountdown) {
            cancelCountdown = false;
            yield break;
        }
        StaticLoadingScreen.SetActive(true);
        loadingTime = Time.unscaledTime + timeToWaitForLoading;
        handler.SetInLobbyPanelActive();
        launcher.StartMatch();
    }
}
