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

namespace Igdc.Controls
{
    public class GamepadConfig : ControlConfigBase<Buttons>
    {
        private GamePadState _state;

        public override ControlState ExitButton => ProcessButton(Buttons.BigButton);

        public override ControlState FullscreenButton => ProcessButton(Buttons.Back);

        public override ControlState RestartButton => ProcessButton(Buttons.Start);

        public override ControlState BattleLeftButton => ProcessButton(Buttons.LeftThumbstickLeft, Buttons.DPadLeft);

        public override ControlState BattleRightButton => ProcessButton(Buttons.LeftThumbstickRight, Buttons.DPadRight);

        public override ControlState MoveLeftButton => ProcessButton(Buttons.LeftThumbstickLeft);

        public override ControlState MoveUpButton => ProcessButton(Buttons.LeftThumbstickUp);

        public override ControlState MoveRightButton => ProcessButton(Buttons.LeftThumbstickRight);

        public override ControlState MoveDownButton => ProcessButton(Buttons.LeftThumbstickDown);

        public override ControlState MagicButton => ProcessButton(Buttons.RightShoulder);

        public override ControlState UiNextButton => ProcessButton(Buttons.LeftThumbstickDown, Buttons.DPadDown, Buttons.LeftThumbstickRight, Buttons.DPadRight);

        public override ControlState UiPrevButton => ProcessButton(Buttons.LeftThumbstickUp, Buttons.DPadUp, Buttons.LeftThumbstickLeft, Buttons.DPadLeft);

        public override ControlState UiAcceptButton => ProcessButton(Buttons.A);

        protected override ControlState GetFieldCell(int x, int y)
        {
            if (x == 0 && y == 0) return ProcessButton(Buttons.LeftTrigger);
            if (x == 1 && y == 0) return ProcessButton(Buttons.Y);
            if (x == 2 && y == 0) return ProcessButton(Buttons.RightTrigger);
            if (x == 0 && y == 1) return ProcessButton(Buttons.X);
            if (x == 1 && y == 1) return ProcessButton(Buttons.A);
            if (x == 2 && y == 1) return ProcessButton(Buttons.B);
            return ControlState.None;
        }

        protected override bool CheckButton(Buttons button) => _state.IsButtonDown(button);

        public override void Refresh()
        {
            _state = GamePad.GetState(PlayerIndex.One);
            GamePad.SetVibration(PlayerIndex.One, VibrateLeft, VibrateRight);
            base.Refresh();
        }
    }
}