#pragma once

//#include "my_functions.h"

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

struct PopisKanalu
{
	int PoradiKanalu;
	char PopisKanaluSelection[20];
	bool Existuje;
	bool Smer;			// true - input, false - output
};

extern PopisKanalu Vstupy[16];
extern PopisKanalu Kanaly[16];


namespace Generator_20 {






	/// <summary>
	/// Summary for form2
	///
	/// 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 form2 : public System::Windows::Forms::Form
	{
	public:
		form2(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			this->listBox1->SelectionMode = SelectionMode::MultiExtended;
			this->comboBox1->SelectedIndex::set(0);			
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~form2()
		{
			if (components)
			{
				delete components;
			}
		}

	protected: 
	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::ListBox^  listBox1;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Button^  button3;
	private: System::Windows::Forms::Button^  button6;
	private: System::Windows::Forms::Button^  button7;
	private: System::Windows::Forms::Button^  button4;
	private: System::Windows::Forms::Button^  button5;
	private: System::Windows::Forms::ComboBox^  comboBox1;
	private: System::Windows::Forms::Button^  button8;

	private: 

	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->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
			this->button4 = (gcnew System::Windows::Forms::Button());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->button5 = (gcnew System::Windows::Forms::Button());
			this->listBox1 = (gcnew System::Windows::Forms::ListBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->button7 = (gcnew System::Windows::Forms::Button());
			this->button6 = (gcnew System::Windows::Forms::Button());
			this->button8 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// comboBox1
			// 
			this->comboBox1->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->comboBox1->FormattingEnabled = true;
			this->comboBox1->ImeMode = System::Windows::Forms::ImeMode::NoControl;
			this->comboBox1->Items->AddRange(gcnew cli::array< System::Object^  >(2) {L"Vstup ", L"Výstup"});
			this->comboBox1->Location = System::Drawing::Point(155, 112);
			this->comboBox1->MaxDropDownItems = 2;
			this->comboBox1->Name = L"comboBox1";
			this->comboBox1->Size = System::Drawing::Size(58, 21);
			this->comboBox1->TabIndex = 8;
			// 
			// button4
			// 
			this->button4->Location = System::Drawing::Point(154, 179);
			this->button4->Name = L"button4";
			this->button4->Size = System::Drawing::Size(57, 21);
			this->button4->TabIndex = 10;
			this->button4->Text = L"Down";
			this->button4->UseVisualStyleBackColor = true;
			this->button4->Click += gcnew System::EventHandler(this, &form2::button4_Click);
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(11, 9);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(45, 13);
			this->label2->TabIndex = 6;
			this->label2->Text = L"Linka č.";
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(76, 25);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(59, 20);
			this->textBox2->TabIndex = 4;
			this->textBox2->Text = L"1";
			// 
			// button5
			// 
			this->button5->Location = System::Drawing::Point(154, 151);
			this->button5->Name = L"button5";
			this->button5->Size = System::Drawing::Size(58, 22);
			this->button5->TabIndex = 9;
			this->button5->Text = L"Up";
			this->button5->UseVisualStyleBackColor = true;
			this->button5->Click += gcnew System::EventHandler(this, &form2::button5_Click);
			// 
			// listBox1
			// 
			this->listBox1->FormattingEnabled = true;
			this->listBox1->Location = System::Drawing::Point(12, 51);
			this->listBox1->Name = L"listBox1";
			this->listBox1->Size = System::Drawing::Size(123, 212);
			this->listBox1->TabIndex = 0;
			this->listBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &form2::listBox1_SelectedIndexChanged);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(75, 9);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(38, 13);
			this->label1->TabIndex = 3;
			this->label1->Text = L"Název";
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(11, 25);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(59, 20);
			this->textBox1->TabIndex = 3;
			this->textBox1->Text = L"1";
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(154, 54);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(59, 23);
			this->button2->TabIndex = 6;
			this->button2->Text = L"Odebrat";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &form2::button2_Click);
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(154, 25);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(59, 23);
			this->button1->TabIndex = 5;
			this->button1->Text = L"Přidat";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &form2::button1_Click);
			// 
			// button3
			// 
			this->button3->Location = System::Drawing::Point(154, 83);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(59, 23);
			this->button3->TabIndex = 7;
			this->button3->Text = L"Edituj";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &form2::button3_Click);
			// 
			// button7
			// 
			this->button7->Location = System::Drawing::Point(11, 269);
			this->button7->Name = L"button7";
			this->button7->Size = System::Drawing::Size(98, 33);
			this->button7->TabIndex = 1;
			this->button7->Text = L"OK";
			this->button7->UseVisualStyleBackColor = true;
			this->button7->Click += gcnew System::EventHandler(this, &form2::button7_Click);
			// 
			// button6
			// 
			this->button6->Location = System::Drawing::Point(115, 269);
			this->button6->Name = L"button6";
			this->button6->Size = System::Drawing::Size(98, 33);
			this->button6->TabIndex = 2;
			this->button6->Text = L"Cancel";
			this->button6->UseVisualStyleBackColor = true;
			this->button6->Click += gcnew System::EventHandler(this, &form2::button6_Click);
			// 
			// button8
			// 
			this->button8->Location = System::Drawing::Point(154, 220);
			this->button8->Name = L"button8";
			this->button8->Size = System::Drawing::Size(57, 21);
			this->button8->TabIndex = 11;
			this->button8->Text = L"Reset";
			this->button8->UseVisualStyleBackColor = true;
			this->button8->Click += gcnew System::EventHandler(this, &form2::button8_Click);
			// 
			// form2
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(224, 310);
			this->Controls->Add(this->button8);
			this->Controls->Add(this->comboBox1);
			this->Controls->Add(this->button7);
			this->Controls->Add(this->button4);
			this->Controls->Add(this->button6);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->listBox1);
			this->Controls->Add(this->button5);
			this->Controls->Add(this->button3);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->textBox1);
			this->Name = L"form2";
			this->Text = L"Nastavení linek";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Cancel button click XXXXXXXXXXXXXXXXXXXXXXXX
