using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using DG.Tweening;
using UnityEditor;

public class SceneLoad : MonoBehaviour {
  public bool DeathReload;
  public string sceneName;

  private bool triggered = false;

  private Transform golem;

  private void Awake() {
    golem = GameObject.FindGameObjectWithTag("Golem").transform;
  }

  private void OnTriggerStay(Collider other) {
    var playerController = other.GetComponent<PlayerController>();
    if (playerController != null) {
      TryTrigger(playerController);
    }
  }

  private void TryTrigger(PlayerController player) {
    if (triggered) {
      return;
    }
    if (Vector3.Distance(player.transform.position, golem.position) > 15f) {
      UI.Instance.ShowInfo("You feel you shouldn't leave the golem behind...");
    } else {
      triggered = true;
      player.Frozen = true;
      UI.Instance.FadeOutScene().OnComplete(HandleFadeDone);
    }
  }

  private void HandleFadeDone() {
    if (DeathReload) {
      SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    } else {
      SceneManager.LoadScene(sceneName);
    }
  }
}
