using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace LunaLightXMG
{
    public class PlayerInput
    {
        public bool HasControl { get; set; }
        public bool Controller { get; set; }
        public bool CanDash { get; set; }
        public float KeyUp { get; private set; }
        public float KeyDown { get; private set; }
        public float KeyRight { get; private set; }
        public float KeyLeft { get; private set; }
        public bool KeyJump { get; private set; }
        public bool KeyDash { get; private set; }
        public bool KeyAttack { get; private set; }
        public bool KeyWallClimb { get; private set; }

        public void Update(KeyboardState keyboardState)
        {
            if (HasControl)
            {
                if (Controller)
                {
                    // If using controller, switch to keyboard by pressing any key
                    if (keyboardState.GetPressedKeys().Length > 0)
                    {
                        // TODO: Insert logic to create the objMouseJoyStick instance
                        Controller = false;
                    }

                    // Zero input each step
                    KeyUp = 0;
                    KeyDown = 0;
                    KeyRight = 0;
                    KeyLeft = 0;
                    KeyJump = false;
                    KeyDash = false;
                    KeyAttack = false;
                    KeyWallClimb = false;

                    // Controller input (assuming Xbox controller)
                    GamePadState gamepadState = GamePad.GetState(PlayerIndex.One);
                    if (gamepadState.IsConnected)
                    {
                        float axisLH = gamepadState.ThumbSticks.Left.X;
                        float axisLV = gamepadState.ThumbSticks.Left.Y;

                        if (Math.Abs(axisLH) > 0.5f || Math.Abs(axisLV) > 0.5f)
                        {
                            KeyDown = Math.Max(axisLV, 0);
                            KeyUp = Math.Abs(Math.Min(axisLV, 0));
                            KeyLeft = Math.Abs(Math.Min(axisLH, 0)) > 0.5f ? Math.Abs(Math.Min(axisLH, 0)) : 0;
                            KeyRight = Math.Max(axisLH, 0) > 0.5f ? Math.Max(axisLH, 0) : 0;
                        }

                        KeyJump = gamepadState.Buttons.A == ButtonState.Pressed;
                        KeyWallClimb = gamepadState.Buttons.LeftShoulder == ButtonState.Pressed;
                        KeyDash = gamepadState.Buttons.RightShoulder == ButtonState.Pressed;
                    }
                }
                else
                {
                    // If using keyboard, switch to controller by pressing the start button on the gamepad
                    GamePadState gamepadState = GamePad.GetState(PlayerIndex.One);
                    if (gamepadState.IsConnected && gamepadState.Buttons.Start == ButtonState.Pressed)
                    {
                        // TODO: Insert  logic to destroy the objMouseJoyStick instance
                        Controller = true;
                    }

                    // Keyboard input
                    //KeyboardState keyboardState = Keyboard.GetState();
                    
                    KeyLeft = keyboardState.IsKeyDown(Keys.A) ? 1 : 0;
                    KeyRight = keyboardState.IsKeyDown(Keys.D) ? 1 : 0;
                    KeyUp = keyboardState.IsKeyDown(Keys.W) ? 1 : 0;
                    KeyDown = keyboardState.IsKeyDown(Keys.S) ? 1 : 0;

                    // Prevent superposition
                    if (KeyLeft != 0 && KeyRight != 0)
                    {
                        KeyLeft = 0;
                        KeyRight = 0;
                    }
                    if (KeyUp != 0 && KeyDown != 0)
                    {
                        KeyUp = 0;
                        KeyDown = 0;
                    }

                    KeyJump = keyboardState.IsKeyDown(Keys.Space);
                    KeyDash = Mouse.GetState().LeftButton == ButtonState.Pressed;
                    //KeyAttack = Mouse.GetState().RightButton == ButtonState.Pressed;
                    KeyWallClimb = keyboardState.IsKeyDown(Keys.LeftShift);
                }
            }
            else
            {
                // Zero input if player has no control
                KeyUp = 0;
                KeyDown = 0;
                KeyRight = 0;
                KeyLeft = 0;
                KeyJump = false;
                KeyDash = false;
                KeyAttack = false;
                KeyWallClimb = false;
            }
        }
    }
}
