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

public class PlayerMovement : MonoBehaviour
{
    public Animator animator;

    public float  movementSpeed, jumpForce;
    public Transform playerGround;
    public Transform playerCeil;

    public bool isGrounded = true;     //Am I on the ground?
    public bool isCeiled = false;      //Am I crouching?

    [Header("Colliders")]
    public BoxCollider fullCollider;
    public BoxCollider crouchedCollider;

    Rigidbody    m_rigidbody;
    private PlayerAttack p_attack;

    [Header("Booleans for deactivation of abilities")]
    public bool canMoveRight = true;
    public bool canMoveLeft = true;
    public bool canJump = true;
    public bool canCrouch = true;
    public bool canAttack = true;

    // Start is called before the first frame update.
    void Start()
    {
        m_rigidbody = GetComponent<Rigidbody>();
        p_attack = GetComponent<PlayerAttack>();
        fullCollider.enabled = true;
        crouchedCollider.enabled = false;
    }
    //void OnDrawGizmos()
    //{
    //    Gizmos.color = Color.yellow;
    //    Gizmos.DrawSphere(playerCeil.position, 0.5f);
    //}

    // Update is called once per frame.
    void Update()
    {
        

        if (Input.GetButton("Horizontal"))
        {
            if (Input.GetAxis("Horizontal") > 0 && canMoveRight)
            {
                animator.SetFloat("speed", 2f);
                m_rigidbody.AddForce(movementSpeed * Vector3.right);
                m_rigidbody.velocity = new Vector3(movementSpeed, m_rigidbody.velocity.y, m_rigidbody.velocity.z);
                transform.rotation = Quaternion.Euler(0, 90, 0);
            }
            if(Input.GetAxis("Horizontal") < 0 && canMoveLeft)
            {
                animator.SetFloat("speed", 2f);
                m_rigidbody.AddForce(movementSpeed * Vector3.right * -1);
                m_rigidbody.velocity = new Vector3(movementSpeed * -1, m_rigidbody.velocity.y, m_rigidbody.velocity.z);
                transform.rotation = Quaternion.Euler(0, 270, 0);
            }
        }

        if (m_rigidbody.velocity == Vector3.zero)
        {
            animator.SetFloat("speed", 0f);
        }

        if (isGrounded) 
        {
            if (!isCeiled)
            {
                if (canJump && Input.GetKeyDown(KeyCode.UpArrow))
                {
                    animator.SetBool("isgrounded", true);
                    m_rigidbody.AddForce(jumpForce * Vector3.up, ForceMode.Impulse);
                }
                else if (canCrouch)
                    if(Input.GetButton("down"))
                    {
                        animator.SetBool("isduck", true);
                        fullCollider.enabled = false;
                        crouchedCollider.enabled = true;
                    }
                    else
                    {
                        animator.SetBool("isduck", false);
                        fullCollider.enabled = true;
                        crouchedCollider.enabled = false;
                    }
            }
            else
            {
                if (Input.GetButton("down"))
                {
                    animator.SetBool("isduck", true);
                    fullCollider.enabled = false;
                    crouchedCollider.enabled = true;
                }
            }
        }
        if (canAttack && Input.GetKeyDown(KeyCode.Space))
        {
            animator.SetTrigger("attack");
            p_attack.attack();
        }

        animator.SetBool("isgrounded", isGrounded);

    }

    void FixedUpdate()
    {
        LayerMask mask1 = LayerMask.GetMask("Ground");
        isGrounded = Physics.CheckSphere(playerGround.position, 0.5f, mask1);

        LayerMask mask2 = LayerMask.GetMask("Platform");
        isCeiled = Physics.CheckSphere(playerCeil.position, 0.5f, mask2);
    }

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Ability")
        {
            other.gameObject.GetComponent<UnlockAbility>().Activate();
            Destroy(other.gameObject);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name == "FinalPointTrigger")
            GameManager.Instance.loadLevel2();
    }
}
