using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using Assets.Scripts.Managers;
using Assets.Scripts.Constants;

public class GameManager : MonoSingleton <GameManager> {

	#region variables
    private static CameraAngle _currentAngle = CameraAngle.FrontAngle;
    private static GameTime _currentGameTime = GameTime.Day;

    [SerializeField]
    private GameObject _dayPlayer;

    [SerializeField]
    private GameObject _nightPlayer;

    private NightClamp[] _nightClamps;
	private DayClamp[] _dayClamps;

    private GameObject _dayUI;
    private GameObject _nightUI;
    #endregion

    #region getters and setters
    public static CameraAngle CurrentAngle
    {
        get
        {
            return _currentAngle;
        }
        set
        {
            _currentAngle = value;
        }
    }

    public static GameTime CurrentGameTime
    {
        get
        {
            return _currentGameTime;
        }
        set
        {
            _currentGameTime = value;
            //Instance.SwitchCharacter ();
            Instance.ToggleClamps ();
            Instance.FixUIElements ();
			Instance.GetComponent <TileManager> ().SwitchTiles (value);
        }
    }
    #endregion


    #region events
    protected override void AwakeEx () {

    	SceneManager.sceneLoaded += Setup;

    }

    void Update () {
        if (Input.GetKey(KeyCode.LeftShift))
        {
            DayNightManager.SetDay();
        }
        else if(Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.RightControl))
        {
            DayNightManager.SetNight();
        }
    }
    #endregion


    #region methods
	void Setup (Scene scene, LoadSceneMode lsm) {

		_nightClamps = GameObject.FindObjectsOfType <NightClamp> ();
		_dayClamps = GameObject.FindObjectsOfType <DayClamp> ();
		ToggleClamps ();

		_dayUI = GameObject.Find ("Day Text");
		_nightUI = GameObject.Find ("Night Text");

		FixUIElements ();

    }

    public void SwitchCharacter () {

    	if (_dayPlayer != null) _dayPlayer.SetActive (CurrentGameTime == GameTime.Day);
		if (_nightPlayer != null) _nightPlayer.SetActive (CurrentGameTime == GameTime.Night);
    	
    }

    void ToggleClamps () {
		foreach (NightClamp nc in _nightClamps) {
			nc.gameObject.SetActive (CurrentGameTime == GameTime.Night);
		}

		foreach (DayClamp dc in _dayClamps) {
			dc.gameObject.SetActive (CurrentGameTime == GameTime.Night);
		}
    }

    void FixUIElements () {

    	_dayUI.GetComponent <Text> ().color = new Color (1f, 1f, 1f, (CurrentGameTime == GameTime.Day) ? 1f : 0.25f);
		_nightUI.GetComponent <Text> ().color = new Color (1f, 1f, 1f, (CurrentGameTime == GameTime.Night) ? 1f : 0.25f);
    	
    }
    #endregion
    	
}
