using System;
using TMPro;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.UI;

public class CircleMeasureGroup : MeasureGroup
{
    private CircleMeasureGroupInfoPopup circleMeasureGroupInfoPopup;
    private int steps = 64; 

    public override void AddPoint(Vector3 pos)
    {
        if (points.Count >= 2) return;

        points.Add(pos);

        if (points.Count == 1)
        {
            if (!circleMeasureGroupInfoPopup)
            {
                circleMeasureGroupInfoPopup = Instantiate(circleInfoPopupPrefab).GetComponent<CircleMeasureGroupInfoPopup>();
                circleMeasureGroupInfoPopup.InitUI(infoPopupRoot);
                circleMeasureGroupInfoPopup.transform.position = pos + new Vector3(20, 0, 20);
            }

            GameObject label = Instantiate(labelPrefab, pos, Quaternion.identity, labelRoot);
            labels.Add(label);
        }
        else if (points.Count == 2)
        {    
            deleteButton = Instantiate(deleteButtonPrefab, pos, Quaternion.identity, infoPopupRoot);
            deleteButton.GetComponentInChildren<Button>().onClick.AddListener(() => DestroyAll());
            deleteButton.transform.position = points[0] + new Vector3(-20, 0, 20);

            FindAnyObjectByType<MeasureManager>().isMeasuring = false;
        }
    }

    private void DrawCircle(float radius)
    {
        line.positionCount = steps + 1;

        for (int i = 0; i <= steps; i++)
        {
            float t = (float)i / steps;
            float angle = t * 2 * Mathf.PI;

            float x = Mathf.Cos(angle) * radius;
            float z = Mathf.Sin(angle) * radius;

            line.SetPosition(i, points[0] + new Vector3(x, 0, z));
        }
    }

    public override void UpdatePreviewLine(Vector3 mousePos)
    {
        if (points.Count == 1)
        {
            float radius = Vector3.Distance(points[0], mousePos);
            DrawCircle(radius);

            Vector3 mid = (points[0] + mousePos) * 0.5f;
            labels[0].transform.position = mid;

            double3 llh = GeoUtils.GetLonLatHeight(cesiumGeoreference, points[0]);
            double circumference = 2 * Mathf.PI * radius;
            double area = Mathf.PI * radius * radius;

            labels[0].GetComponentInChildren<TextMeshProUGUI>().text = $"{circumference:F1} m";
            circleMeasureGroupInfoPopup?.UpdateInfo("(¾øÀ½)", llh.x, llh.y, circumference, area);
        }
    }

    public override void EndMeasure()
    {
        DestroyAll();
        FindAnyObjectByType<MeasureManager>().isMeasuring = false;
    }

    public override void DestroyAll()
    {
        base.DestroyAll();
        if (circleMeasureGroupInfoPopup)
            Destroy(circleMeasureGroupInfoPopup.gameObject);
        Destroy(this);
    }

    public override void LateUpdate()
    {
        base.LateUpdate();
        if (circleMeasureGroupInfoPopup)
        {
            circleMeasureGroupInfoPopup.transform.LookAt(circleMeasureGroupInfoPopup.transform.position + cam.transform.forward);

            float heightDiff = Mathf.Abs(cam.transform.position.y - circleMeasureGroupInfoPopup.transform.position.y);
            circleMeasureGroupInfoPopup.transform.localScale = Vector3.one * (heightDiff * lineScaleMultiplier);
        }
    }
}
