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

[RequireComponent(typeof(Rigidbody))]
public class HoverBoardControl : MonoBehaviour
{
    [Header("Hover Settings")]
    public float hoverForce = 9.0f;
    public float hoverHeight = 2.0f;
    public GameObject[] hoverPoints;

    [Header("Movement Settings")]
    public float forwardAcceleration = 100.0f;
    public float backwardAcceleration = 25.0f;
    public float turnStrength = 10f;

    [Header("UI & Scoring")]
    public Text scoreText;
    public int currentScore;
    public HUD hud;
    public TrickManager trickManager;

    [Header("Boost Settings")]
    public float boostMultiplier = 2.0f;

    [Header("Rotation Settings")]
    public float minRotation = -20.0f;
    public float maxRotation = 0.0f;

    [Header("Timer Settings")]
    public float timer = 5.0f;

    [Header("Miscellaneous")]
    public bool moving;
    public GameObject board;
    private bool isGrinding;
   

    [Header("Input Settings")]
    public KeyCode moveForwardKey = KeyCode.W;
    public KeyCode moveBackwardKey = KeyCode.S;
    public KeyCode turnLeftKey = KeyCode.A;
    public KeyCode turnRightKey = KeyCode.D;

    [Header("Customizable Keys")]
    [SerializeField] private KeyCode ollieKey = KeyCode.Space;
    [SerializeField] private KeyCode boostKey = KeyCode.LeftControl;
    [SerializeField] private KeyCode grindKey = KeyCode.G;

    int layerMask;
    float currentThrust;
    float currentTurn;

    Rigidbody rb;
    HoverboardGrindController hoverboardGrindController;

    // States
    private bool isGrounded;
    private bool isJump;
    private bool isRamp;
    private bool isDead;
    private bool isEnteringGrind; // Added boolean flag

    // Velocity control
    private float speedLimit;
    private float speedReductionValue;
    private float currentSpeed;
    private float collisionSpeedReduction;

    // Grind variables
    private Transform grindRail;

    void Awake()
    {
        rb = GetComponent<Rigidbody>();
        hud = FindObjectOfType<HUD>();
        trickManager = GetComponent<TrickManager>();
        hoverboardGrindController = GetComponent<HoverboardGrindController>();
    }

