#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;


namespace LifeSim {

	/// <summary>
	/// Summary for PrefDialog
	///
	/// 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 PrefDialog : public System::Windows::Forms::Form
	{
	public:
		PrefDialog(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}
	private: System::Windows::Forms::ColorDialog^  colorDialog;
	public: 

	// set these, return these
	ref struct Preferences
		{
		Color aliveColor_,deadColor_,gridColor_,bigGridColor_;
		enum class Engine {HashEngine, BasicEngine, FastEngine, SparseEngine} engine_;
		bool checkEngine_, wraparound_;
		Preferences % operator=(const Preferences % p)
			{
			checkEngine_  = p.checkEngine_  ;
			wraparound_   = p.wraparound_   ;
			aliveColor_   = p.aliveColor_   ; 
			deadColor_    = p.deadColor_    ;
			gridColor_    = p.gridColor_    ;
			bigGridColor_ = p.bigGridColor_ ;
			engine_       = p.engine_       ;
			return *this;
			}
		Preferences(void) : checkEngine_(false), wraparound_(false),
			aliveColor_(Color::White), deadColor_(Color::Black),
			gridColor_(Color::Gray),bigGridColor_(Color::LightGray),
			engine_(Engine::HashEngine)
			{
			}
		} preferences_;

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~PrefDialog()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::GroupBox^  groupBox3;
	protected: 
	private: System::Windows::Forms::Button^  resetColor;
	private: System::Windows::Forms::PictureBox^  aliveColorBox;
	private: System::Windows::Forms::PictureBox^  lightGridColorBox;
	private: System::Windows::Forms::Label^  label11;
	private: System::Windows::Forms::PictureBox^  gridColorBox;
	private: System::Windows::Forms::Label^  label13;
	private: System::Windows::Forms::PictureBox^  deadColorBox;
	private: System::Windows::Forms::Label^  label16;
	private: System::Windows::Forms::Label^  label20;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::CheckBox^  wrapBox;
	private: System::Windows::Forms::CheckBox^  checkEngine;
	private: System::Windows::Forms::ComboBox^  engineBox;
	private: System::Windows::Forms::GroupBox^  groupBox1;
	private: System::Windows::Forms::Button^  okButton;
	private: System::Windows::Forms::Button^  cancelButton;


	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)
		{
		this->groupBox3 = (gcnew System::Windows::Forms::GroupBox());
		this->resetColor = (gcnew System::Windows::Forms::Button());
		this->aliveColorBox = (gcnew System::Windows::Forms::PictureBox());
		this->lightGridColorBox = (gcnew System::Windows::Forms::PictureBox());
		this->label11 = (gcnew System::Windows::Forms::Label());
		this->gridColorBox = (gcnew System::Windows::Forms::PictureBox());
		this->label13 = (gcnew System::Windows::Forms::Label());
		this->deadColorBox = (gcnew System::Windows::Forms::PictureBox());
		this->label16 = (gcnew System::Windows::Forms::Label());
		this->label20 = (gcnew System::Windows::Forms::Label());
		this->label4 = (gcnew System::Windows::Forms::Label());
		this->wrapBox = (gcnew System::Windows::Forms::CheckBox());
		this->checkEngine = (gcnew System::Windows::Forms::CheckBox());
		this->engineBox = (gcnew System::Windows::Forms::ComboBox());
		this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
		this->okButton = (gcnew System::Windows::Forms::Button());
		this->cancelButton = (gcnew System::Windows::Forms::Button());
		this->colorDialog = (gcnew System::Windows::Forms::ColorDialog());
		this->groupBox3->SuspendLayout();
		(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->aliveColorBox))->BeginInit();
		(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->lightGridColorBox))->BeginInit();
		(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->gridColorBox))->BeginInit();
		(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->deadColorBox))->BeginInit();
		this->groupBox1->SuspendLayout();
		this->SuspendLayout();
		// 
		// groupBox3
		// 
		this->groupBox3->Controls->Add(this->resetColor);
		this->groupBox3->Controls->Add(this->aliveColorBox);
		this->groupBox3->Controls->Add(this->lightGridColorBox);
		this->groupBox3->Controls->Add(this->label11);
		this->groupBox3->Controls->Add(this->gridColorBox);
		this->groupBox3->Controls->Add(this->label13);
		this->groupBox3->Controls->Add(this->deadColorBox);
		this->groupBox3->Controls->Add(this->label16);
		this->groupBox3->Controls->Add(this->label20);
		this->groupBox3->Location = System::Drawing::Point(12, 181);
		this->groupBox3->Name = L"groupBox3";
		this->groupBox3->Size = System::Drawing::Size(148, 184);
		this->groupBox3->TabIndex = 21;
		this->groupBox3->TabStop = false;
		this->groupBox3->Text = L"Color";
		// 
		// resetColor
		// 
		this->resetColor->Location = System::Drawing::Point(33, 145);
		this->resetColor->Name = L"resetColor";
		this->resetColor->Size = System::Drawing::Size(75, 23);
		this->resetColor->TabIndex = 15;
		this->resetColor->Text = L"Reset";
		this->resetColor->UseVisualStyleBackColor = true;
		this->resetColor->Click += gcnew System::EventHandler(this, &PrefDialog::resetColor_Click);
		// 
		// aliveColorBox
		// 
		this->aliveColorBox->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
		this->aliveColorBox->Location = System::Drawing::Point(93, 29);
		this->aliveColorBox->Name = L"aliveColorBox";
		this->aliveColorBox->Size = System::Drawing::Size(46, 23);
		this->aliveColorBox->TabIndex = 14;
		this->aliveColorBox->TabStop = false;
		this->aliveColorBox->Click += gcnew System::EventHandler(this, &PrefDialog::aliveColorBox_Click);
		// 
		// lightGridColorBox
		// 
		this->lightGridColorBox->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
		this->lightGridColorBox->Location = System::Drawing::Point(93, 116);
		this->lightGridColorBox->Name = L"lightGridColorBox";
		this->lightGridColorBox->Size = System::Drawing::Size(46, 23);
		this->lightGridColorBox->TabIndex = 14;
		this->lightGridColorBox->TabStop = false;
		this->lightGridColorBox->Click += gcnew System::EventHandler(this, &PrefDialog::lightGridColorBox_Click);
		// 
		// label11
		// 
		this->label11->AutoSize = true;
		this->label11->Location = System::Drawing::Point(12, 30);
		this->label11->Name = L"label11";
		this->label11->Size = System::Drawing::Size(56, 13);
		this->label11->TabIndex = 13;
		this->label11->Text = L"Alive color";
		// 
		// gridColorBox
		// 
		this->gridColorBox->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
		this->gridColorBox->Location = System::Drawing::Point(93, 87);
		this->gridColorBox->Name = L"gridColorBox";
		this->gridColorBox->Size = System::Drawing::Size(46, 23);
		this->gridColorBox->TabIndex = 14;
		this->gridColorBox->TabStop = false;
		this->gridColorBox->Click += gcnew System::EventHandler(this, &PrefDialog::gridColorBox_Click);
		// 
		// label13
		// 
		this->label13->AutoSize = true;
		this->label13->Location = System::Drawing::Point(12, 58);
		this->label13->Name = L"label13";
		this->label13->Size = System::Drawing::Size(59, 13);
		this->label13->TabIndex = 13;
		this->label13->Text = L"Dead color";
		// 
		// deadColorBox
		// 
		this->deadColorBox->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
		this->deadColorBox->Location = System::Drawing::Point(93, 58);
		this->deadColorBox->Name = L"deadColorBox";
		this->deadColorBox->Size = System::Drawing::Size(46, 23);
		this->deadColorBox->TabIndex = 14;
		this->deadColorBox->TabStop = false;
		this->deadColorBox->Click += gcnew System::EventHandler(this, &PrefDialog::deadColorBox_Click);
		// 
		// label16
		// 
		this->label16->AutoSize = true;
		this->label16->Location = System::Drawing::Point(12, 87);
		this->label16->Name = L"label16";
		this->label16->Size = System::Drawing::Size(52, 13);
		this->label16->TabIndex = 13;
		this->label16->Text = L"Grid color";
		// 
		// label20
		// 
		this->label20->AutoSize = true;
		this->label20->Location = System::Drawing::Point(12, 116);
		this->label20->Name = L"label20";
		this->label20->Size = System::Drawing::Size(76, 13);
		this->label20->TabIndex = 13;
		this->label20->Text = L"Light grid color";
		// 
		// label4
		// 
		this->label4->AutoSize = true;
		this->label4->Location = System::Drawing::Point(-3, 104);
		this->label4->Margin = System::Windows::Forms::Padding(2, 0, 2, 0);
		this->label4->Name = L"label4";
		this->label4->Size = System::Drawing::Size(239, 13);
		this->label4->TabIndex = 20;
		this->label4->Text = L"TODO - check on pixels set, not just internal cells";
		// 
		// wrapBox
		// 
		this->wrapBox->AutoSize = true;
		this->wrapBox->Enabled = false;
		this->wrapBox->Location = System::Drawing::Point(6, 69);
		this->wrapBox->Name = L"wrapBox";
		this->wrapBox->Size = System::Drawing::Size(125, 17);
		this->wrapBox->TabIndex = 18;
		this->wrapBox->Text = L"Wraparound (TODO)";
		this->wrapBox->UseVisualStyleBackColor = true;
		// 
		// checkEngine
		// 
		this->checkEngine->AutoSize = true;
		this->checkEngine->Enabled = false;
		this->checkEngine->Location = System::Drawing::Point(6, 46);
		this->checkEngine->Name = L"checkEngine";
		this->checkEngine->Size = System::Drawing::Size(158, 17);
		this->checkEngine->TabIndex = 19;
		this->checkEngine->Text = L"Check engine against Basic";
		this->checkEngine->UseVisualStyleBackColor = true;
		// 
		// engineBox
		// 
		this->engineBox->Enabled = false;
		this->engineBox->FormattingEnabled = true;
		this->engineBox->Items->AddRange(gcnew cli::array< System::Object^  >(4) {L"Hash", L"Basic", L"Fast", L"Sparse"});
		this->engineBox->Location = System::Drawing::Point(6, 19);
		this->engineBox->Name = L"engineBox";
		this->engineBox->Size = System::Drawing::Size(121, 21);
		this->engineBox->TabIndex = 16;
		this->engineBox->Text = L"Hash";
		this->engineBox->SelectedIndexChanged += gcnew System::EventHandler(this, &PrefDialog::engineBox_SelectedIndexChanged);
		// 
		// groupBox1
		// 
		this->groupBox1->Controls->Add(this->engineBox);
		this->groupBox1->Controls->Add(this->checkEngine);
		this->groupBox1->Controls->Add(this->label4);
		this->groupBox1->Controls->Add(this->wrapBox);
		this->groupBox1->Location = System::Drawing::Point(12, 12);
		this->groupBox1->Name = L"groupBox1";
		this->groupBox1->Size = System::Drawing::Size(227, 163);
		this->groupBox1->TabIndex = 22;
		this->groupBox1->TabStop = false;
		this->groupBox1->Text = L"Engine";
		// 
		// okButton
		// 
		this->okButton->Location = System::Drawing::Point(173, 342);
		this->okButton->Name = L"okButton";
		this->okButton->Size = System::Drawing::Size(75, 23);
		this->okButton->TabIndex = 23;
		this->okButton->Text = L"Apply";
		this->okButton->UseVisualStyleBackColor = true;
		this->okButton->Click += gcnew System::EventHandler(this, &PrefDialog::okButton_Click);
		// 
		// cancelButton
		// 
		this->cancelButton->Location = System::Drawing::Point(173, 313);
		this->cancelButton->Name = L"cancelButton";
		this->cancelButton->Size = System::Drawing::Size(75, 23);
		this->cancelButton->TabIndex = 23;
		this->cancelButton->Text = L"Cancel";
		this->cancelButton->UseVisualStyleBackColor = true;
		this->cancelButton->Click += gcnew System::EventHandler(this, &PrefDialog::cancelButton_Click);
		// 
		// colorDialog
		// 
		this->colorDialog->AnyColor = true;
		this->colorDialog->FullOpen = true;
		// 
		// PrefDialog
		// 
		this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
		this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
		this->ClientSize = System::Drawing::Size(253, 380);
		this->Controls->Add(this->cancelButton);
		this->Controls->Add(this->okButton);
		this->Controls->Add(this->groupBox1);
		this->Controls->Add(this->groupBox3);
		this->Name = L"PrefDialog";
		this->Text = L"Preferences...";
		this->groupBox3->ResumeLayout(false);
		this->groupBox3->PerformLayout();
		(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->aliveColorBox))->EndInit();
		(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->lightGridColorBox))->EndInit();
		(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->gridColorBox))->EndInit();
		(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->deadColorBox))->EndInit();
		this->groupBox1->ResumeLayout(false);
		this->groupBox1->PerformLayout();
		this->ResumeLayout(false);

			}
