using Intrepid.Theatre.Curtain;
using Intrepid.Utilities;
using UnityEngine;

namespace Intrepid.Core.Curtain.Actions
{
    public class CurtainFadeOpeningAction : AbstractCurtainAction
    
    {
        [SerializeField] private float timeToOpen = 2.5f;
        [SerializeField] private CanvasGroup canvasGroup;
        
        public override bool IsFinished()
        {
            return canvasGroup.alpha <= 0;
        }

        private Timer Timer { get; } = new();

        private void Update()
        {
            if (!Timer.IsRunning) return;
            Timer.Update(Time.deltaTime);
            if (!Timer.Check(timeToOpen))
            {
                canvasGroup.alpha = 1f - Mathf.Clamp01((float)Timer.RunningTime / timeToOpen);
                return;
            }
            StateMachine.RaiseCommand(CurtainCommandType.Done);
        }

        public override void Setup()
        {
            Timer.Start();
            canvasGroup.alpha = 1f;
        }

        public override void TearDown()
        {
            Timer.Stop();
            canvasGroup.alpha = 0f;
        }
    }
}