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

public class Agente : MonoBehaviour
{
    public ADN adn;
    public float moveSpeed = 2f; // REDUCIDO para más control
    public int currentStep = 0;
    public Transform target;
    private Vector3 startPos;
    private bool isMoving = true;
    private float initialDistance;
    private bool reachedTarget = false;

    void Start()
    {
        // DESACTIVAR FÍSICA COMPLETAMENTE
        Rigidbody rb = GetComponent<Rigidbody>();
        if (rb != null)
        {
            rb.isKinematic = true; // ¡IMPORTANTE!
            rb.useGravity = false;
        }

        // Desactivar colliders si existen
        Collider collider = GetComponent<Collider>();
        if (collider != null)
        {
            collider.enabled = false;
        }
    }

    public void Init(ADN adn, Transform target)
    {
        this.adn = adn;
        this.target = target;
        startPos = transform.position;
        currentStep = 0;
        isMoving = true;
        reachedTarget = false;
        initialDistance = Vector3.Distance(startPos, target.position);
    }

    void Update()
    {
        if (adn == null || currentStep >= adn.geneLength || !isMoving || reachedTarget) return;

        Move(adn.genes[currentStep]);
        currentStep++;

        // Verificar si llegó al objetivo
        float currentDistance = Vector3.Distance(transform.position, target.position);
        if (currentDistance < 0.5f)
        {
            reachedTarget = true;
            Debug.Log($"🎯 Agente {name} llegó al objetivo!");
        }
    }

    void Move(int directionGene)
    {
        // MOVIMIENTO SIMPLE Y DIRECTO
        Vector3 direction = Vector3.zero;

        switch (directionGene)
        {
            case 0: direction = Vector3.forward; break;
            case 1: direction = Vector3.back; break;
            case 2: direction = Vector3.left; break;
            case 3: direction = Vector3.right; break;
            case 4: direction = (Vector3.forward + Vector3.right).normalized; break;
            case 5: direction = (Vector3.forward + Vector3.left).normalized; break;
            case 6: direction = (Vector3.back + Vector3.right).normalized; break;
            case 7: direction = (Vector3.back + Vector3.left).normalized; break;
        }

        transform.Translate(direction * moveSpeed * Time.deltaTime, Space.World);
    }

    public float GetFitness()
    {
        float currentDistance = Vector3.Distance(transform.position, target.position);

        // FITNESS SIMPLE PERO EFECTIVO
        float fitness = 0f;

        if (currentDistance < 0.5f) // Llegó al objetivo
        {
            fitness = 1000f + (adn.geneLength - currentStep) * 10f;
        }
        else if (currentDistance < 1f)
        {
            fitness = 500f;
        }
        else if (currentDistance < 2f)
        {
            fitness = 300f;
        }
        else if (currentDistance < 5f)
        {
            fitness = 100f;
        }
        else
        {
            // Fitness base basado en proximidad
            fitness = 50f / (currentDistance + 1f);
        }

        // Bonus por estar más cerca que al inicio
        if (currentDistance < initialDistance)
        {
            fitness += 50f;
        }

        return Mathf.Max(0.1f, fitness);
    }

    public void ResetAgent()
    {
        currentStep = 0;
        isMoving = true;
        reachedTarget = false;
        initialDistance = Vector3.Distance(transform.position, target.position);
    }

    public void StopMoving()
    {
        isMoving = false;
    }

    void OnDrawGizmos()
    {
        if (target != null)
        {
            Gizmos.color = reachedTarget ? Color.green : Color.red;
            Gizmos.DrawLine(transform.position, target.position);
        }
    }
}