using UnityEngine;
using System;
using System.Collections;

public class InputRedirecter : MonoBehaviour 
{
    public static InputRedirecter Instance { get; private set; }
    //static InputRedirecter() { Instance = new GameObject("InputRedirecter").AddComponent<InputRedirecter>(); }

    private const float MOVEMENT_THRESHOLD = 0.1f;

    public event EventHandler<AxisMovedEventArgs> AxisMoved;
    public event EventHandler<AxisKeyEventArgs> VerticalAxisKeyDown, VerticalAxisKeyUp, HorizontalAxisKeyDown, HorizontalAxisKeyUp;
    public event EventHandler<EventArgs> EnterPressed;

    private void Awake()
    {
        //Instance = this;
        //DontDestroyOnLoad(this);
    }

    public static void Create()
    {
        Instance = new GameObject("InputRedirecter").AddComponent<InputRedirecter>();
    }

    public class AxisMovedEventArgs : EventArgs
    {
        public float HorizontalMovement { get; private set; }
        public float VerticalMovement { get; private set; }

        public AxisMovedEventArgs(float hMovement, float vMovement)
        {
            HorizontalMovement = hMovement;
            VerticalMovement = vMovement;
        }
    }

    public class AxisKeyEventArgs : EventArgs
    {
        public float Direction { get; private set; }

        public AxisKeyEventArgs(float direction)
        {
            Direction = direction;
        }
    }

    public event EventHandler<EventArgs> InteractionPressed;

    private void Update()
    {
        if (Input.GetButtonDown("Interaction"))
        {
            InteractionPressed(this, EventArgs.Empty);
        }

        if(Input.GetButtonDown("Vertical"))
        {
            if(VerticalAxisKeyDown != null)
            {
                VerticalAxisKeyDown(this, new AxisKeyEventArgs(Mathf.Sign(Input.GetAxis("Vertical"))));
            }
        }
        else if (Input.GetButtonUp("Vertical"))
        {
            if (VerticalAxisKeyUp != null)
            {
                VerticalAxisKeyUp(this, new AxisKeyEventArgs(Mathf.Sign(Input.GetAxis("Vertical"))));
            }
        }
        if (Input.GetButtonDown("Horizontal"))
        {
            if (HorizontalAxisKeyDown != null)
            {
                HorizontalAxisKeyDown(this, new AxisKeyEventArgs(Mathf.Sign(Input.GetAxis("Horizontal"))));
            }
        }
        else if (Input.GetButtonUp("Horizontal"))
        {
            if (HorizontalAxisKeyUp != null)
            {
                HorizontalAxisKeyUp(this, new AxisKeyEventArgs(Mathf.Sign(Input.GetAxis("Horizontal"))));
            }
        }
        if (Input.GetKeyDown(KeyCode.Return))
        {
            if (EnterPressed != null)
            {
                EnterPressed(this, EventArgs.Empty);
            }
        }
    }

    private void FixedUpdate()
    {
        float horMov = Mathf.Abs(Input.GetAxis("Horizontal")) > MOVEMENT_THRESHOLD ? Input.GetAxis("Horizontal") : (0.0f);
        float vertMov = Mathf.Abs(Input.GetAxis("Vertical")) > MOVEMENT_THRESHOLD ? Input.GetAxis("Vertical") : (0.0f);
        if (Mathf.Abs(horMov) > MOVEMENT_THRESHOLD || Mathf.Abs(vertMov) > MOVEMENT_THRESHOLD)
        {
            AxisMoved(this, new AxisMovedEventArgs(horMov, vertMov));
        }
    }
}