    void Start()
    {
        layerMask = 1 << LayerMask.NameToLayer("Characters");
        layerMask = ~layerMask;

        hoverForce = 1500f;
        hoverHeight = 6f;

        if (scoreText == null)
        {
            GameObject canvas = GameObject.Find("Canvas");
            if (canvas != null)
            {
                scoreText = Instantiate(Resources.Load("ScoreText", typeof(Text))) as Text;
                scoreText.transform.SetParent(canvas.transform, false);
            }
            else
            {
                Debug.LogError("Canvas not found. Make sure there is a Canvas GameObject in the scene.");
            }
        }

        // Initialize state variables
        isGrounded = false;
        isJump = false;
        isRamp = false;
        isDead = false;

        // Initialize velocity control variables
        speedLimit = 10f; // Example value, assign your desired values
        speedReductionValue = 2f; // Example value, assign your desired values
        currentSpeed = 0f;
        collisionSpeedReduction = 2f; // Example value, assign your desired values
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
            // Handle other gameplay effects of collision with the ground
            // Add any additional logic for isGrounded transition
        }
        else if (collision.gameObject.CompareTag("Obstacle"))
        {
            currentSpeed -= collisionSpeedReduction;
            // Add any additional logic for collision with obstacles
        }
        // Other collision handling...
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("GrindRail"))
        {
            isGrinding = true;
            grindRail = other.transform;
            hoverboardGrindController.StartGrinding(other.transform, other.transform.right);
            // Add any additional logic for starting grind
        }
        // Other trigger handling...
    }

    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.CompareTag("GrindRail"))
        {
            isGrinding = false;
            hoverboardGrindController.EndGrinding();
            // Additional grind exit logic goes here
        }
        // Other trigger exit handling...
    }

    void Update()
    {
        currentThrust = 0.0f;
        currentTurn = 0.0f;

        scoreText.text = currentScore + " Points";

        if (Input.GetKeyDown(ollieKey))
        {
            trickManager.ExecuteOllieOrTrick(1500f);
        }

        if (hud != null)
        {
            if (Input.GetKeyDown(boostKey) && (hud.BoostInt > 0))
            {
                forwardAcceleration = forwardAcceleration * boostMultiplier;
                backwardAcceleration = backwardAcceleration * boostMultiplier;
            }
            else if (Input.GetKeyUp(boostKey))
            {
                forwardAcceleration = forwardAcceleration / boostMultiplier;
                backwardAcceleration = backwardAcceleration / boostMultiplier;
            }

            if (Input.GetKey(boostKey))
            {
                hud.BoostInt -= 0.2f;
            }
        }

        Vector3 currentRotation = transform.rotation.eulerAngles;
        currentRotation.x = Mathf.Clamp(currentRotation.x, minRotation, maxRotation);
        currentRotation.z = Mathf.Clamp(currentRotation.z, minRotation, maxRotation);
        transform.rotation = Quaternion.Euler(currentRotation);

        float aclAxis = Input.GetAxis("Vertical");
        if (aclAxis > 0.1f)
            currentThrust = aclAxis * forwardAcceleration;
        else if (aclAxis < -0.1f)
            currentThrust = aclAxis * backwardAcceleration;

        float turnAxis = Input.GetAxis("Horizontal");
        if (Mathf.Abs(turnAxis) > 0.1f)
            currentTurn = turnAxis;

        if (Input.GetKeyDown(trickManager.kickflipKey))
        {
            trickManager.ExecuteKickflip();
        }
        else if (Input.GetKeyDown(trickManager.heelflipKey))
        {
            trickManager.ExecuteHeelflip();
        }
        else if (Input.GetKeyDown(trickManager.shoveItKey))
        {
            trickManager.ExecuteShoveIt();
        }

        // Handle the grinding functionality with the new flag
        if (Input.GetKeyDown(grindKey))
        {
            if (!isGrind && !isEnteringGrind) // Check isEnteringGrind flag
            {
                isEnteringGrind = true; // Set the flag
                StartCoroutine(EnterGrind()); // Use the coroutine for grind initiation
            }
        }
        else if (Input.GetKeyUp(grindKey))
        {
            if (isGrind)
            {
                hoverboardGrindController.EndGrinding();
                isGrind = false;
            }
        }

    }

    private void FixedUpdate()
    {
        // Skip hover mechanics when grinding
        if (!hoverboardGrindController.GrindMovement(isGrinding, isEnteringGrind))
        {
            ApplyHover();
            ApplyMovement();
            ApplyTurning();
        }
    }

    private void ApplyHover()
    {
        for (int i = 0; i < hoverPoints.Length; i++)
        {
            var hoverPoint = hoverPoints[i];
            RaycastHit hoverHit;
            if (Physics.Raycast(hoverPoint.transform.position, -Vector3.up, out hoverHit, hoverHeight, layerMask))
            {
                rb.AddForceAtPosition(Vector3.up * hoverForce * (1.0f - (hoverHit.distance / hoverHeight)), hoverPoint.transform.position);
            }
            else
            {
                if (transform.position.y > hoverPoint.transform.position.y)
                {
                    rb.AddForceAtPosition(hoverPoint.transform.up * hoverForce, hoverPoint.transform.position);
                }
                else
                {
                    rb.AddForceAtPosition(hoverPoint.transform.up * -hoverForce, hoverPoint.transform.position);
                }
            }
        }
    }

    private void ApplyMovement()
    {
        if (Mathf.Abs(currentThrust) > 0)
        {
            rb.AddForce(transform.forward * currentThrust);
        }
    }

    private void ApplyTurning()
    {
        if (currentTurn > 0)
        {
            rb.AddRelativeTorque(Vector3.up * currentTurn * turnStrength);
        }
        else if (currentTurn < 0)
        {
            rb.AddRelativeTorque(Vector3.up * currentTurn * turnStrength);
        }
    }

    private void HandleGrindingMovement()
    {
        hoverboardGrindController.GrindMovement(isGrinding, isEnteringGrind);
    }

    // New Coroutine for grind initiation
    IEnumerator EnterGrind()
    {
        GrindableSurface nearestSurface = hoverboardGrindController.FindClosestGrindableSurface();
        if (nearestSurface != null)
        {
            hoverboardGrindController.StartGrinding(nearestSurface.transform, nearestSurface.GetDirection());
            isGrinding = true;

            // Add any additional logic for entering grind, e.g., disable player controls, animations, etc.

            yield return new WaitForSeconds(1f); // Adjust the duration as needed

            // Add any additional logic after entering grind, e.g., enable player controls, animations, etc.
            isEnteringGrind = false;
        }
        else
        {
            isEnteringGrind = false;
        }
    }
}


Assets\Hover\HoverBoardControl.cs(218,18): error CS0103: The name 'isGrind' does not exist in the current context

Assets\Hover\HoverBoardControl.cs(226,17): error CS0103: The name 'isGrind' does not exist in the current context

Assets\Hover\HoverBoardControl.cs(229,17): error CS0103: The name 'isGrind' does not exist in the current context

Assets\Hover\HoverBoardControl.cs(238,13): error CS0023: Operator '!' cannot be applied to operand of type 'void'