using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Salamax
{
  public partial class Main : Form
  {
    private LogViewer _logWindow = null;
    private CodeNotifier _notifier = null;

    public Main()
    {
      InitializeComponent();
    }

    private void Main_Load(object sender, EventArgs e)
    {
      var asm = System.Reflection.Assembly.GetExecutingAssembly();
      var name = asm.GetName();
      this.Text = name.Name + " v" + name.Version.ToString(3);

      _ShowHideLogWindow();

      Logger.WriteLine("Main window opened.", Logger.Type.Information);

      var th = new System.Threading.Thread(new System.Threading.ThreadStart(_UpdateConnectionStatus));
      th.IsBackground = true;
      th.Start();

      _notifier = new CodeNotifier();
      ButtonStateChanges.AddNotifier(_notifier);
    }

    private void _UpdateConnectionStatus()
    {
      while (true)
      {
        _UpdateLabels();

        System.Threading.Thread.Sleep(1000);
      }
    }

    private void _UpdateLabels()
    {
      try
      {
        if (this.InvokeRequired)
        {
          this.Invoke(new MethodInvoker(_UpdateLabels));
        }
        else
        {
          if (Startup.Service != null)
          {
            if (Startup.Service.IsSilverlinkConnected)
            {
              lblSilverlinkStatus.Text = "Silverlink Cable: Connected";
              lblSilverlinkStatus.ForeColor = Color.Green;
            }
            else
            {
              lblSilverlinkStatus.Text = "Silverlink Cable: Not Connected";
              lblSilverlinkStatus.ForeColor = Color.Red;
            }
            _CenterLabel(lblSilverlinkStatus);

            if (Startup.Service.IsControllerConnected)
            {
              lblControllerStatus.Text = "Wired Xbox 360 Controller: Connected";
              lblControllerStatus.ForeColor = Color.Green;
            }
            else
            {
              lblControllerStatus.Text = "Wired Xbox 360 Controller: Not Connected";
              lblControllerStatus.ForeColor = Color.Red;
            }
            _CenterLabel(lblControllerStatus);
          }
        }
      }
      catch
      {
        //Eat it...
      }
    }

    private void _CenterLabel(Control ctrl)
    {
      ctrl.Location = new Point(((this.Width - ctrl.Width) / 2), ctrl.Location.Y);
    }

    private void _ShowHideLogWindow()
    {
      if (_logWindow == null)
      {
        _logWindow = new LogViewer();
        _logWindow.StartPosition = FormStartPosition.Manual;
        _logWindow.Visible = true;
        int offset = 0;
        if (_logWindow.Width > this.Width)
          offset = 0 - (_logWindow.Width - this.Width) / 2;
        else
          offset = (this.Width - _logWindow.Width) / 2;
        _logWindow.Location = new Point(this.Location.X + offset, this.Location.Y + this.Size.Height + 10);
      }

      if (_logWindow.Visible)
        _logWindow.Hide();
      else
        _logWindow.Show();
    }

    private void mniShowHideLog_Click(object sender, EventArgs e)
    {
      _ShowHideLogWindow();
    }

    private void mniAbout_Click(object sender, EventArgs e)
    {
      MessageBox.Show(this.Text +
        "\r\n\r\nBrandon Wilson\r\n\r\nFeel free to do whatever you want with this application.", this.Text,
        MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    private void mniExit_Click(object sender, EventArgs e)
    {
      _logWindow.ForceClose();
      this.Close();
    }

    private void btnConnectWiiRemote_Click(object sender, EventArgs e)
    {
      try { Startup.Service.Wiimote.Connect(); Logger.WriteLine("Wii remote connected successfully.", Logger.Type.Information);  }
      catch { Logger.WriteLine("Wii remote did not connect successfully.", Logger.Type.Error); };
    }

    private void btnDisconnectWiiRemote_Click(object sender, EventArgs e)
    {
      try { Startup.Service.Wiimote.Disconnect(); Logger.WriteLine("Wii remote disconnected successfully.", Logger.Type.Information); }
      catch { Logger.WriteLine("Wii remote did not disconnect successfully.", Logger.Type.Error); };
    }

    private void chkUp_CheckedChanged(object sender, EventArgs e)
    {
      _notifier.Up = chkUp.Checked;
    }

    private void chkDown_CheckedChanged(object sender, EventArgs e)
    {
      _notifier.Down = chkDown.Checked;
    }

    private void chkLeft_CheckedChanged(object sender, EventArgs e)
    {
      _notifier.Left = chkLeft.Checked;
    }

    private void chkRight_CheckedChanged(object sender, EventArgs e)
    {
      _notifier.Right = chkRight.Checked;
    }

    private void chkA_CheckedChanged(object sender, EventArgs e)
    {
      _notifier.A = chkA.Checked;
    }

    private void chkB_CheckedChanged(object sender, EventArgs e)
    {
      _notifier.B = chkB.Checked;
    }

    private void chkX_CheckedChanged(object sender, EventArgs e)
    {
      _notifier.X = chkX.Checked;
    }

    private void chkY_CheckedChanged(object sender, EventArgs e)
    {
      _notifier.Y = chkY.Checked;
    }

    private void chkGuide_CheckedChanged(object sender, EventArgs e)
    {
      _notifier.Guide = chkGuide.Checked;
    }

    private void chkLT_CheckedChanged(object sender, EventArgs e)
    {
      _notifier.LeftTrigger = (byte)(chkLT.Checked ? 0xFF : 0x00);
    }

    private void chkRT_CheckedChanged(object sender, EventArgs e)
    {
      _notifier.RightTrigger = (byte)(chkRT.Checked ? 0xFF : 0x00);
    }

    private void chkLB_CheckedChanged(object sender, EventArgs e)
    {
      _notifier.LeftButton = chkLB.Checked;
    }

    private void chkRB_CheckedChanged(object sender, EventArgs e)
    {
      _notifier.RightButton = chkRB.Checked;
    }

    private void chkBack_CheckedChanged(object sender, EventArgs e)
    {
      _notifier.Back = chkBack.Checked;
    }

    private void mniEnableKeyboardControl_Click(object sender, EventArgs e)
    {
      KeyboardInfo.KeysEnabled = true;
    }

    private void mniDisableKeyboardControl_Click(object sender, EventArgs e)
    {
      KeyboardInfo.KeysEnabled = false;
    }
  }
}
