using UnityEngine;

public class TargetingLine : MonoBehaviour
{
    [Header("Line Settings")]
    public Material lineMaterial;
    public Color moveLineColor = Color.blue;
    public Color attackLineColor = Color.red;
    public float lineWidth = 0.1f;

    private LineRenderer lineRenderer;
    private bool isActive = false;

    void Awake()
    {
        SetupLineRenderer();
    }

    private void SetupLineRenderer()
    {
        lineRenderer = gameObject.AddComponent<LineRenderer>();
        lineRenderer.material = lineMaterial;
        lineRenderer.startWidth = lineWidth;
        lineRenderer.endWidth = lineWidth;
        lineRenderer.positionCount = 2;
        lineRenderer.enabled = false; // Initially hidden
        lineRenderer.sortingOrder = 10; // Ensure it renders above other objects

    }

    public void StartTargeting(Vector3 startPosition)
    {
        isActive = true;
        lineRenderer.enabled = true;
        lineRenderer.SetPosition(0, startPosition);
        lineRenderer.SetPosition(1, startPosition);
    }

    public void UpdateTargeting(Vector3 startPosition, Vector3 endPosition, bool isEnemyTarget)
    {
        if (!isActive) return;

        lineRenderer.SetPosition(0, startPosition);
        lineRenderer.SetPosition(1, endPosition);
        lineRenderer.startColor = isEnemyTarget ? attackLineColor : moveLineColor;
        lineRenderer.endColor = isEnemyTarget ? attackLineColor : moveLineColor;
    }

    public void StopTargeting()
    {
        isActive = false;
        lineRenderer.enabled = false;
    }

    public bool IsActive => isActive;
}