using System;
using System.Collections.Generic;
using System.Text;
using WiimoteLib;

namespace Salamax
{
  public class WiiRemote : IButtonNotifier
  {
    private Wiimote _remote = new Wiimote();

    public event EventHandler<EventArgs> ButtonStateChanged;
    public bool A { get; set; }
    public bool B { get; set; }
    public bool X { get; set; }
    public bool Y { get; set; }
    public bool Start { get; set; }
    public bool Back { get; set; }
    public bool Guide { get; set; }
    public bool Up { get; set; }
    public bool Down { get; set; }
    public bool Left { get; set; }
    public bool Right { get; set; }
    public bool LeftButton { get; set; }
    public bool RightButton { get; set; }
    public byte LeftTrigger { get; set; }
    public byte RightTrigger { get; set; }
    public short LeftX { get; set; }
    public short LeftY { get; set; }
    public short RightX { get; set; }
    public short RightY { get; set; }

    public WiiRemote()
    {
      _remote.WiimoteChanged += _remote_WiimoteChanged;
    }

    public void Connect()
    {
      _remote.Connect();
      _remote.SetLEDs(true, false, false, false);
    }

    public void Disconnect()
    {
      _remote.Disconnect();
    }

    private void _remote_WiimoteChanged(object sender, WiimoteChangedEventArgs e)
    {
      A = e.WiimoteState.ButtonState.A || e.WiimoteState.ButtonState.One || e.WiimoteState.ButtonState.Two;
      B = e.WiimoteState.ButtonState.B;
      Up = e.WiimoteState.ButtonState.Right;
      Down = e.WiimoteState.ButtonState.Left;
      Left = e.WiimoteState.ButtonState.Up;
      Right = e.WiimoteState.ButtonState.Down;
      Guide = e.WiimoteState.ButtonState.Home;
      Start = e.WiimoteState.ButtonState.Plus;

      if (ButtonStateChanged != null)
        ButtonStateChanged(this, EventArgs.Empty);
    }
  }
}
