using CesiumForUnity;
using System.Collections.Generic;
using UnityEngine;

public abstract class MeasureGroup : MonoBehaviour
{
    protected CesiumGeoreference cesiumGeoreference;
    protected Camera cam;
    protected LineRenderer line;

    protected GameObject deleteButton;
    protected List<GameObject> labels = new List<GameObject>();

    protected GameObject labelPrefab;
    protected GameObject deleteButtonPrefab;
    protected GameObject lineInfoPopupPrefab;
    protected GameObject polygonInfoPopupPrefab;
    protected GameObject circleInfoPopupPrefab;

    protected RectTransform labelRoot;
    protected RectTransform infoPopupRoot;

    protected float lineWidth;
    protected float lineScaleMultiplier;
    protected double totalDistance;
    protected List<Vector3> points = new List<Vector3>();

    public virtual void Init(
        LineRenderer line,
        GameObject labelPrefab,
        GameObject deleteButtonPrefab,
        GameObject lineinfoPopupPrefab,
        GameObject polygonInfoPopupPrefab,
        GameObject circleInfoPopupPrefab,
        RectTransform labelRoot,
        RectTransform infoPopupRoot,
        float lineScaleMultiplier,
        float lineWidth,
        Material lineMaterial)
    {
        this.line = line;
        this.labelPrefab = labelPrefab;
        this.deleteButtonPrefab = deleteButtonPrefab;
        this.lineInfoPopupPrefab = lineinfoPopupPrefab;
        this.polygonInfoPopupPrefab = polygonInfoPopupPrefab;
        this.circleInfoPopupPrefab = circleInfoPopupPrefab;
        this.labelRoot = labelRoot;
        this.infoPopupRoot = infoPopupRoot;
        this.lineScaleMultiplier = lineScaleMultiplier;
        this.lineWidth = lineWidth;

        cesiumGeoreference = FindAnyObjectByType<CesiumGeoreference>();
        cam = Camera.main;
    }

    public abstract void AddPoint(Vector3 pos);
    public abstract void EndMeasure();

    public virtual void DestroyAll()
    {
        foreach (var lbl in labels)
        {
            Destroy(lbl);
        }

        Destroy(line.gameObject);
        if (deleteButton) Destroy(deleteButton);
    }

    public abstract void UpdatePreviewLine(Vector3 mousePos);

    public virtual void LateUpdate()
    {
        if (line)
        {
            float heightDiff = Mathf.Abs(cam.transform.position.y - points[0].y);
            line.widthMultiplier = lineWidth * (heightDiff * lineScaleMultiplier);
        }

        foreach (var lbl in labels)
        {
            lbl.transform.LookAt(lbl.transform.position + cam.transform.forward);

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

        if (deleteButton)
        {
            deleteButton.transform.LookAt(deleteButton.transform.position + cam.transform.forward);

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