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

namespace InteractiveModules
{
    public class EntryDoor : InteractiveModule
    {
        private enum WindowState
        {
            DEFAULT,
            DAMAGED,
            BROKEN,
        };

        [SerializeField] private Sprite DamagedSprite;
        [SerializeField] private Sprite BrokenSprite;

        [SerializeField] private GameScreenAsset nextScreen;

        public float FallSpeed;

        WindowState windowState;

        // Start is called before the first frame update
        void Start()
        {
            windowState = WindowState.DEFAULT;
        }

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

        }

        public void WindowClicked()
        {
            SpriteRenderer renderer = GetComponent<SpriteRenderer>();
            if (windowState == WindowState.DEFAULT)
            {
                windowState = WindowState.DAMAGED;
                renderer.sprite = DamagedSprite;

                GetComponent<AudioSource>().Play();
            }
            else if (windowState == WindowState.DAMAGED)
            {
                Transform glass = transform.Find("GlassGroup");
                glass.gameObject.SetActive(true);
                windowState = WindowState.BROKEN;
                renderer.sprite = BrokenSprite;

                for(int i = 0;i < glass.childCount; ++i)
                {
                    Rigidbody2D rb = glass.GetChild(i).GetComponent<Rigidbody2D>();
                    rb.WakeUp();
                    rb.angularVelocity = (Random.value - 0.5f) * 720f;
                }

                transform.Find("Window").GetComponent<AudioSource>().Play();
            }
            else if (windowState == WindowState.BROKEN)
            {
                Invoke("SwitchScene", 2.5f);
                Transform glass = transform.Find("GlassGroup");
                glass.GetComponent<AudioSource>().Play();
            }
        }

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