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

namespace Intrepid.Core.Curtain.Actions
{
    public class CurtainZoomOpeningAction : AbstractCurtainAction
    
    {
        [SerializeField] private float timeToOpen = 1f;
        [SerializeField] private RectTransform rectTransform;

        private float Size { get; set; } 
        
        public override bool IsFinished()
        {
            return Size <= 0;
        }

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

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

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

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