#pragma once

#include "..\\..\\IOS7Crypt.h"
#include "Muck.h"
#include "winsparkle.h"

namespace IOS7CryptGUI {
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void) {
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//

			win_sparkle_set_appcast_url("https://downloads.yellosoft.us/apps/ios7crypt-changelog-windows.xml");
			win_sparkle_init();
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1() {
			win_sparkle_cleanup();

			if (components) {
				delete components;
			}
		}
	private: System::Windows::Forms::Label^  passwordLabel;
	private: System::Windows::Forms::Label^  hashLabel;
	private: System::Windows::Forms::TextBox^  passwordBox;
	private: System::Windows::Forms::TextBox^  hashBox;
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	private: System::Windows::Forms::ToolStripMenuItem^  fileMenuItem;

	private: System::Windows::Forms::ToolStripMenuItem^  checkForUpdatesItem;
	private: System::Windows::Forms::ToolStripMenuItem^  exitItem;


	protected: 

	protected: 




	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->passwordLabel = (gcnew System::Windows::Forms::Label());
			this->hashLabel = (gcnew System::Windows::Forms::Label());
			this->passwordBox = (gcnew System::Windows::Forms::TextBox());
			this->hashBox = (gcnew System::Windows::Forms::TextBox());
			this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
			this->fileMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->checkForUpdatesItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->exitItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->menuStrip1->SuspendLayout();
			this->SuspendLayout();
			// 
			// passwordLabel
			// 
			this->passwordLabel->AutoSize = true;
			this->passwordLabel->Location = System::Drawing::Point(12, 32);
			this->passwordLabel->Name = L"passwordLabel";
			this->passwordLabel->Size = System::Drawing::Size(53, 13);
			this->passwordLabel->TabIndex = 0;
			this->passwordLabel->Text = L"Password";
			// 
			// hashLabel
			// 
			this->hashLabel->AutoSize = true;
			this->hashLabel->Location = System::Drawing::Point(12, 54);
			this->hashLabel->Name = L"hashLabel";
			this->hashLabel->Size = System::Drawing::Size(32, 13);
			this->hashLabel->TabIndex = 1;
			this->hashLabel->Text = L"Hash";
			// 
			// passwordBox
			// 
			this->passwordBox->Location = System::Drawing::Point(71, 29);
			this->passwordBox->Name = L"passwordBox";
			this->passwordBox->Size = System::Drawing::Size(100, 20);
			this->passwordBox->TabIndex = 2;
			this->passwordBox->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::passwordEntered);
			// 
			// hashBox
			// 
			this->hashBox->Location = System::Drawing::Point(71, 51);
			this->hashBox->Name = L"hashBox";
			this->hashBox->Size = System::Drawing::Size(100, 20);
			this->hashBox->TabIndex = 3;
			this->hashBox->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::hashEntered);
			// 
			// menuStrip1
			// 
			this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->fileMenuItem});
			this->menuStrip1->Location = System::Drawing::Point(0, 0);
			this->menuStrip1->Name = L"menuStrip1";
			this->menuStrip1->Size = System::Drawing::Size(192, 24);
			this->menuStrip1->TabIndex = 4;
			this->menuStrip1->Text = L"menuStrip1";
			// 
			// fileMenuItem
			// 
			this->fileMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->checkForUpdatesItem, 
				this->exitItem});
			this->fileMenuItem->Name = L"fileMenuItem";
			this->fileMenuItem->Size = System::Drawing::Size(37, 20);
			this->fileMenuItem->Text = L"File";
			// 
			// checkForUpdatesItem
			// 
			this->checkForUpdatesItem->Name = L"checkForUpdatesItem";
			this->checkForUpdatesItem->Size = System::Drawing::Size(171, 22);
			this->checkForUpdatesItem->Text = L"Check for Updates";
			this->checkForUpdatesItem->Click += gcnew System::EventHandler(this, &Form1::clickCheckForUpdates);
			// 
			// exitItem
			// 
			this->exitItem->Name = L"exitItem";
			this->exitItem->Size = System::Drawing::Size(171, 22);
			this->exitItem->Text = L"Exit";
			this->exitItem->Click += gcnew System::EventHandler(this, &Form1::clickExit);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(192, 107);
			this->Controls->Add(this->hashBox);
			this->Controls->Add(this->passwordBox);
			this->Controls->Add(this->hashLabel);
			this->Controls->Add(this->passwordLabel);
			this->Controls->Add(this->menuStrip1);
			this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
			this->KeyPreview = true;
			this->MaximizeBox = false;
			this->MinimizeBox = false;
			this->Name = L"Form1";
			this->Text = L"IOS7Crypt";
			this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::formKeyPressed);
			this->menuStrip1->ResumeLayout(false);
			this->menuStrip1->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void passwordEntered(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
		if (e->KeyChar==(char) System::Windows::Forms::Keys::Enter) {
			std::string password=Muck::SysToStd(this->passwordBox->Text);
			std::string hash=IOS7Crypt::encrypt(password);

			this->hashBox->Text=Muck::StdToSys(hash);
			e->Handled=true; // Silence system beep
		}
	}

	private: System::Void hashEntered(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
		if (e->KeyChar==(char) System::Windows::Forms::Keys::Enter) {
			std::string hash=Muck::SysToStd(this->hashBox->Text);
			std::string password=IOS7Crypt::decrypt(hash);

			this->passwordBox->Text=Muck::StdToSys(password);
			e->Handled=true; // Silence system beep
		}
	}

	private: System::Void formKeyPressed(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) {
		if (e->Control && (e->KeyCode == Keys::Q)) {
			e->Handled=true; // Silence system beep
			this->Close();
		}
	}

	private: System::Void menuStrip1_ItemClicked(System::Object^ sender, System::Windows::Forms::ToolStripItemClickedEventArgs^ e) {
	}

	private: System::Void clickExit(System::Object^ sender, System::EventArgs^ e) {
		delete this; //exit(0);
	}

	private: System::Void clickCheckForUpdates(System::Object^ sender, System::EventArgs^ e) {
		win_sparkle_check_update_with_ui();
	}
};
}