using UnityEngine;
using UnityEngine.UI;

public class ModeChangeUI : MonoBehaviour
{
    private MeasureManager measureManager;

    [SerializeField] private Toggle Toggle_Line;
    [SerializeField] private Toggle Toggle_Rect;
    [SerializeField] private Toggle Toggle_Poly;
    [SerializeField] private Toggle Toggle_Circle;

    private void Awake()
    {
        measureManager = FindAnyObjectByType<MeasureManager>();

        Toggle_Line.onValueChanged.AddListener(OnClickLine);
        Toggle_Rect.onValueChanged.AddListener(OnClickRect);
        Toggle_Poly.onValueChanged.AddListener(OnClickPoly);
        Toggle_Circle.onValueChanged.AddListener(OnClickCircle);
    }

    public void OnClickLine(bool isOn)
    {
        Toggle_Line.GetComponent<Image>().color = isOn ? Color.yellow : Color.white;
        UpdateMeasureMode();
    }

    public void OnClickRect(bool isOn)
    {
        Toggle_Rect.GetComponent<Image>().color = isOn ? Color.yellow : Color.white;
        UpdateMeasureMode();
    }

    public void OnClickPoly(bool isOn)
    {
        Toggle_Poly.GetComponent<Image>().color = isOn ? Color.yellow : Color.white;
        UpdateMeasureMode();
    }

    public void OnClickCircle(bool isOn)
    {
        Toggle_Circle.GetComponent<Image>().color = isOn ? Color.yellow : Color.white;
        UpdateMeasureMode();
    }

    private void UpdateMeasureMode()
    {
        if (Toggle_Line.isOn) measureManager.currentMeasureMode = MeasureMode.Line;
        else if (Toggle_Rect.isOn) measureManager.currentMeasureMode = MeasureMode.Rectangle;
        else if (Toggle_Poly.isOn) measureManager.currentMeasureMode = MeasureMode.Polygon;
        else if (Toggle_Circle.isOn) measureManager.currentMeasureMode = MeasureMode.Circle;
        else measureManager.currentMeasureMode = MeasureMode.None;
    }
}
