using UnityEngine;

public class CameraOrbit : MonoBehaviour
{
    [Header("Target Settings")]
    public Transform target;        // Das Objekt, um das sich die Kamera drehen soll
    public float distance = 5.0f;   // Abstand zur Kamera

    [Header("Rotation Settings")]
    public float xSpeed = 120.0f;   // Geschwindigkeit für horizontale Drehung
    public float ySpeed = 120.0f;   // Geschwindigkeit für vertikale Drehung
    public float yMinLimit = -20f;  // Min. vertikaler Winkel
    public float yMaxLimit = 80f;   // Max. vertikaler Winkel

    private float x = 0.0f;
    private float y = 0.0f;

    void Start()
    {
        if (target == null)
        {
            Debug.LogWarning("Kein Target gesetzt! Bitte ein Objekt in 'target' zuweisen.");
            enabled = false;
            return;
        }

        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;

        // Cursor frei beweglich lassen
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
    }

    void LateUpdate()
    {
        if (target == null) return;

        if (Input.GetMouseButton(1)) // Rechte Maustaste gedrückt
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
            x += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
            y -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
            y = Mathf.Clamp(y, yMinLimit, yMaxLimit);
        } else
        {
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
        }

            Quaternion rotation = Quaternion.Euler(y, x, 0);
        Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;

        transform.rotation = rotation;
        transform.position = position;

        Ray ray = transform.GetChild(0).GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, float.MaxValue))
        {
            if (hit.transform.CompareTag("Node"))
            {
                var obj = hit.transform.parent.GetComponent<Node>();
                obj.hovered = true;
            }
            else
            {
                GameManager gm = FindFirstObjectByType<GameManager>();
                Connection closestCon = null;
                float closestDist = 0.5f; // maximale Abstand in Welt-Einheiten

                foreach (var con in gm.GetConnections())
                {
                    Vector3 a = con.nodeA.transform.position;
                    Vector3 b = con.nodeB.transform.position;

                    Vector3 ab = b - a;
                    float t = Mathf.Clamp01(Vector3.Dot(hit.point - a, ab) / ab.sqrMagnitude);
                    Vector3 proj = a + t * ab;

                    float dist = Vector3.Distance(hit.point, proj);
                    if (dist < closestDist)
                    {
                        closestDist = dist;
                        closestCon = con;
                    }
                }

                if (closestCon != null)
                {
                    closestCon.hovered = true;
                }
            }

        }
    }
}