using System;
using UnityEngine;
using UnityEngine.UI;

public class ThermoMeter : MonoBehaviour
{
    [SerializeField] private Slider slider;
    [SerializeField] private RectTransform coldCondition;
    [SerializeField] private RectTransform yellowCondition;
    [SerializeField] private RectTransform hotCondition;

    private void Start()
    {
        slider.value = 0f;
    }

    // 温度の目盛りの幅を設定
    public void SetScale(float coldValue, float hotValue)
    {
        var width = (slider.transform as RectTransform).sizeDelta.x;
        var yellowSize = yellowCondition.sizeDelta;
        yellowSize.x = Mathf.Lerp(0, width, hotValue - coldValue);
        yellowCondition.sizeDelta = yellowSize;
        yellowCondition.transform.localPosition = new Vector3(width * (hotValue + coldValue) / 2 - width / 2, 0f);
        var coldSize = coldCondition.sizeDelta;
        coldSize.x = Mathf.Lerp(0, width, coldValue);
        coldCondition.sizeDelta = coldSize;
        var hotSize = hotCondition.sizeDelta;
        hotSize.x = Mathf.Lerp(0, width, 1 - hotValue);
        hotCondition.sizeDelta = hotSize;
    }

    public void SetValue(float value)
    {
        slider.value = value;
    }
}
