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

namespace Intrepid.Core.Curtain.Actions
{
    public class CurtainZoomClosingAction : AbstractCurtainAction
    
    {
        [SerializeField] private float timeToClose = 1f;
        [SerializeField] private RectTransform rectTransform;
        
        private float Size { get; set; }
        
        public override bool IsFinished()
        {
            return Size >= 1;
        }
        
        private Timer Timer { get; } = new();

        private void Update()
        {
            if (!Timer.IsRunning) return;
            Timer.Update(Time.deltaTime);
            if (!Timer.Check(timeToClose))
            {
                Size = Mathf.Clamp01((float)Timer.RunningTime / timeToClose);
                rectTransform.localScale = new Vector2(Size, Size);
                return;
            }
            StateMachine.RaiseCommand(CurtainCommandType.Done);
        }

        public override void Setup()
        {
            Timer.Start();
            Size = 0f;
            rectTransform.localScale = Vector2.zero;
        }

        public override void TearDown()
        {
            Timer.Stop();
            Size = 1f;
            rectTransform.localScale = Vector2.one;
        }
    }
}