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

public class movement : MonoBehaviour
{
    public Animator animator;
    private string animName = "DoggoWalk";

    public GameObject spawnBZ;
    public GameObject BarkZone;
    public AudioSource bark;
    public AudioClip bark1;
    public AudioClip bark2;
    public AudioClip bark3;

    public KeyCode upbutton = KeyCode.W;
    public KeyCode leftbutton = KeyCode.A;
    public KeyCode rightbutton = KeyCode.D;
    public KeyCode downbutton = KeyCode.S;
    public KeyCode jumpbutton = KeyCode.Space;
    public KeyCode runbutton = KeyCode.LeftShift;
    public KeyCode baseAttackBtn = KeyCode.E;
    public KeyCode strongAttackBtn = KeyCode.Q;

    public GameObject poop;
    public GameObject pooped;

    public float baseJump = 10f;

    // Que tan rapido se mueve
    public float baseSpeed = 10f;
    public bool running = false;
    // Que tan rapido gira.
    public float rotationSpeed = 10f;


    public bool dead = false;
    public bool damaged = false;
    public float dmgTimer = 2f;


    private Vector3 previousLocation;
    private Vector3 currentLocation;
    public bool spook = false;
    private float jumpStrength;
    private bool walk=false;


    // Use this for initialization
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey(KeyCode.Escape)){
            SceneManager.LoadScene("Menu");
    }
        float moveSpeed;
        walk=false;
        previousLocation = currentLocation;
        currentLocation = transform.position;


        if (running == true)
        {
            moveSpeed = baseSpeed * 0.5f;
            jumpStrength = baseJump * 0.8f;
            rotationSpeed = 5f;
            // animName = "Run";
        }
        else
        {
            moveSpeed = baseSpeed * 0.3f;
            jumpStrength = baseJump * 0.6f;
            rotationSpeed = 3f;
            // animName = "Walk";
        }

        if (Input.GetKey(upbutton) && Input.GetKey(leftbutton))
        {
            walk=true;
            animator.Play(animName);
            currentLocation.z += moveSpeed * Time.fixedDeltaTime;
            currentLocation.x -= moveSpeed * Time.fixedDeltaTime;
            transform.position = currentLocation;
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(transform.position - previousLocation), Time.fixedDeltaTime * rotationSpeed);
        }
        else if (Input.GetKey(upbutton) && Input.GetKey(rightbutton))
        {
            walk=true;
            animator.Play(animName);
            currentLocation.z += moveSpeed * Time.fixedDeltaTime;
            currentLocation.x += moveSpeed * Time.fixedDeltaTime;
            transform.position = currentLocation;
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(transform.position - previousLocation), Time.fixedDeltaTime * rotationSpeed);
        }
        else if (Input.GetKey(downbutton) && Input.GetKey(leftbutton))
        {
            walk=true;
            animator.Play(animName);
            currentLocation.x -= moveSpeed * Time.fixedDeltaTime;
            currentLocation.z -= moveSpeed * Time.fixedDeltaTime;
            transform.position = currentLocation;
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(transform.position - previousLocation), Time.fixedDeltaTime * rotationSpeed);
        }
        else if (Input.GetKey(downbutton) && Input.GetKey(rightbutton))
        {
            walk=true;
            animator.Play(animName);
            currentLocation.z -= moveSpeed * Time.fixedDeltaTime;
            currentLocation.x += moveSpeed * Time.fixedDeltaTime;
            transform.position = currentLocation;
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(transform.position - previousLocation), Time.fixedDeltaTime * rotationSpeed);
        }
        else if (Input.GetKey(upbutton))
        {
            walk=true;
            animator.Play(animName);
            currentLocation.z += moveSpeed * Time.fixedDeltaTime;
            transform.position = currentLocation;
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(transform.position - previousLocation), Time.fixedDeltaTime * rotationSpeed);
        }
        else if (Input.GetKey(leftbutton))
        {
            walk=true;
            animator.Play(animName);
            currentLocation.x -= moveSpeed * Time.fixedDeltaTime;
            transform.position = currentLocation;
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(transform.position - previousLocation), Time.fixedDeltaTime * rotationSpeed);
        }
        else if (Input.GetKey(downbutton))
        {
            walk=true;
            animator.Play(animName);
            currentLocation.z -= moveSpeed * Time.fixedDeltaTime;
            transform.position = currentLocation;
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(transform.position - previousLocation), Time.fixedDeltaTime * rotationSpeed);
        }
        else if (Input.GetKey(rightbutton))
        {
            walk=true;
            animator.Play(animName);
            currentLocation.x += moveSpeed * Time.fixedDeltaTime;
            transform.position = currentLocation;
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(transform.position - previousLocation), Time.fixedDeltaTime * rotationSpeed);
        }
        else if (!Input.GetKey(upbutton) && !Input.GetKey(downbutton) && !Input.GetKey(leftbutton) && !Input.GetKey(rightbutton))
        {
            walk=false;
            animator.SetTrigger("DoggoIdle");
        }

        //JUMP
        if (Input.GetKeyDown(jumpbutton))
        {
            Jump();
            animator.SetTrigger("jumpAnim");
        }
        if (Input.GetKeyDown(baseAttackBtn))
        {
            Poop();
        }
        if (Input.GetKeyDown(strongAttackBtn))
        {

            // if (walk)
            // {
            //     // animator.SetTrigger("DoggoBark");
            //     animator.Play("DoggoBarkWalk");
            // }
            // else
            // {
            //     // animator.SetTrigger("DoggoBark");
            //     animator.Play("DogIdleBark");
            // }
            animator.Play("DogIHeadBark");
            // animator.SetTrigger("Bark");
            Bark();
            if (spook)
            {
                GameObject zone = Instantiate(BarkZone, spawnBZ.transform.position, Quaternion.identity);
                StartCoroutine(ExecuteAfterTime(0.1f, zone));
                spook=false;
            }
        }
        //JUMP
        if (Input.GetKeyDown(runbutton))
        {
            if (running == false)
                running = true;
            else
                running = false;
        }
    }

    IEnumerator ExecuteAfterTime(float time, GameObject zone)
    {
        yield return new WaitForSeconds(time);
        Destroy(zone);
        // animName="DoggoWalk";
        // Code to execute after the delay
    }
    void Poop()
    {
        Instantiate(poop, pooped.transform.position, Quaternion.identity);
    }
    void Bark()
    {
        int opc = Random.Range(1, 4);

        switch (opc)
        {
            case 1:
                bark.clip = bark1;
                break;
            case 2:
                bark.clip = bark2;
                break;
            case 3:
                bark.clip = bark3;
                break;
        }

        if (!bark.isPlaying)
        {
            bark.Play();
        }

    }
    void Jump()
    {
        if (CanJump())
        {
            //Salta el rigidbody
            GetComponent<Rigidbody>().AddForce(jumpStrength * transform.up, ForceMode.VelocityChange);
        }
    }

    void OnTriggerStay(Collider coll)
    {
        if (coll.tag == "Barker")
        {
            spook = true;
        }
    }

    public bool CanJump()
    {
        //Cre una linea de tu posicion a tu posicion menos 1
        Ray ray = new Ray(transform.position, transform.up * -1);

        //Crea un choque
        RaycastHit hit;

        //Si hay suelo a tu posicion -1 regresa un true para que te deje saltar
        if (Physics.Raycast(ray, out hit, transform.localScale.y + 0.2f))
        {
            return true;
        }

        // Regresa falso para que no te deje saltar
        return false;
    }
}

