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

public class MarioController : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float jumpForce = 5f;
    public float ladderClimbSpeed = 2f;
    public int maxHealth = 3;
    private int currentHealth;
    private bool grounded;
    private bool onLadder;
    private bool climbingLadder;
    private bool overBarrel;
    private bool countedScore;
    private Vector2 direction;
    private Rigidbody2D rb2d;
    private new Collider2D collider;
    private Collider2D[] results;
    private ScoreCounter scoreCounter;

    private void Awake()
    {
        rb2d = GetComponent<Rigidbody2D>();
        collider = GetComponent<Collider2D>();
        results = new Collider2D[4];
    }

    private void Start()
    {
        currentHealth = maxHealth;
        scoreCounter = FindObjectOfType<ScoreCounter>();
    }

    private void CheckCollision()
    {
        grounded = false;
        onLadder = false;

        Vector2 size = collider.bounds.size;
        size.y += 0.1f;
        size.x /= 2f;

        int amount = Physics2D.OverlapBoxNonAlloc(transform.position, size, 0f, results);

        for (int i = 0; i < amount; i++)
        {
            GameObject hit = results[i].gameObject;

            if (hit.layer == LayerMask.NameToLayer("Platform"))
            {
                grounded = hit.transform.position.y < (transform.position.y - 0.5f);
            }
            else if (hit.layer == LayerMask.NameToLayer("Ladder"))
            {
                onLadder = true;
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Barrel"))
        {
            overBarrel = true;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Barrel"))
        {
            overBarrel = false;
            countedScore = false; // Reset the countedScore flag when leaving the barrel trigger
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Barrel"))
        {
            if (overBarrel && !countedScore)
            {
                scoreCounter.score += 100;
                countedScore = true;
            }
            else
            {
                currentHealth = 0;
                ResetScene();
            }
        }
        else if (collision.gameObject.layer == LayerMask.NameToLayer("Win"))
        {
            ResetScene();
        }
    }

    private void ResetScene()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    private void Update()
    {
        CheckCollision();

        if (grounded && Input.GetButtonDown("Jump"))
        {
            rb2d.velocity = new Vector2(rb2d.velocity.x, jumpForce);
        }

        float movement = Input.GetAxis("Horizontal");

        if (grounded || overBarrel)
        {
            direction.y = Mathf.Max(direction.y, -1f);
            climbingLadder = false;
        }

        if (onLadder && Input.GetButton("Vertical"))
        {
            climbingLadder = true;
            float climbDirection = Input.GetAxis("Vertical");
            rb2d.velocity = new Vector2(0, climbDirection * ladderClimbSpeed);
            rb2d.gravityScale = 0f;
        }
        else if (climbingLadder)
        {
            rb2d.gravityScale = 1f;
            rb2d.velocity = new Vector2(rb2d.velocity.x, 0f);
            rb2d.constraints = RigidbodyConstraints2D.FreezePositionX;
        }
        else
        {
            rb2d.gravityScale = 1f;
            rb2d.constraints = RigidbodyConstraints2D.FreezeRotation;
        }

        rb2d.velocity = new Vector2(movement * moveSpeed, rb2d.velocity.y);

        if (movement > 0)
        {
            transform.localScale = new Vector3(1f, 1f, 1f);
        }
        else if (movement < 0)
        {
            transform.localScale = new Vector3(-1f, 1f, 1f);
        }
    }

    private void FixedUpdate()
    {
        int amount = Physics2D.OverlapCircleNonAlloc(transform.position, 0.1f, results);

        for (int i = 0; i < amount; i++)
        {
            GameObject hit = results[i].gameObject;

            if (hit.layer == LayerMask.NameToLayer("Barrel"))
            {
                overBarrel = true;
                return;
            }
        }

        overBarrel = false;
    }
} why is mario not getting increased score when completing a jump over a barrel?