/*
 * ------------------------------------------------------------------------
 *  Description: This class handles enemy movement with ai
 *  Author:      Eli Simpkins-Simmonds
 *  ------------------------------------------------------------------------
 */

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

public class EnemyMovement : MonoBehaviour
{
    [SerializeField] float moveSpeed = 1f;
    [SerializeField] float moveDuration = 2f; // Duration to move in one direction
    [SerializeField] float detectionRange = 5f; // Range within which the enemy detects the player
    [SerializeField] float attackRange = 1.5f; // Range within which the enemy attacks the player
    [SerializeField] float fallSpeed = 2f; // Additional downward velocity

    Rigidbody2D myRigidbody;
    Animator myAnimator;
    Transform player;
    bool movingRight = true;
    float moveTimer;

    void Start()
    {
        myRigidbody = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>();
        moveTimer = moveDuration;
        
        player = GameObject.FindWithTag("Player")?.transform;
        
        if (player == null)
        {
            Debug.LogError("Player not found. Please ensure the player GameObject has the 'Player' tag.");
        }
    }

    void Update()
    {
        if (player != null && IsPlayerInRange())
        {
            MoveTowardsPlayer();
        }
        else
        {
            MoveEnemy();
        }

        FlipEnemy();
    }

    bool IsPlayerInRange()
    {
        return Vector2.Distance(transform.position, player.position) <= detectionRange;
    }

    void MoveEnemy()
    {
        moveTimer -= Time.deltaTime;

        if (moveTimer <= 0)
        {
            movingRight = !movingRight; // Change direction
            moveTimer = moveDuration;   // Reset timer
        }

        float direction = movingRight ? 1f : -1f;
        myRigidbody.velocity = new Vector2(direction * moveSpeed, myRigidbody.velocity.y - fallSpeed * Time.deltaTime);

        myAnimator.SetBool("isAttacking", false);
    }

    void MoveTowardsPlayer()
    {
        Vector2 direction = (player.position - transform.position).normalized;
        float distanceToPlayer = Vector2.Distance(transform.position, player.position);

        Debug.Log("Distance to player: " + distanceToPlayer);

        if (distanceToPlayer <= attackRange) // Distance to stop and attack
        {
            Debug.Log("Attacking player!");
            myRigidbody.velocity = Vector2.zero;
            myAnimator.SetBool("isAttacking", true);
        }
        else
        {
            myRigidbody.velocity = new Vector2(direction.x * moveSpeed, myRigidbody.velocity.y - fallSpeed * Time.deltaTime);
            myAnimator.SetBool("isAttacking", false);
        }
    }

    void FlipEnemy()
    {
        // Flip the enemy by inverting its localScale.x
        Vector3 localScale = transform.localScale;
        if (myRigidbody.velocity.x > 0)
        {
            localScale.x = Mathf.Abs(localScale.x);
        }
        else if (myRigidbody.velocity.x < 0)
        {
            localScale.x = -Mathf.Abs(localScale.x);
        }
        transform.localScale = localScale;
    }
}