using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace LuaDecrypter
{
  public class KeyPopup : Form
  {
    private KeyValidator validator;
    private IContainer components;
    private TextBox serial1;
    private TextBox serial2;
    private Label seperator1;
    private Label seperator2;
    private TextBox serial3;
    private Button verifyButton;
    private TextBox statusBox;

    public KeyPopup(KeyValidator validator)
    {
      this.InitializeComponent();
      this.validator = validator;
    }

    private void SetStatus(string text)
    {
      if (this.statusBox.InvokeRequired)
        this.Invoke((Delegate) new KeyPopup.SetStatusCallback(this.SetStatus), new object[1]
        {
          (object) text
        });
      else
        this.statusBox.Text = text;
    }

    private void serial1_TextChanged(object sender, EventArgs e)
    {
      if (this.serial1.Text.Length != 5)
        return;
      ((Control) this.serial2).Select();
    }

    private void serial2_TextChanged(object sender, EventArgs e)
    {
      if (this.serial2.Text.Length != 5)
        return;
      ((Control) this.serial3).Select();
    }

    private void serial3_TextChanged(object sender, EventArgs e)
    {
      if (this.serial3.Text.Length != 5)
        return;
      this.verifyButton.Select();
    }

    private void verifyButton_Click(object sender, EventArgs e)
    {
      this.statusBox.Text = "Validating key...";
      BackgroundWorker backgroundWorker = new BackgroundWorker();
      backgroundWorker.DoWork += new DoWorkEventHandler(this.background_DoWork);
      backgroundWorker.RunWorkerAsync();
    }

    private void background_DoWork(object sender, DoWorkEventArgs e)
    {
      string key = string.Format("{0}-{1}-{2}", (object) this.serial1.Text, (object) this.serial2.Text, (object) this.serial3.Text);
      if (key.Length != 17)
      {
        this.SetStatus("Invalid key!");
      }
      else
      {
        this.validator.Validate(key, true);
        if (this.validator.IsValid())
          this.Invoke((Delegate) (() => this.Close()));
        else
          this.SetStatus("Invalid key!");
      }
    }

    protected override void Dispose(bool disposing)
    {
      if (disposing && this.components != null)
        this.components.Dispose();
      base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
      ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (KeyPopup));
      this.serial1 = new TextBox();
      this.serial2 = new TextBox();
      this.seperator1 = new Label();
      this.seperator2 = new Label();
      this.serial3 = new TextBox();
      this.verifyButton = new Button();
      this.statusBox = new TextBox();
      this.SuspendLayout();
      this.serial1.Location = new Point(18, 16);
      this.serial1.MaxLength = 5;
      this.serial1.Name = "serial1";
      this.serial1.Size = new Size(38, 20);
      this.serial1.TabIndex = 1;
      this.serial1.TextChanged += new EventHandler(this.serial1_TextChanged);
      this.serial2.Location = new Point(80, 16);
      this.serial2.MaxLength = 5;
      this.serial2.Name = "serial2";
      this.serial2.Size = new Size(38, 20);
      this.serial2.TabIndex = 2;
      this.serial2.TextChanged += new EventHandler(this.serial2_TextChanged);
      this.seperator1.AutoSize = true;
      this.seperator1.Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Regular, GraphicsUnit.Point, (byte) 0);
      this.seperator1.Location = new Point(63, 17);
      this.seperator1.Name = "seperator1";
      this.seperator1.Size = new Size(13, 17);
      this.seperator1.TabIndex = 0;
      this.seperator1.Text = "-";
      this.seperator2.AutoSize = true;
      this.seperator2.Font = new Font("Microsoft Sans Serif", 10f, FontStyle.Regular, GraphicsUnit.Point, (byte) 0);
      this.seperator2.Location = new Point(125, 17);
      this.seperator2.Name = "seperator2";
      this.seperator2.Size = new Size(13, 17);
      this.seperator2.TabIndex = 0;
      this.seperator2.Text = "-";
      this.serial3.Location = new Point(142, 16);
      this.serial3.MaxLength = 5;
      this.serial3.Name = "serial3";
      this.serial3.Size = new Size(38, 20);
      this.serial3.TabIndex = 3;
      this.serial3.TextChanged += new EventHandler(this.serial3_TextChanged);
      this.verifyButton.Location = new Point(18, 50);
      this.verifyButton.Name = "verifyButton";
      this.verifyButton.Size = new Size(162, 23);
      this.verifyButton.TabIndex = 6;
      this.verifyButton.Text = "Verify Key";
      this.verifyButton.UseVisualStyleBackColor = true;
      this.verifyButton.Click += new EventHandler(this.verifyButton_Click);
      this.statusBox.Enabled = false;
      this.statusBox.Location = new Point(18, 88);
      this.statusBox.Name = "statusBox";
      this.statusBox.Size = new Size(162, 20);
      this.statusBox.TabIndex = 7;
      this.statusBox.TextAlign = HorizontalAlignment.Center;
      this.AutoScaleDimensions = new SizeF(6f, 13f);
      this.AutoScaleMode = AutoScaleMode.Font;
      this.ClientSize = new Size(196, 123);
      this.Controls.Add((Control) this.statusBox);
      this.Controls.Add((Control) this.verifyButton);
      this.Controls.Add((Control) this.seperator2);
      this.Controls.Add((Control) this.serial3);
      this.Controls.Add((Control) this.seperator1);
      this.Controls.Add((Control) this.serial2);
      this.Controls.Add((Control) this.serial1);
      this.FormBorderStyle = FormBorderStyle.FixedDialog;
      this.Icon = (Icon) componentResourceManager.GetObject("$this.Icon");
      this.MaximizeBox = false;
      this.MinimizeBox = false;
      this.Name = "KeyPopup";
      this.ShowIcon = false;
      this.ShowInTaskbar = false;
      this.Text = "Product Key";
      this.ResumeLayout(false);
      this.PerformLayout();
    }

    private delegate void SetStatusCallback(string text);
  }
}