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


public class ControllerProfileScript : MonoBehaviour
{
    [Header("CurrentKeys")]
    [SerializeField] KeyCode currentKeyboardMoveLeftKey         = KeyCode.A;
    [SerializeField] KeyCode currentKeyboardMoveRightKey        = KeyCode.D;
    [SerializeField] KeyCode currentKeyboardMoveUpKey           = KeyCode.W;
    [SerializeField] KeyCode currentKeyboardMoveDownKey         = KeyCode.S;
    [SerializeField] KeyCode currentKeyboardStartKey            = KeyCode.Plus;
    [SerializeField] KeyCode currentKeyboardSelectKey           = KeyCode.Minus;
    [SerializeField] KeyCode currentKeyboardNextKey             = KeyCode.N;
    [SerializeField] KeyCode currentKeyboardBackKey             = KeyCode.B;

    [Header("CurrentController")]
    [SerializeField] string currentControllerMoveXAxis          = "Horitontal";
    [SerializeField] string currentControllerMoveYAxis          = "Vertical";
    [SerializeField] KeyCode currentControllerStartButton       = KeyCode.Joystick1Button7;
    [SerializeField] KeyCode currentControllerSelectButton      = KeyCode.Joystick1Button6;
    [SerializeField] KeyCode currentControllerNextButton        = KeyCode.Joystick1Button0;
    [SerializeField] KeyCode currentControllerBackButton        = KeyCode.Joystick1Button1;
    
    [Header("Tolerance")]
    [SerializeField] private float buttonTol = 0.01f; // used so that controllers have dead zone with sticks.
    [SerializeField] private float stickTol = 0.66f;

    [Header("Movement")]
    private Vector3 lastPosition = Vector3.zero;
    private Vector2 lastDir = Vector2.zero;

    [Header("ControllerAxis")]
    private float H; // used to store horizontal input values.
    private float V; // used to store vertical input values.

    [Header("Player")]
    //player stuff
    private Rigidbody2D rigidBody;


    #region StartAndUpdate
    // Use this for initialization
    void Start()
    {
        rigidBody = this.gameObject.GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        GetMovement();
    }

    void FixedUpdate()
    {
      
    }

    #endregion

    #region Controller
    //=======Controller Functions===================

    public Vector2 MovementControllers()
    {
        if (IsControllerMoveAxisSet())
        {
            //get the axis values first
            H = Input.GetAxis(currentControllerMoveXAxis);
            V = Input.GetAxis(currentControllerMoveYAxis);


            if (Mathf.Abs(H) > Mathf.Abs(V))
            {
                if (H > stickTol)
                {
                    lastDir = Vector2.right;
                }
                //check if pushed horizontally to the left
                else if (H < -stickTol)
                {
                    lastDir = Vector2.left;
                }
            }
            else if (Mathf.Abs(V) > Mathf.Abs(H))
            {
                if (V > stickTol)
                {
                    lastDir = Vector2.down;
                }
                //check if pushed vertically to the right
                else if (V < -stickTol)
                {
                    lastDir = Vector2.up;
                }
            }
            else
            {
                lastDir = Vector2.zero;
            } 
        }
        else
        {
            lastDir = Vector2.zero;
        }

        //Debug.Log ("NONE, H: " + H + ", V: " + V);

        return lastDir;

    }

    public bool IsControllerMoveAxisSet()
    {
        return (!string.IsNullOrEmpty(currentControllerMoveYAxis) &&
            !string.IsNullOrEmpty(currentControllerMoveXAxis));
    }
    #endregion

    #region Keyboard
    //=========Keyboard Functions===========	
    Vector2 MovementKeyboard()
    {
        Vector2 dir = Vector2.zero;
        if (IsKeyboardMovementSet())
        {
            int keysPressed = 0;
            Vector2 v, l, r, d, u;
            v = l = r = u = d = Vector2.zero;
            if (Input.GetKeyDown(currentKeyboardMoveLeftKey))
            {
                dir = Vector2.left;
                keysPressed++;
            }
            if (Input.GetKeyDown(currentKeyboardMoveRightKey))
            {
                dir = Vector2.right;
                keysPressed++;
            }
            if (Input.GetKeyDown(currentKeyboardMoveUpKey))
            {
                dir = Vector2.up;
                keysPressed++;
            }
            if (Input.GetKeyDown(currentKeyboardMoveDownKey))
            {
                dir = Vector2.down;
                keysPressed++;
            }

            //check multiple keys pressed
            if (keysPressed == 1)
            {
                lastDir = dir;
                //Debug.Log("LastDIR: " + lastDir);
                return lastDir;
            }
        }
        return lastDir = Vector2.zero;
    }

    public bool IsKeyboardMovementSet()
    {
        return (currentKeyboardMoveDownKey != KeyCode.None &&
            currentKeyboardMoveUpKey != KeyCode.None &&
            currentKeyboardMoveRightKey != KeyCode.None &&
            currentKeyboardMoveLeftKey != KeyCode.None);
    }
    #endregion

    #region Getters

    public Vector2 GetLastDirection()
    {
        return lastDir;
    }

    public Vector2 GetMovement()
    {
        Vector2 currentMovement = Vector2.zero;
        if (IsKeyboardMovementSet())
        {
            if(MovementKeyboard() != Vector2.zero)
            {
                currentMovement = MovementKeyboard();
            }
        }
        //if (IsControllerMoveAxisSet())
        //{ 
        //    if (MovementControllers() != Vector2.zero)
        //    {
        //        currentMovement = MovementControllers();
        //    }
        //}

        return Vector2.zero;
    }

    public bool IsStartButtonPushed()
    {
        return (Input.GetKeyDown(currentControllerStartButton) || Input.GetKeyDown(currentKeyboardStartKey));
    }

    public bool IsSelectButtonPushed()
    {
        return (Input.GetKeyDown(currentControllerSelectButton) || Input.GetKeyDown(currentKeyboardSelectKey));
    }

    public bool IsNextButtonPushed()
    {
        return (Input.GetKeyDown(currentControllerNextButton) || Input.GetKeyDown(currentKeyboardNextKey));
    }

    public bool IsBackButtonPushed()
    {
        return (Input.GetKeyDown(currentControllerBackButton) || Input.GetKeyDown(currentKeyboardBackKey));
    }

    public bool IsTryingToMove()
    {
        if (IsControllerMoveAxisSet())
        {
            //Debug.Log ("Trying controller");
            H = Input.GetAxis(currentControllerMoveXAxis);
            V = Input.GetAxis(currentControllerMoveYAxis);
            return ((Mathf.Abs(H) > stickTol) ||
                (Mathf.Abs(V) > stickTol));
        }
        else if (IsKeyboardMovementSet())
        {
            //Debug.Log ("Trying keyboard");
            if (MovementKeyboard() != Vector2.zero)
            {
                return true;
            }
        }

        //Debug.Log ("FALSE");
        return false;
    }

    #endregion
}