using QuizPleaseTest.Common.UI;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

namespace QuizPleaseTest.Boot.UI
{
    public class LoadingUIView : UIView<LoadingUIViewModel>
    {
        [field: SerializeField] public Slider ProgressSlider { get; private set; }

        private CompositeDisposable _disposables;

        public override void Initialize()
        {
            base.Initialize();

            _disposables?.Dispose();
            _disposables = new CompositeDisposable();

            SetProgress(ViewModel.Progress.Value);
            ViewModel.Progress
                .Subscribe(SetProgress)
                .AddTo(_disposables);
        }

        public override void Release()
        {
            _disposables?.Dispose();
            _disposables = null;
            base.Release();
        }

        private void SetProgress(float progress)
        {
            if (ProgressSlider == null)
            {
                return;
            }

            ProgressSlider.value = Mathf.Clamp01(progress);
        }
    }
}
