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

namespace InteractiveModules
{
    public class TvScreen : InteractiveModule
    {
        [SerializeField] private GameScreenAsset nextScreen;

        [SerializeField] private Sprite[] NoiseSprites;

        private bool isOn;
        IEnumerator noiseC;
        private Sprite originalSprite;
        public override void Activate(ScreenController screenController)
        {
            base.Activate(screenController);
            screenController.SetBackgroundColor(ColorType.Green);
        }
            // Start is called before the first frame update
            void Start()
        {
            isOn = false;
            noiseC = NoiseCoroutine(0.1f);
            originalSprite = GetComponent<SpriteRenderer>().sprite;
        }

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

        }

        public IEnumerator NoiseCoroutine(float wait)
        {
            int i = 0;
            SpriteRenderer renderer = GetComponent<SpriteRenderer>();
            while (true)
            {
                renderer.sprite = NoiseSprites[i];
                i = (i + 1) % NoiseSprites.Length;
                yield return new WaitForSeconds(wait);
            }
        }

        public void SwitchScene()
        {
            currentScreenController.LoadScreen(nextScreen);
        }

        public void OnMouseDown()
        {
            if (!isOn)
            {
                StartCoroutine(noiseC);
                GetComponent<AudioSource>().Play();
            }
            else
            {
                StopCoroutine(noiseC);
                GetComponent<SpriteRenderer>().sprite = originalSprite;
                GetComponent<AudioSource>().Stop();

                Invoke("SwitchScene", 1f);
            }
            isOn = !isOn;
        }
    }
}
