using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.Rendering.DebugUI.Table;

public class WeatherAndTime : MonoBehaviour
{
    public GameObject Rain;
    private Coroutine updateTime;
    // Start is called before the first frame update
    void Start()
    {
        updateTime = StartCoroutine(setTimeEveryFrame());
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void Sunny()
    {
        Enviro.EnviroManager.instance.Weather.ChangeWeather("Clear Sky");
        Rain.SetActive(false);
    }
    public void Sprinkle()
    {
        Enviro.EnviroManager.instance.Weather.ChangeWeather("Rain");
        Rain.SetActive(true);
        ParticleSystem.EmissionModule emission = Rain.GetComponent<ParticleSystem>().emission;
        emission.rateOverTime = 2000f;
    }
    public void ModerateRain()
    {
        Enviro.EnviroManager.instance.Weather.ChangeWeather("Rain");
        Rain.SetActive(true);
        ParticleSystem.EmissionModule emission = Rain.GetComponent<ParticleSystem>().emission;
        emission.rateOverTime = 5000f;
    }
    public void HeavyRain()
    {
        Enviro.EnviroManager.instance.Weather.ChangeWeather("Rain");
        Rain.SetActive(true);
        ParticleSystem.EmissionModule emission = Rain.GetComponent<ParticleSystem>().emission;
        emission.rateOverTime = 10000f;
    }
    public void Storm()
    {
        Enviro.EnviroManager.instance.Weather.ChangeWeather("Storm");
        Rain.SetActive(true);
        ParticleSystem.EmissionModule emission = Rain.GetComponent<ParticleSystem>().emission;
        emission.rateOverTime = 20000f;
    }

    public void Snow()
    {
        Enviro.EnviroManager.instance.Weather.ChangeWeather("Snow");
        Rain.SetActive(false);
    }
    public void Cloudy()
    {
        Enviro.EnviroManager.instance.Weather.ChangeWeather("Cloudy 1");
        Rain.SetActive(false);
    }

    public void SetTime(int hour)
    {
        StopCoroutine(updateTime);
        Enviro.EnviroManager.instance.Time.hours = hour;
    }
    public void RealTime()
    {
        if (updateTime != null)
        {
            StopCoroutine(updateTime);
        }
        updateTime=StartCoroutine(setTimeEveryFrame());
    }
    IEnumerator setTimeEveryFrame()
    {
        while (true)
        {
            System.DateTime now = System.DateTime.Now;
            //string timeStr = now.ToString("yyyy-MM-dd HH:mm:ss");
            //Debug.Log(timeStr);
            Enviro.EnviroManager.instance.Time.seconds = now.Second;
            Enviro.EnviroManager.instance.Time.minutes = now.Minute;
            Enviro.EnviroManager.instance.Time.hours = now.Hour;
            Enviro.EnviroManager.instance.Time.days = now.Day;
            Enviro.EnviroManager.instance.Time.months = now.Month;
            Enviro.EnviroManager.instance.Time.years = now.Year;
            yield return null;
        }
        
    }
}
