using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ImageSizeChange : MonoBehaviour
{
    [SerializeField] private Slider sliderMin, sliderMax;
    [SerializeField] private Image imageMono, imageSpectrum;
    [SerializeField] private RoscanModelUnity model;
    private float sliderMaxValue, sliderMinValue, maxWidth, minWidth, height = 0;
    private Vector3 posMaxValue, posMinValue, posMaxValueMeas;


    private void Start()
    {
        sliderMaxValue = sliderMin.maxValue;
        sliderMinValue = sliderMin.minValue;
        maxWidth = imageMono.rectTransform.rect.width;
        minWidth = 2; //measured
        posMaxValue = imageMono.rectTransform.localPosition;
        posMinValue = new Vector3(-242.16f, posMaxValue.y, posMaxValue.z); //measured
        posMaxValueMeas = new Vector3(240f, posMaxValue.y, posMaxValue.z); //measured 
        height = imageMono.rectTransform.rect.height;

        ChangeImage();
        ChangeImagetwoSides();
    }


    /// <summary>Change width of image (mono color image) by value of ONE slider (sliderMin) </summary>
    public void ChangeImage()
    {
        if (maxWidth == minWidth)
            return;

        float tempWidth = ((maxWidth - minWidth) * ((sliderMin.value - sliderMinValue) / (sliderMaxValue - sliderMinValue))) + minWidth;

        imageMono.rectTransform.sizeDelta = new Vector2(tempWidth, height);

        //interpolation between width of image and positions (x) of center of image
        float tempXPos = ((posMaxValue.x - posMinValue.x) * ((tempWidth - minWidth) / (maxWidth - minWidth))) + posMinValue.x;

        imageMono.rectTransform.localPosition = new Vector3(tempXPos, posMaxValue.y, posMaxValue.z);
    }

    /// <summary>Change wifth of image (any color spectrum) by value of both sliders (sliderMin and sliderMax) </summary>
    public void ChangeImagetwoSides()
    {
       if (maxWidth == minWidth)
            return;
 

        //interpolation between width of image and scale of slider (max)
        float tempWidthMax = ((maxWidth - minWidth) * ((sliderMax.value - sliderMinValue) / (sliderMaxValue - sliderMinValue))) + minWidth;
        //interpolation between width of image and scale of slider (min)
        float tempWidthMin = ((maxWidth - minWidth) * ((sliderMin.value - sliderMinValue) / (sliderMaxValue - sliderMinValue))) + minWidth;

        float tempWidth = tempWidthMax - tempWidthMin;

        imageSpectrum.rectTransform.sizeDelta = new Vector2(tempWidth, height);

        //interpolation between slider value (max) and positions (x) of center of image
        float tempXPosMax = ((posMaxValueMeas.x - posMinValue.x) * ((sliderMax.value - sliderMinValue) / (sliderMaxValue - sliderMinValue))) + posMinValue.x;
        //interpolation between slider value (min) and positions (x) of center of image
        float tempXPosMin = ((posMaxValueMeas.x - posMinValue.x) * ((sliderMin.value - sliderMinValue) / (sliderMaxValue - sliderMinValue))) + posMinValue.x;

        float tempXPos = (tempXPosMax+tempXPosMin)/2;

        imageSpectrum.rectTransform.localPosition = new Vector3(tempXPos, posMaxValue.y, posMaxValue.z);

    }
}