private: System::Void button6_Click(System::Object^  sender, System::EventArgs^  e) {
			this->Hide();
				 
			System::String^ TempNazev;
			int TempKanal;
						
			while(this->listBox1->Items->Count > 0) 
			{	this->listBox1->Items->RemoveAt(0);}

			int i=0;
			while( Kanaly[i].Existuje == true)
			{
				TempKanal = Kanaly[i].PoradiKanalu;
				TempNazev = gcnew String(Kanaly[i].PopisKanaluSelection);
			
			if(Kanaly[i].Smer == true)
				{
					if(TempKanal <10)
						TempNazev = "  " + TempKanal + "    Vstup       " + TempNazev; 
					else
						TempNazev = TempKanal + "    Vstup       " + TempNazev;
				}
				else
				{
					if(TempKanal <10)
						TempNazev = "  " + TempKanal + "    Výstup     " + TempNazev; 
					else
						TempNazev = TempKanal + "    Výstup     " + TempNazev;
				}

				listBox1->Items->Add(TempNazev);	//prida polozku do LB
				i++;
			}
			this->textBox1->Text = "";
			this->textBox2->Text = "";
			

			 }
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX OK button click XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
private: System::Void button7_Click(System::Object^  sender, System::EventArgs^  e) {
				this->Hide();
				for(int i=0;i<15;i++)
				{
					Kanaly[i] = Vstupy[i];
				}

		 }
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

private: System::Int32 NajdiNejmensi()
			 {
				bool nasel = false;
				bool pole[16];
				int lowest = 16;
				int vysledek = 16;

				for(int i =0;i<16;i++)
				{
					pole[i] = false;
				};
				for(lowest = 1;lowest<17;lowest++)
				{
					for(int i=0;i<16;i++)
					{
						if((Vstupy[i].PoradiKanalu == lowest) && (Vstupy[i].Existuje == true) )
						{
							pole[lowest-1]=true;
						};
					};
				};

				for(int i=15;i>-1;i--)
				{
					if(pole[i]==false)
					{
						vysledek = i;
					};
				};
				return(vysledek+1);
			 }
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXX  pridani polozky do listBoxu XXXXXXXXXXXXXXXXXXXXXXXX
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			
			System::String^ TempNazev;
			int TempKanal;
			int i = 0;
//---------------------nacteni parametru z TB----------------------------------------			
			TempNazev = textBox2->Text; //Nacteni nazvu kanalu
			try			//Nacteni cisla kanalu
			{TempKanal = Convert::ToInt32(textBox1->Text);}
			catch(FormatException^ e){
				e;
				textBox1->BackColor = Color::LightCoral;
				return;}
			catch(OverflowException^ e){
				e;
				textBox1->BackColor = Color::LightCoral;
			return;}
