using DG.Tweening;
using UniRx;
using UnityEngine;

namespace UI_and_Audio
{
    public class FireExtinguisherVisualCue : MonoBehaviour
    {
        [SerializeField] private SpriteRenderer fireExtinguisherIcon;
        [SerializeField] private SpriteRenderer background;
        
        [SerializeField] private float pulseDuration;

        [SerializeField] private Color fireExtinguisherIconIntialColor;
        [SerializeField] private Color innerSquareInitialColor;
        
        private Tween fireExtinguisherIconTween;
        private Tween innerSquareTween;
        
        private void Start()
        {
            StopReturnCue();
            ExtinguisherHouse.Instance.isTargetInside
                .Skip(1) // ignore first value
                .Subscribe(isInside =>
                {
                    if (!isInside) ShowExtinguisherReturnCue();
                    else
                    {
                        AudioManager.Instance.PlayInsertSound();
                        StopReturnCue();
                    }
                })
                .AddTo(this);
        }

        public void ShowExtinguisherReturnCue()
        {
            var fECol = fireExtinguisherIconIntialColor;
            var iSCol = innerSquareInitialColor;

            fireExtinguisherIcon.color = fECol;
            background.color = iSCol;
  
            
            fireExtinguisherIconTween = fireExtinguisherIcon.DOColor(new Color(fECol.r, fECol.g, fECol.b, 0.85f), pulseDuration*2)
                .SetLoops(-1, LoopType.Yoyo)
                .SetEase(Ease.InOutSine)
                .SetUpdate(true);
            
            innerSquareTween = background.DOColor(new Color(iSCol.r, iSCol.g, iSCol.b, 0.0f), pulseDuration)
                .SetLoops(-1, LoopType.Yoyo)
                .SetEase(Ease.InOutSine)
                .SetUpdate(true);
        }

        public void StopReturnCue()
        {
            fireExtinguisherIconTween?.Pause();
            innerSquareTween?.Pause();

            var fECol = fireExtinguisherIcon.color;
            var iSCol = background.color;
            
            fireExtinguisherIcon.color = new Color(fECol.r, fECol.g, fECol.b, 0.0f);
            background.color = new Color(iSCol.r, iSCol.g, iSCol.b, 0.0f);
            
        }
        
    }
}
