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

public class ScrollViewSlider : MonoBehaviour
{
    [SerializeField] private ScrollRect scrollRect;
    [SerializeField] private Slider verticalSlider;
    [SerializeField] private Slider.Direction direction = Slider.Direction.BottomToTop;

    private void Start()
    {
        verticalSlider.direction = direction;
        if (direction == Slider.Direction.BottomToTop || direction == Slider.Direction.TopToBottom)
        {
            verticalSlider.OnValueChangedAsObservable().Subscribe(x =>
            {
                scrollRect.verticalNormalizedPosition = x;
            }).AddTo(this);
            scrollRect.OnValueChangedAsObservable().Subscribe(x =>
            {
                verticalSlider.value = x.y;
            }).AddTo(this);
            scrollRect.verticalNormalizedPosition = 1;
        }
        else
        {
            verticalSlider.OnValueChangedAsObservable().Subscribe(x =>
            {
                scrollRect.horizontalNormalizedPosition = x;
            }).AddTo(this);
            scrollRect.OnValueChangedAsObservable().Subscribe(x =>
            {
                verticalSlider.value = x.x;
            }).AddTo(this);
            scrollRect.horizontalNormalizedPosition = 0;
        }
    }
}
