using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SliderLimits : MonoBehaviour
{
    [SerializeField] private Slider Slider;
    [SerializeField] private Slider Slider2;
    [SerializeField] private RoscanModelUnity model;
    [SerializeField] private Text sliderText1, sliderText2;
    [SerializeField] private Button autoRangeButton;

    // initialization
    private void Start()
    {

        Slider.value = model.LayerValueMin;
        Slider2.value = model.LayerValueMax;

        model.SetPalleteMaxAsPercentage();
        model.SetPalleteMinAsPercentage();

        Slider.maxValue = model.PaletteMax;
        Slider.minValue = model.PaletteMin;
        Slider2.maxValue = model.PaletteMax;
        Slider2.minValue = model.PaletteMin;

        sliderText1.text = Slider.value.ToString();
        sliderText2.text = Slider2.value.ToString();

        Slider.onValueChanged.AddListener(delegate { ValueMax(); });
        Slider2.onValueChanged.AddListener(delegate { ValueMin(); });
    }

 /// <summary> Upper limit of sliders and every move changes the text and color of model</summary>
    private void ValueMax()
    {
        if (Slider.value > Slider2.value)
            Slider.value = Slider2.value;

        model.PaletteMin = Slider.value;
        model.PaletteMax = Slider2.value;
        model.ResetColorPoints(model.points);

        sliderText1.text = Slider.value.ToString();
        sliderText2.text = Slider2.value.ToString();
    }
 
 /// <summary> Lower limit of sliders and every move changes the text and color of model</summary>
    private void ValueMin()
    {
        if (Slider.value > Slider2.value)
            Slider2.value = Slider.value;

        model.PaletteMin = Slider.value;
        model.PaletteMax = Slider2.value;
        model.ResetColorPoints(model.points);

        sliderText1.text = Slider.value.ToString();
        sliderText2.text = Slider2.value.ToString();
    }

/// <summary>After clicking the button, Auto range sliders to default values</summary>
    public void AutoRange()
    {
        Slider.value = model.LayerValueMin;
        Slider2.value = model.LayerValueMax;

        model.PaletteMin = Slider.value;
        model.PaletteMax = Slider2.value;

        sliderText1.text = Slider.value.ToString();
        sliderText2.text = Slider2.value.ToString();

        model.ResetColorPoints(model.points);
    }
}

