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

public class FrogMovement : MonoBehaviour
{
    private float jumpSpeed = 50f; // Speed of the jump
    public LayerMask platformLayer; // Layer for the platforms
    private float rotationSpeed = 5f; // Speed of rotation
    private float maxScaleFactor = 2.5f; // Maximum size the frog should grow during jump
    public float scaleSpeed = 5f; // Speed at which the frog scales up/down


    private bool isJumping = false;
    private Vector2 targetPosition;
    private Quaternion targetRotation; // To store the direction the frog is facing
    private Vector3 originalScale; // To store the original size of the frog
    private float startYPosition; // To store the Y position when the jump starts
    private float startPositionX; // To store the starting X position


    void Start()
    {
        originalScale = transform.localScale; // Store the original scale at the start
    }

    void Update()
    {
        {
            // Prevent new jump input while the frog is in the air
            if (!isJumping && Input.GetMouseButtonDown(0)) // Left mouse click
            {
                Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                mousePos.z = 0f; // Ignore Z-axis

                RaycastHit2D hit = Physics2D.Raycast(mousePos, Vector2.down, Mathf.Infinity, platformLayer);

                if (hit.collider != null)
                {
                var _audioHandler = GetComponentInParent<PlayerAudioHandler>();
                _audioHandler.PlayJump();
                    // If we hit a platform, set the target position
                    targetPosition = hit.point;
                    targetPosition.y += 1f; // Make sure frog doesn't go below the platform
                    isJumping = true;
                    startYPosition = transform.position.y; // Record the starting Y position of the jump
                    startPositionX = transform.position.x; // Record the starting X position

                    // Calculate direction to the target position
                    Vector2 direction = (targetPosition - (Vector2)transform.position).normalized;
                    // Store the rotation the frog should face during the jump
                    float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
                    targetRotation = Quaternion.Euler(0, 0, angle); // Rotation only on the Z-axis
                }
            }

            // Handle the frog's jump towards the target position
            if (isJumping)
            {
                // Rotate the frog towards the target direction (on the Z-axis only)
                transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);

                // Move the frog towards the target position without affecting the rotation
                float step = jumpSpeed * Time.deltaTime;
                Vector2 moveDirection = (targetPosition - (Vector2)transform.position).normalized;
                transform.position = Vector2.MoveTowards(transform.position, targetPosition, step);

                // Calculate progress of movement (from start position to target position)
                float totalDistance = Vector2.Distance(new Vector2(startPositionX, startYPosition), targetPosition);
                float currentDistance = Vector2.Distance(new Vector2(startPositionX, startYPosition), transform.position);
                float progress = currentDistance / totalDistance; // A value between 0 and 1

                // Scale the frog based on its progress (0 to 1)
                float targetScale;
                if (progress <= 0.5f) // Growing phase (up to halfway point)
                {
                    targetScale = Mathf.Lerp(1f, maxScaleFactor, progress * 2); // Scale up
                }
                else // Shrinking phase (after halfway point)
                {
                    targetScale = Mathf.Lerp(maxScaleFactor, 1f, (progress - 0.5f) * 2); // Scale down
                }

                // Smoothly scale the frog
                transform.localScale = Vector3.Lerp(transform.localScale, originalScale * targetScale, scaleSpeed * Time.deltaTime);

                // Stop jumping once we reach the target position
                if (Vector2.Distance(transform.position, targetPosition) < 0.1f)
                {
                    isJumping = false; // Frog has landed
                    transform.localScale = originalScale; // Ensure the frog is back to its original size when it lands
                }
            }
        }
    }
}