using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class AttackSystem : MonoBehaviour
{
    public GameObject attackBox;         // The attack box UI panel
    public Slider attackSlider;           // The slider representing the timing bar
    public float sliderSpeed = 2f;        // Speed at which the slider moves
    public Button fightButton;            // The Fight button to initiate the attack
    public EnemyScript currentEnemy;

    private bool isAttacking = false;     // Is the attack currently happening?

    void Start()
    {
        attackBox.SetActive(false);       // Hide the attack box initially
        fightButton.onClick.AddListener(StartAttack); // Assign the Fight button click event
    }

    void Update()
    {
        if (isAttacking)
        {
            MoveSlider();
            if (Input.GetKeyDown(KeyCode.Space))
            {
                // Capture the wrapped slider value at the moment of the key press
                float hitPosition = Mathf.Repeat(attackSlider.value, attackSlider.maxValue);
                Debug.Log($"Space pressed at attackSlider.value = {hitPosition}");
                StopAttack(hitPosition);
            }
        }
    }

    // Start the attack process
    void StartAttack()
    {
        if (currentEnemy == null)
        {
            Debug.LogError("No enemy assigned to attack!");
            return;
        }

        attackBox.SetActive(true);
        attackSlider.value = 0;          // Reset slider to start
        isAttacking = true;
    }

    // Move the slider handle
    void MoveSlider()
    {
        attackSlider.value += sliderSpeed * Time.deltaTime;

        // Log the raw slider value for debugging
        Debug.Log($"Moving Slider: {attackSlider.value}");
    }

    // Stop the slider and determine the hit outcome
    void StopAttack(float hitPosition)
    {
        isAttacking = false;
        attackBox.SetActive(false);

        Debug.Log($"Captured hitPosition: {hitPosition}");

        int damage = 0;

        if (hitPosition >= 0.45f && hitPosition <= 0.55f)
        {
            Debug.Log("Perfect Hit! (Green Zone)");
            damage = 10; // High damage for a perfect hit
        }
        else if (hitPosition >= 0.35f && hitPosition < 0.45f || hitPosition > 0.55f && hitPosition <= 0.65f)
        {
            Debug.Log("Good Hit! (Yellow Zone)");
            damage = 5; // Medium damage for a good hit
        }
        else
        {
            Debug.Log("Missed! (Red Zone)");
            damage = 0; // No damage for a miss
        }
        Debug.Log($"{hitPosition} hitPosition!");

        if (damage > 0)
        {
            currentEnemy.TakeDamage(damage);
        }
        else
        {
            Debug.Log($"{currentEnemy.enemyName} dodged the attack!");
        }
    }
}