using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class Window : MonoBehaviour
{
    public static Window Instance;
    
    public SpriteRenderer sky;
    public Color nightColour;
    public Color dayColour;

    private bool windowIsOpen = false;
    private bool curtainIsOpen = true;
    public GameObject windowOpen;
    public GameObject windowClosed;
    public GameObject curtainOpen;
    public GameObject curtainClosed;

    public AudioSource ambientNoise;

    public bool lightIsActive = false;
    public float mttLight = 30f;
    public float timeUntilLightVar = 15f;
    private float timeUntilLight;
    public float lightTime;
    private bool lightOn = false;
    
    public AudioClip curtainOpenSound;
    public AudioClip curtainCloseSound;

    public AudioClip switchOn;
    public AudioClip switchOff;

    public DateTime sunriseTime = Timer.startTime + TimeSpan.FromHours(6);
    private bool sunriseStarted = false;
    private float secondsSinceSunriseStarted;
    private float sunriseDuration = Timer.gameHourInS;

    public float mtt_slide_open = 20f;
    public float slide_open_time_dev = 10f;

    public float time_until_slide_open;
    
    private float maxLightLevelContribution = 0.7f;

    private void Awake()
    {
        Instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        Light.contributions.Add(gameObject, 0f);
        Thermometer.contributions.Add(gameObject, 0f);
    }

    public void reset()
    {
        sunriseStarted = false;
        secondsSinceSunriseStarted = 0;
        timeUntilLight = 0;
        lightTime = 0;
        timeUntilLight = Random.Range(mttLight - timeUntilLightVar, mttLight + timeUntilLightVar);
        windowIsOpen = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (Timer.Instance.gameTime > sunriseTime)
        {
            sunriseStarted = true;
            secondsSinceSunriseStarted += Time.deltaTime;
        }

        float t = secondsSinceSunriseStarted / sunriseDuration;

        Color c = Color.Lerp(nightColour, dayColour, t);
        c.a = 1f;
        sky.color = c;

        float maxLight = maxLightLevelContribution * (curtainIsOpen ? 1f : .2f);

        Light.contributions[gameObject] = Mathf.Lerp(0f, maxLight, t);

        windowOpen.SetActive(windowIsOpen);
        windowClosed.SetActive(!windowIsOpen);
        
        if (curtainIsOpen)
        {
            curtainOpen.SetActive(true);
            curtainClosed.SetActive(false);
        }
        else
        {
            curtainOpen.SetActive(false);
            curtainClosed.SetActive(true);

            time_until_slide_open -= Time.deltaTime;

            if (time_until_slide_open <= 0)
            {
                openCurtain();
            }
        }

        lightTime -= Time.deltaTime;
        timeUntilLight -= Time.deltaTime;

        if (timeUntilLight <= 0 && lightIsActive && !sunriseStarted)
        {
            timeUntilLight = Random.Range(mttLight - timeUntilLightVar, mttLight + timeUntilLightVar);
            lightTime = 5f;
            AudioManager.playClip(switchOn);
        }

        if (0 < lightTime && lightTime < 4)
        {
            sky.color = Color.white;
            Light.contributions[gameObject] = maxLight;
            lightOn = true;
        }

        else if (lightTime < 0 && lightOn)
        {
            lightOn = false;
            AudioManager.playClip(switchOff);
        }
        
        float tempContribution;
            
        if (windowIsOpen)
        {
            if (sunriseStarted)
            {
                tempContribution = -.08f;
            }
            else
            {
                tempContribution = -.11f;
            }
        }
        else
        {
            if (sunriseStarted)
            {
                tempContribution = .01f;
            }
            else
            {
                tempContribution = -.015f;
            }
        }
            
        Thermometer.contributions[gameObject] = tempContribution;
    }

    public void openCurtain()
    {
        curtainIsOpen = true;
        AudioManager.playClip(curtainOpenSound);
    }

    public void closeCurtain()
    {
        curtainIsOpen = false;
        AudioManager.playClip(curtainCloseSound);

        time_until_slide_open =
            Random.Range(mtt_slide_open - slide_open_time_dev, mtt_slide_open + slide_open_time_dev);
    }
    
    public void openWindow()
    {
        windowIsOpen = true;
        ambientNoise.volume = 1.0f;
        
    }

    public void closeWindow()
    {
        windowIsOpen = false;
        ambientNoise.volume = 0.7f;
    }
}
