using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;

public class UIController : MonoBehaviour
{

  public TextMeshProUGUI threadLeftText;

  public int startWarning = 5;
  public Color normalColor = Color.white;
  public Color warningColor = Color.red;
  public Color gameOverColor = Color.black;

  public TextMeshProUGUI loseText;
  public string loseOutOfBoundString;
  public string loseThreadString;

  public CanvasGroup winScreen,loseScreen,blackScreen,pauseScreen;

  private Vector2 scaleVel, originalScale;
  public Vector2 jiggleTo = Vector3.one;

  bool _winScreenUp   = false;
  bool _loseScreenUp  = false;
  bool _blackScreenUp = false;
  bool _pauseUp       = false;

  bool _restartPending = false;
  bool _mainMenuPending = false;
  bool _countdown      = false;

  bool lastLink = false;
  public float _animSpeed = 2.0f;
  public float _delayBeforeBegin = 3.0f;
  public static bool _isPaused = false;

  public string mainMenuName = "MainMenu";

  // Start is called before the first frame update
  void Awake()
  {
    originalScale = transform.localScale;
    GameController.LinkLeft       += LinksUpdate;
    GameController.OutOfWorld     += Lose_OutOfBoundsListener;
    GameController.RanOutOfThread += Lose_RanOutOfThread;
    GameController.AllHolesFilled += GameWinEventListener;

    _isPaused      = false;
    _countdown     = true;
    _blackScreenUp = true;
    blackScreen.alpha = 1.0f;
  }

  // Update is called once per frame
  void Update()
  {
    if (_countdown){
        BlackScreenCountdown();
    }

    if (_restartPending || _mainMenuPending){
        SceneChangePendingUpdate();
    }

    if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.P))
    {
        TogglePause();
    }

    JiggleLinkTextUpdate();
    CanvasAnimate();
  }

  void TogglePause()
  {
      _isPaused = !_isPaused;
      if (_isPaused) Pause();
      else{
          Resume();
      }
  }
  public void Pause(){
      Time.timeScale = 0.0f;
      _isPaused = true;
      _pauseUp = true;
      pauseScreen.interactable = true;
      pauseScreen.blocksRaycasts = true;
  }

  public void Resume(){
      Time.timeScale = 1.0f;
      _isPaused = false;
      _pauseUp = false;
      pauseScreen.interactable = false;
      pauseScreen.blocksRaycasts = false;
  }

  void SceneChangePendingUpdate()
  {
      _blackScreenUp = true;
      if (blackScreen.alpha >= 1.0f){
          if (_restartPending){
              Time.timeScale = 1.0f;
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
          }
          else if (_mainMenuPending){
              Time.timeScale = 1.0f;
              SceneManager.LoadScene(mainMenuName);
          }
      }
  }

  void BlackScreenCountdown()
  {
      if (_delayBeforeBegin > 0.0f){
        _delayBeforeBegin -= Time.deltaTime;
      }
      else{
          _blackScreenUp = false;
          _countdown = false;
      }

  }

  void CanvasAnimate()
  {
      winScreen.alpha   = Mathf.MoveTowards(winScreen.alpha,  _winScreenUp  ? 1 : 0, _animSpeed * Time.unscaledDeltaTime);
      loseScreen.alpha  = Mathf.MoveTowards(loseScreen.alpha, _loseScreenUp ? 1 : 0, _animSpeed * Time.unscaledDeltaTime);
      blackScreen.alpha = Mathf.MoveTowards(blackScreen.alpha, _blackScreenUp  ? 1 : 0, _animSpeed * Time.unscaledDeltaTime);
      pauseScreen.alpha = Mathf.MoveTowards(pauseScreen.alpha, _pauseUp  ? 1 : 0, _animSpeed * Time.unscaledDeltaTime);

      winScreen.blocksRaycasts = _winScreenUp;
      winScreen.interactable   = _winScreenUp;

      loseScreen.blocksRaycasts = _loseScreenUp;
      loseScreen.interactable = _loseScreenUp;

      blackScreen.blocksRaycasts = _blackScreenUp;
      blackScreen.interactable = _blackScreenUp;
  }

  void JiggleLinkTextUpdate()
  {
    threadLeftText.transform.localScale = Mathfx.VectorSpring(threadLeftText.transform.localScale,
                                         ref scaleVel,
                                         originalScale, 0.1f, 20, Time.deltaTime);
  }

  void GameWinEventListener()
  {
      _winScreenUp = true;
  }

  void Lose_OutOfBoundsListener()
  {
      if (_winScreenUp == false)
      {
          _loseScreenUp = true;
          loseText.text = loseOutOfBoundString;
      }
  }

  void Lose_RanOutOfThread()
  {
      if (_winScreenUp == false)
      {
          _loseScreenUp = true;
          loseText.text = loseThreadString;
      }
  }

  public void RestartLevel(){
      _restartPending = true;
  }

  public void MainMenu(){
      _mainMenuPending = true;
  }


  void JiggleLinksText()
  {
    threadLeftText.transform.localScale = jiggleTo;
  }


  void LinksUpdate(int linkLeft)
  {
    if (threadLeftText)
    {

      if (linkLeft > 5)
      {
        threadLeftText.text = linkLeft.ToString() + " Links left";
        threadLeftText.color = normalColor;
        JiggleLinksText();
        lastLink = false;
      }

      else if (linkLeft > 0 && linkLeft <= 5)
      {
        threadLeftText.text = linkLeft.ToString() + " Links left";
        threadLeftText.color = warningColor;
        JiggleLinksText();
        lastLink = false;
      }

      else if (linkLeft == 0)
      {
        threadLeftText.text = "Last Link Left!";
        threadLeftText.color = warningColor;
        JiggleLinksText();
        lastLink = true;
      }

      else if (linkLeft < 0)
      {
        threadLeftText.text = "Game Over";
        threadLeftText.color = gameOverColor;
        JiggleLinksText();
        lastLink = false;
      }

    }
  }


}
