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

[RequireComponent(typeof(Collider2D))]
[RequireComponent(typeof(Rigidbody2D))]
public class PlayerController : MonoBehaviour
{
    public enum PlayerState
    {
        Idle,
        Walking_Right,
        Walking_Left,
        Walking_Up,
        Walking_Down
    };

    public PlayerState playerState;
    public float myVelocity = 10f;
    public bool facingRight = true;
    public Animator playerAnimator;
    private Vector2 velocity;
    private Vector2 moveDistance;

    // Start is called before the first frame update
    void Start()
    {
        playerState = PlayerState.Idle;
        PlayerStateChange();
        AudioManager.instance.PlaySounds("Jazz");
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.D))
        {
            velocity.x = myVelocity;
            playerState = PlayerState.Walking_Right;
            PlayerStateChange();
            /*if (!facingRight)
                Flip();*/
        }

        else if (Input.GetKey(KeyCode.A))
        {
            velocity.x = -myVelocity;
            playerState = PlayerState.Walking_Left;
            PlayerStateChange();
            /*if (facingRight)
                Flip();*/
        }

        else if (Input.GetKey(KeyCode.W))
        {
            velocity.y = myVelocity;
            playerState = PlayerState.Walking_Up;
            PlayerStateChange();
            /*if (facingRight)
                Flip();*/
        }

        else if (Input.GetKey(KeyCode.S))
        {
            velocity.y = -myVelocity;
            playerState = PlayerState.Walking_Down;
            PlayerStateChange();
            /*if (facingRight)
                Flip();*/
        }
        else
        {
            playerState = PlayerState.Idle;
            PlayerStateChange();
        }

        moveDistance = velocity * Time.deltaTime;
        transform.position += (Vector3) moveDistance;
        velocity = new Vector2(0, 0);
    }



    void PlayerStateChange()
        {
            switch (playerState)
            {
                case PlayerState.Idle:
                    if (!playerAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Idle"))
                    {
                        playerAnimator.SetTrigger("Idle");
                    }

                    break;
                case PlayerState.Walking_Right:
                    if (!playerAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Walking_RIght"))
                    {
                        playerAnimator.SetTrigger("Walking_Right");
                    }

                    break;
                case PlayerState.Walking_Left:
                    if (!playerAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Walking_Left"))
                    {
                        playerAnimator.SetTrigger("Walking_Left");
                    }

                    break;
                case PlayerState.Walking_Up:
                    if (!playerAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Walking_Up"))
                    {
                        playerAnimator.SetTrigger("Walking_Up");
                    }

                    break;
                case PlayerState.Walking_Down:
                    if (!playerAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Walking_Down"))
                    {
                        playerAnimator.SetTrigger("Walking_Down");
                    }

                    break;

            }
        }

        /*void Flip()
        {
            // Switch the way the player is labelled as facing.
            facingRight = !facingRight;
    
            // Multiply the player's x local scale by -1.
            Vector3 theScale = transform.localScale;
            theScale.x *= -1;
            transform.localScale = theScale;
        }*/
    
}