#pragma endregion
private: System::Void engineBox_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) 
			 {
			 if (engineBox->SelectedText == "Hash")
				 preferences_.engine_ = Preferences::Engine::HashEngine;
			 else if (engineBox->SelectedText == "Basic")
				 preferences_.engine_ = Preferences::Engine::BasicEngine;
			 else if (engineBox->SelectedText == "Fast")
				 preferences_.engine_ = Preferences::Engine::FastEngine;
			 else if (engineBox->SelectedText == "Sparse")
				 preferences_.engine_ = Preferences::Engine::SparseEngine;
			 else if (engineBox->SelectedText == "Basic")
				 MessageBox::Show("Unknown engine type");
			 }
private: System::Void okButton_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
			 DialogResult = Windows::Forms::DialogResult::OK;
			 Close();
			 }
private: System::Void cancelButton_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
			 DialogResult = Windows::Forms::DialogResult::Cancel;
			 Close();
			 }
private: void ResetColor(void)
			 {
			 Preferences pref2;
			 preferences_.aliveColor_ = pref2.aliveColor_;
			 preferences_.bigGridColor_ = pref2.bigGridColor_;
			 preferences_.deadColor_ = pref2.deadColor_;
			 preferences_.gridColor_ = pref2.gridColor_;

			 aliveColorBox->BackColor = preferences_.aliveColor_;
			 gridColorBox->BackColor = preferences_.gridColor_;
			 deadColorBox->BackColor = preferences_.deadColor_;
			 lightGridColorBox->BackColor = preferences_.bigGridColor_;
			 } // ResetColor
