using System;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

namespace MyGame.Scripts
{
    [RequireComponent(typeof(Button))]
    public class FooterButton : MonoBehaviour
    {
        [SerializeField] private Sprite onSprite;
        [SerializeField] private Sprite offSprite;
        [SerializeField] private GameObject onObject;
        [SerializeField] private GameObject offObject;
        private static readonly int DelayCount = 2;
        private Button button;
        public IObservable<Unit> ClickObservable => button.OnClickAsObservable().TakeUntilDestroy(this);

        private void Awake()
        {
            button = GetComponent<Button>();
        }

        private void Start()
        {
            button.OnClickAsObservable().Subscribe(_ =>
            {
                
            }).AddTo(this);
        }

        public void SetSelected(bool selected)
        {
            button.interactable = !selected;
            button.image.sprite = selected ? onSprite : offSprite;
            Observable.TimerFrame(DelayCount)
                .Subscribe(_ =>
                {
                    onObject.SetActive(selected);
                    offObject.SetActive(!selected);
                }).AddTo(this);
        }
    }
}