//-------------------------- zisteni zda zadane cislo kanalu jiz existuje-------------
			bool ex = false;
			for(i=0;i<16;i++)
			{
				if((Vstupy[i].PoradiKanalu == (TempKanal)) && (Vstupy[i].Existuje == true))
				{
					ex = true;
					textBox1->BackColor = Color::LightCoral;
				}
			}

			if((TempKanal>=1) && (TempKanal<=16) && (ex == false))
			{	
				textBox1->BackColor = SystemColors::Window;

//---------------------prida polozku do struktury---------------------------		
				Vstupy[this->listBox1->Items->Count::get()].Existuje = true;//nastavi pozadovany kanal na existujici
				Vstupy[this->listBox1->Items->Count::get()].PoradiKanalu = (short)TempKanal;//zapise poradi kanalu
				if(this->comboBox1->SelectedIndex == 0)
					{Vstupy[this->listBox1->Items->Count::get()].Smer = true;}//nastavi smer na input
				else{Vstupy[this->listBox1->Items->Count::get()].Smer = false;}//nastavi smer na output
				for(i=0;i<TempNazev->Length::get();i++)	//zapise do struktury vstupu nazev kanalu
				{
					Vstupy[this->listBox1->Items->Count::get()].PopisKanaluSelection[i] = (char)System::Convert::ToSByte(TempNazev[i]);
				}
//-------------------------------------------------------------------------
//----------------------prida polozku do LB-------------------------------
				if(this->comboBox1->SelectedIndex == 0)
				{
					if(TempKanal <10)
						TempNazev = "  " + TempKanal + "    Vstup       " + TempNazev; 
					else
						TempNazev = TempKanal + "    Vstup       " + TempNazev;
				}
				else
				{
					if(TempKanal <10)
						TempNazev = "  " + TempKanal + "    Výstup     " + TempNazev; 
					else
						TempNazev = TempKanal + "    Výstup     " + TempNazev;
				}

				listBox1->Items->Add(TempNazev);	//prida polozku do LB
//-------------------------------------------------------------------------
				this->textBox1->Text = "";
				this->textBox1->Text += NajdiNejmensi();
				this->textBox2->Text = "";
			}
			else{textBox1->BackColor = Color::LightCoral;}

	if((this->listBox1->Items->Count) >= 16)
			{this->button1->Enabled::set(false);}
		 }

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx  odebrani polozek ze seznamu  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
			 		
		int i;
		while(listBox1->SelectedItems->Count > 0)
			{
				for(i=this->listBox1->SelectedIndex;i<16;i++)
				{
					if(Vstupy[i+1].Existuje == true)
					{
						Vstupy[i] = Vstupy[i+1];
					}
					else
					{
						Vstupy[i].Existuje = false;
					}
				}
				listBox1->Items->Remove(listBox1->SelectedItem);
			}

		if((this->listBox1->Items->Count) < 16)
			{this->button1->Enabled::set(true);}
		this->textBox1->Text = "";
		this->textBox1->Text += NajdiNejmensi();
		this->textBox2->Text = "";
		 }
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
private: System::Void listBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
			this->textBox1->Text = "";
			this->textBox1->Text += Vstupy[this->listBox1->SelectedIndex].PoradiKanalu;
			this->textBox2->Text = "";
			int k = -1;
			do{k++;
			}while(Vstupy[this->listBox1->SelectedIndex].PopisKanaluSelection[k] != '\0');
			this->textBox2->Text = gcnew String(Vstupy[this->listBox1->SelectedIndex].PopisKanaluSelection);
			if(Vstupy[this->listBox1->SelectedIndex].Smer == true)
				{this->comboBox1->SelectedIndex=0;}
			else
				{this->comboBox1->SelectedIndex=1;}
		 }

//XXXXXXXXXXXXXXXXXXXXXXXX Editace XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
			
			System::String^ TempNazev;
			int TempKanal;
			int i = 0;
//---------------------nacteni parametru z TB----------------------------------------			
			TempNazev = textBox2->Text; //Nacteni nazvu kanalu
			try			//Nacteni cisla kanalu
			{TempKanal = Convert::ToInt32(textBox1->Text);}
			catch(FormatException^ e){
				e;
				textBox1->BackColor = Color::LightCoral;
				return;}
			catch(OverflowException^ e){
				e;
				textBox1->BackColor = Color::LightCoral;
			return;}
//-------------------------- pokud se kanal nemeni prepise strukturu -------------
			if(TempKanal == Vstupy[this->listBox1->SelectedIndex].PoradiKanalu)
			{
				if(this->comboBox1->SelectedIndex == 0)
					{Vstupy[this->listBox1->SelectedIndex].Smer = true;}//nastavi smer na input
				else{Vstupy[this->listBox1->SelectedIndex].Smer = false;}//nastavi smer na output
				
				for(i=0;i<TempNazev->Length::get();i++)	//zapise do struktury vstupu nazev kanalu
					{Vstupy[this->listBox1->SelectedIndex].PopisKanaluSelection[i] = (char)System::Convert::ToSByte(TempNazev[i]);}
				Vstupy[this->listBox1->SelectedIndex].PopisKanaluSelection[i] = '\0';

				if(this->comboBox1->SelectedIndex == 0)
				{
					if(TempKanal <10)
						TempNazev = "  " + TempKanal + "    Vstup       " + TempNazev; 
					else
						TempNazev = TempKanal + "    Vstup       " + TempNazev;
				}
				else
				{
					if(TempKanal <10)
						TempNazev = "  " + TempKanal + "    Výstup     " + TempNazev; 
					else
						TempNazev = TempKanal + "    Výstup     " + TempNazev;
				}
				int x =this->listBox1->SelectedIndex;
				this->listBox1->Items[this->listBox1->SelectedIndex] = "";
				this->listBox1->Items[this->listBox1->SelectedIndex] += TempNazev;
				this->listBox1->SelectedIndex = x;
				textBox1->BackColor = SystemColors::Window;
			}