private: System::Void resetColor_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
			 ResetColor();
			 }
private: System::Void aliveColorBox_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
			 if (System::Windows::Forms::DialogResult::OK == colorDialog->ShowDialog())
				{
				aliveColorBox->BackColor = colorDialog->Color;
				preferences_.aliveColor_ = colorDialog->Color;
				}
			 }
private: System::Void deadColorBox_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
			if (System::Windows::Forms::DialogResult::OK == colorDialog->ShowDialog())
				{
				deadColorBox->BackColor = colorDialog->Color;
				preferences_.deadColor_= colorDialog->Color;
				}
			 }
private: System::Void gridColorBox_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
			if (System::Windows::Forms::DialogResult::OK == colorDialog->ShowDialog())
				{
				gridColorBox->BackColor = colorDialog->Color;
				preferences_.gridColor_= colorDialog->Color;
				}
			 }
private: System::Void lightGridColorBox_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
			if (System::Windows::Forms::DialogResult::OK == colorDialog->ShowDialog())
				{
				lightGridColorBox->BackColor = colorDialog->Color;
				preferences_.bigGridColor_= colorDialog->Color;
				}
			 }

public: System::Void SetPreferences(const PrefDialog::Preferences % pref) 
			 {
			 preferences_ = pref;
			 checkEngine->Checked = preferences_.checkEngine_;
			 wrapBox->Checked = preferences_.wraparound_;
			 aliveColorBox->BackColor = preferences_.aliveColor_;
			 gridColorBox->BackColor = preferences_.gridColor_;
			 deadColorBox->BackColor = preferences_.deadColor_;
			 lightGridColorBox->BackColor = preferences_.bigGridColor_;
			 switch (preferences_.engine_)
				 {
				 case Preferences::Engine::BasicEngine :
					 engineBox->SelectedText = "Basic";
					 break;
				 case Preferences::Engine::FastEngine :
					 engineBox->SelectedText = "Fast";
					 break;
				 case Preferences::Engine::HashEngine :
					 engineBox->SelectedText = "Hash";
					 break;
				 case Preferences::Engine::SparseEngine :
					 engineBox->SelectedText = "Sparse";
					 break;
				 default :
					 MessageBox::Show("Unknown engine during load");
					 break;
				 }
			 } // Load
};
}
