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

public class Droid : MonoBehaviour
{
    public Camera cam;

    public Rigidbody rigidbodyComponent;

    private Boolean jumpKeyIsPressed = false;

    private Boolean isGrounded = false;

    public Boolean isJumping = false; 
    public float jumpStartTime;
    public float jumpTime;

    public float jumpForce;

    public float horizontalInput;
    public float speed = 2f;

    public int facing;

    public int Energy = 0 ;

    public int HP = 1;

    [SerializeField] private GameObject fusee;
    public ManagerUI UIManager;
    // Start is called before the first frame update
    void Start()
    {
        UIManager = FindAnyObjectByType<ManagerUI>();
        StartCoroutine(UIManager.UpdateUI(HP, Energy));
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            this.jumpKeyIsPressed = true;
        }
        else if (Input.GetKeyUp(KeyCode.Space))
        {
            this.jumpKeyIsPressed = false;
        }
        if (Input.GetKeyDown(KeyCode.S) && this.Energy > 0)
        {
            this.Energy--;
            StartCoroutine(UIManager.UpdateUI(HP, Energy));
            GameObject ball = Instantiate(fusee, transform.position, new Quaternion(180*this.facing,180,0,0));
            ball.GetComponent<Rigidbody>().velocity =  new Vector3(20*this.facing, ball.GetComponent<Rigidbody>().velocity.y, 0);
        }

        if(this.HP <= 0) 
        {
            Destroy(gameObject);
        }

        horizontalInput = Input.GetAxis("Horizontal");
    }

    private void FixedUpdate()
    {
        Jump();
        rigidbodyComponent.velocity = new Vector3(horizontalInput * speed, rigidbodyComponent.velocity.y, 0);

        if (this.rigidbodyComponent.velocity.x > 0)
        {
            this.facing = 1;
        }
        else if (this.rigidbodyComponent.velocity.x < 0)
        { 
            this.facing = -1; 
        }

    }

    private void Jump()
    {
        if (this.isGrounded && this.jumpKeyIsPressed)
        {
            this.isJumping = true;
            jumpTime = jumpStartTime;
            rigidbodyComponent.velocity = Vector2.up * jumpForce;
        }

        if(this.jumpKeyIsPressed && this.isJumping)
        {
            if (jumpTime > 0)
            {
                rigidbodyComponent.velocity = Vector2.up * jumpForce;
                jumpTime -= Time.deltaTime;
            }
            else
            {
                isJumping = false;
            }
        }

        if(!this.jumpKeyIsPressed)
        {
            this.isJumping = false;
        }
    }

    private void OnCollisionStay() 
    {
        if (!this.jumpKeyIsPressed)
        {
            this.isGrounded = true;
        }
    }

    private void OnCollisionExit()
    {
        this.isGrounded = false;
    }

    private void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Enemy")
        {
            this.HP -= 2;
            Vector3 knockbackDirection = this.rigidbodyComponent.transform.position - other.gameObject.transform.position;
            this.rigidbodyComponent.velocity = knockbackDirection * 20f;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.layer == 3)
        {
            Destroy(other.gameObject);
            this.Energy += 1;
            this.HP += 1;
            StartCoroutine(UIManager.UpdateUI(HP,Energy));
            if (this.Energy == 5)
            {
                this.speed *= 2f;
                this.jumpForce *= 2f;
            }
        }
        if (other.gameObject.layer == 8)
        {
            this.cam.transform.Rotate(new Vector3(0, 0, 180));
        }
    }
}