//-------------------------- kanal se meni-------------			
			else
			{
					bool ex;
					for(i=0;i<this->listBox1->Items->Count;i++)
					{
						if((Vstupy[i].PoradiKanalu == TempKanal) && (Vstupy[i].Existuje == true))
						{
							ex = true;
						}
					}

							if(ex == false)
					{
						if(this->comboBox1->SelectedIndex == 0)
						{Vstupy[this->listBox1->SelectedIndex].Smer = true;}//nastavi smer na input
						else{Vstupy[this->listBox1->SelectedIndex].Smer = false;}//nastavi smer na output
						Vstupy[this->listBox1->SelectedIndex].PoradiKanalu = TempKanal;
						for(i=0;i<TempNazev->Length::get();i++)	//zapise do struktury vstupu nazev kanalu
							{Vstupy[this->listBox1->SelectedIndex].PopisKanaluSelection[i] = (char)System::Convert::ToSByte(TempNazev[i]);}
						Vstupy[this->listBox1->SelectedIndex].PopisKanaluSelection[i] = '\0';

						if(this->comboBox1->SelectedIndex == 0)
						{
							if(TempKanal <10)
								TempNazev = "  " + TempKanal + "    Vstup       " + TempNazev; 
							else
								TempNazev = TempKanal + "    Vstup       " + TempNazev;
						}
						else
						{
							if(TempKanal <10)
								TempNazev = "  " + TempKanal + "    Výstup     " + TempNazev; 
							else
								TempNazev = TempKanal + "    Výstup     " + TempNazev;
						}
						int x =this->listBox1->SelectedIndex;
						this->listBox1->Items[this->listBox1->SelectedIndex] = "";
						this->listBox1->Items[this->listBox1->SelectedIndex] += TempNazev;
						this->listBox1->SelectedIndex = x;

						textBox1->BackColor = SystemColors::Window;
					}
					else
					{
						textBox1->BackColor = Color::LightCoral;
					}
			}
//-------------------------------------------------------------------------
		 }
		
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) {
			
			 PopisKanalu temp;
			 System::Object ^tempItem;

			 if(this->listBox1->SelectedIndex != 0 )
			 {
				temp = Vstupy[this->listBox1->SelectedIndex-1];
				tempItem = this->listBox1->Items[this->listBox1->SelectedIndex-1];
				Vstupy[this->listBox1->SelectedIndex-1] = Vstupy[this->listBox1->SelectedIndex];
				this->listBox1->Items[this->listBox1->SelectedIndex-1] = this->listBox1->Items[this->listBox1->SelectedIndex];
				Vstupy[this->listBox1->SelectedIndex] = temp;
				this->listBox1->Items[this->listBox1->SelectedIndex] = tempItem;
				int x = this->listBox1->SelectedIndex::get() - 1;
				this->listBox1->ClearSelected();
				this->listBox1->SelectedIndex = x;
			 }
		 }
private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) {
			
			 PopisKanalu temp;
			 System::Object ^tempItem;

			 if((this->listBox1->SelectedIndex != 15) && (Vstupy[this->listBox1->SelectedIndex+1].Existuje == true))
			 {
				temp = Vstupy[this->listBox1->SelectedIndex+1];
				tempItem = this->listBox1->Items[this->listBox1->SelectedIndex+1];
				Vstupy[this->listBox1->SelectedIndex+1] = Vstupy[this->listBox1->SelectedIndex];
				this->listBox1->Items[this->listBox1->SelectedIndex+1] = this->listBox1->Items[this->listBox1->SelectedIndex];
				Vstupy[this->listBox1->SelectedIndex] = temp;
				this->listBox1->Items[this->listBox1->SelectedIndex] = tempItem;
				int x = this->listBox1->SelectedIndex::get() + 1;
				this->listBox1->ClearSelected();
				this->listBox1->SelectedIndex = x;
			 }
		 }
private: System::Void button8_Click(System::Object^  sender, System::EventArgs^  e) {
				
			 for(int i = 0;i<16;i++)
			 {
				 Vstupy[i].Existuje = false;
				 if(this->listBox1->Items->Count !=0)
				 {this->listBox1->Items->RemoveAt(0);}
			 }
		 }
};
}
