using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    private GameData data;

    private SaveManager saveManager;
    private TimeManager timeManager;
    private MapGenerator mapGenerator;

    private GameObject player;
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        //Get the player
        player = GameObject.Find("Player");
        if (player == null) return;

        //Get components
        saveManager = GetComponent<SaveManager>();
        timeManager = GetComponent<TimeManager>();
        mapGenerator = GameObject.Find("Map Generator").GetComponent<MapGenerator>();

        data = saveManager.LoadGame();
        //reset light settings
        RenderSettings.skybox.SetTexture("_Texture1", timeManager.skyboxNight);
        RenderSettings.skybox.SetTexture("_Texture2", timeManager.skyboxSunrise);
        RenderSettings.skybox.SetFloat("_Blend", 0);

        timeManager.globalLight.color = timeManager.gradientNightToSunrise.Evaluate(0);
        RenderSettings.fogColor = timeManager.globalLight.color;

        if (data != null)
        {
            SetData();
        }
        else
        {
            data = new GameData();
        }
    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.F5)) 
        {
            Save();
        }
        if (Input.GetKeyDown(KeyCode.F9)) 
        {
            //SceneManager.LoadScene("Game");
        }
    }
    void GetData()
    {
        data.playerPosition = player.transform.position;

        #region DaylightCycle
        data.minutes = timeManager.Minutes;
        data.hours = timeManager.Hours;
        data.skybox = RenderSettings.skybox;
        data.globalLightColor = timeManager.globalLight.color;
        data.globalLightRotation = timeManager.globalLight.transform.rotation;

        #endregion

        #region Stamina
        data.currentStamina = player.GetComponent<Stamina>().GetCurrentStamina();
        data.canSprint = player.GetComponent<Stamina>().GetCanSprint();
        #endregion

        #region MapGeneration
        data.seed = mapGenerator.noiseData.seed;
        #endregion
    }

    void SetData()
    {
        if (data.playerPosition != Vector3.zero)
            player.transform.position = data.playerPosition;

        #region DaylightCycle

        timeManager.Minutes = data.minutes;
        timeManager.Hours = data.hours;

        if (data.skybox != null)
            RenderSettings.skybox = data.skybox;

        if(data.globalLightColor != null)
        {
            timeManager.globalLight.color = data.globalLightColor;
            RenderSettings.fogColor = data.globalLightColor;
        } 

        if (data.globalLightRotation != null)
            timeManager.globalLight.transform.rotation = data.globalLightRotation;

        #endregion

        #region Stamina
        if(data.skybox !=null) //because !
            player.GetComponent<Stamina>().Reinit(data.currentStamina, data.canSprint);

        #endregion

        #region MapGeneration
        mapGenerator.noiseData.seed = data.seed;
        #endregion

    }

    public void Save()
    {
        GetData();
        saveManager.SaveGame(data);
    }

}
