using System.Collections;
using System.Collections.Generic;
using Game;
using UnityEngine;
using Audio;

namespace InteractiveModules
{

    public class Radio : InteractiveModule
    {
        [SerializeField] private GameScreenAsset nextScreen;
        [SerializeField] private MusicManager musicManager;
        [SerializeField] private AudioClip silentClip;
        [SerializeField] private AudioClip musicClip;
        private GameObject radioOn;
        private bool played;

        public override void Activate(ScreenController screenController)
        {
            base.Activate(screenController);
            screenController.SetBackgroundColor(ColorType.Grey);
            musicManager.MusicPlayer.PlayMusic(silentClip);
        }

        // Start is called before the first frame update
        void Start()
        {
            radioOn = transform.Find("RadioOn").gameObject;
            played = false;
        }

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

        }

        public void OnMouseDown()
        {
            if (!played)
            {
                played = true;
                radioOn.GetComponent<SpriteRenderer>().color = Color.white;
                //GetComponent<AudioSource>().Play();
                musicManager.MusicPlayer.PlayMusic(musicClip);
                Invoke("SwitchScene", 3f);
                currentScreenController.SetBackgroundColor(ColorType.Green);
            }
        }

        /*public void FadeOut()
        {
            float fadeOutTime = 1f;
            radioOn.LeanColor(new Color(1f, 1f, 1f, 0f), fadeOutTime);
            //StartCoroutine(FadeOutSound(GetComponent<AudioSource>(), fadeOutTime));
            currentScreenController.SetBackgroundColor(ColorType.Grey, fadeOutTime);

            Invoke("SwitchScene", 3f + fadeOutTime);
        }*/
        
        public void SwitchScene()
        {
            currentScreenController.LoadScreen(nextScreen);
        }

        /*public IEnumerator FadeOutSound(AudioSource audioSource, float duration)
        {
            float startVolume = audioSource.volume;

            while (audioSource.volume > 0)
            {
                audioSource.volume -= startVolume * Time.deltaTime / duration;

                yield return null;
            }

            audioSource.Stop();
            audioSource.volume = startVolume;
        }*/
    }
}
