#pragma once
#include "Form3.h"
#include "Kalibrace.h"
namespace Bakalarska_prace {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Xml;
	/// <summary>
	/// Summary for Form2
	/// </summary>
	public ref class Form2 : public System::Windows::Forms::Form
	{
	public:
		Form2(Teleso* Neco)
		{
			InitializeComponent();
			MyTeleso = Neco;
			this->InitPictureBox1();
			InitListView1();
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form2()
		{
			if (components)
			{
				delete components;
			}
			Spoj = false;
			Rozpoj = false;
		}
	private: System::Windows::Forms::PictureBox^  pictureBox1;




	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::Button^  button2;

	protected: 
	private: bool Spoj,Rozpoj;
	private: int Cis;
	private: Teleso* MyTeleso;
	private: System::Windows::Forms::ListView^  listView1;
	private: System::Windows::Forms::ColumnHeader^  Cislo;
	private: System::Windows::Forms::ColumnHeader^  X;
	private: System::Windows::Forms::ColumnHeader^  Y;
	private: System::Windows::Forms::ColumnHeader^  Z;
	private: System::Windows::Forms::Button^  button3;
	private: System::Windows::Forms::Button^  button4;
	private: System::Windows::Forms::Button^  button5;
	private: System::Windows::Forms::Button^  button6;
	private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;
	private: System::Windows::Forms::OpenFileDialog^  openFileDialog1;
	private: 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->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->listView1 = (gcnew System::Windows::Forms::ListView());
			this->Cislo = (gcnew System::Windows::Forms::ColumnHeader());
			this->X = (gcnew System::Windows::Forms::ColumnHeader());
			this->Y = (gcnew System::Windows::Forms::ColumnHeader());
			this->Z = (gcnew System::Windows::Forms::ColumnHeader());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->button4 = (gcnew System::Windows::Forms::Button());
			this->button5 = (gcnew System::Windows::Forms::Button());
			this->button6 = (gcnew System::Windows::Forms::Button());
			this->saveFileDialog1 = (gcnew System::Windows::Forms::SaveFileDialog());
			this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
			this->SuspendLayout();
			// 
			// pictureBox1
			// 
			this->pictureBox1->Location = System::Drawing::Point(12, 12);
			this->pictureBox1->Name = L"pictureBox1";
			this->pictureBox1->Size = System::Drawing::Size(400, 300);
			this->pictureBox1->TabIndex = 0;
			this->pictureBox1->TabStop = false;
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(418, 258);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 2;
			this->button1->Text = L"Přidej";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form2::button1_Click);
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(499, 258);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(75, 23);
			this->button2->TabIndex = 3;
			this->button2->Text = L"Odeber";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &Form2::button2_Click);
			// 
			// listView1
			// 
			this->listView1->Columns->AddRange(gcnew cli::array< System::Windows::Forms::ColumnHeader^  >(4) {this->Cislo, this->X, this->Y, 
				this->Z});
			this->listView1->FullRowSelect = true;
			this->listView1->GridLines = true;
			this->listView1->HeaderStyle = System::Windows::Forms::ColumnHeaderStyle::Nonclickable;
			this->listView1->Location = System::Drawing::Point(418, 12);
			this->listView1->Name = L"listView1";
			this->listView1->Size = System::Drawing::Size(237, 240);
			this->listView1->TabIndex = 4;
			this->listView1->UseCompatibleStateImageBehavior = false;
			this->listView1->View = System::Windows::Forms::View::Details;
			this->listView1->ItemSelectionChanged += gcnew System::Windows::Forms::ListViewItemSelectionChangedEventHandler(this, &Form2::listView1_ItemSelectionChanged);
			this->listView1->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form2::listView1_KeyDown);
			// 
			// Cislo
			// 
			this->Cislo->Text = L"Číslo";
			this->Cislo->Width = 37;
			// 
			// X
			// 
			this->X->Text = L"X";
			this->X->Width = 61;
			// 
			// Y
			// 
			this->Y->Text = L"Y";
			this->Y->Width = 58;
			// 
			// Z
			// 
			this->Z->Text = L"Z";
			this->Z->Width = 76;
			// 
			// button3
			// 
			this->button3->Location = System::Drawing::Point(418, 287);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(75, 23);
			this->button3->TabIndex = 5;
			this->button3->Text = L"Spoj";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &Form2::button3_Click);
			// 
			// button4
			// 
			this->button4->Location = System::Drawing::Point(499, 287);
			this->button4->Name = L"button4";
			this->button4->Size = System::Drawing::Size(75, 23);
			this->button4->TabIndex = 6;
			this->button4->Text = L"Rozpoj";
			this->button4->UseVisualStyleBackColor = true;
			this->button4->Click += gcnew System::EventHandler(this, &Form2::button4_Click);
			// 
			// button5
			// 
			this->button5->Location = System::Drawing::Point(580, 258);
			this->button5->Name = L"button5";
			this->button5->Size = System::Drawing::Size(75, 23);
			this->button5->TabIndex = 7;
			this->button5->Text = L"Save";
			this->button5->UseVisualStyleBackColor = true;
			this->button5->Click += gcnew System::EventHandler(this, &Form2::button5_Click);
			// 
			// button6
			// 
			this->button6->Location = System::Drawing::Point(580, 287);
			this->button6->Name = L"button6";
			this->button6->Size = System::Drawing::Size(75, 23);
			this->button6->TabIndex = 8;
			this->button6->Text = L"Load";
			this->button6->UseVisualStyleBackColor = true;
			this->button6->Click += gcnew System::EventHandler(this, &Form2::button6_Click);
			// 
			// saveFileDialog1
			// 
			this->saveFileDialog1->Filter = L"\"XML Files (.xml)|*.xml|All Files (*.*)|*.*\"";
			// 
			// openFileDialog1
			// 
			this->openFileDialog1->FileName = L"openFileDialog1";
			this->openFileDialog1->Filter = L"\"XML Files (.xml)|*.xml|All Files (*.*)|*.*\"";
			// 
			// Form2
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(659, 316);
			this->Controls->Add(this->button6);
			this->Controls->Add(this->button5);
			this->Controls->Add(this->button4);
			this->Controls->Add(this->button3);
			this->Controls->Add(this->listView1);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->pictureBox1);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
			this->Name = L"Form2";
			this->Text = L"Těleso";
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
			this->ResumeLayout(false);

		}
#pragma endregion

	private: void InitPictureBox1(){
					//if (pictureBox1->Image!=nullptr) pictureBox1->Image->Dispose();
					Bitmap^ BitIm = gcnew Bitmap(400,300); 
					Graphics^ g = Graphics::FromImage(BitIm);
					g->Clear(Color::White);
					if (MyTeleso->GetCount()>0)
					{
						Bod MinZ = MyTeleso->GetBod(0);
						Bod Zkusebni;
						for (int i=1; i<MyTeleso->GetCount();i++)
						{
							Zkusebni = MyTeleso->GetBod(i);
							if (MinZ.z>Zkusebni.z)
								MinZ = Zkusebni;
						}
						MinZ.z = MinZ.z-8;
						double f = double::MaxValue;
						for (int i = 0; i < MyTeleso->GetCount(); i++)
						{
							Zkusebni = MyTeleso->GetBod(i);
							if ((f * Zkusebni.x / (Zkusebni.z - MinZ.z) + BitIm->Width / 2) < 0.2 * BitIm->Width)
							{
								f = -0.3 * BitIm->Width * (Zkusebni.z - MinZ.z) / Zkusebni.x;
							}
							if ((f * Zkusebni.x / (Zkusebni.z - MinZ.z) + BitIm->Width / 2) > 0.8 * BitIm->Width)
							{
								f = 0.3 * BitIm->Width * (Zkusebni.z - MinZ.z) / Zkusebni.x;
							}
							if ((-f * Zkusebni.y / (Zkusebni.z - MinZ.z) + BitIm->Height / 2) > 0.8 * BitIm->Height)
							{
								f = -0.3 * BitIm->Height * (Zkusebni.z - MinZ.z) / Zkusebni.y;
							}
							if ((-f * Zkusebni.y / (Zkusebni.z - MinZ.z) + BitIm->Height / 2) < 0.2 * BitIm->Height)
							{
								f = 0.3 * BitIm->Height * (Zkusebni.z - MinZ.z) / Zkusebni.y;
							}
						}
						int* x = new int[MyTeleso->GetCount()];
						int* y = new int[MyTeleso->GetCount()];
						Pen^ MyPen = gcnew Pen(Color::Black,1);
						for (int i = 0; i < MyTeleso->GetCount(); i++)
						{
							Zkusebni = MyTeleso->GetBod(i);
							x[i] = (f * Zkusebni.x / (Zkusebni.z - MinZ.z)) + BitIm->Width/2;
							y[i] = (-f * Zkusebni.y / (Zkusebni.z - MinZ.z)) + BitIm->Height/2;
							for (int k=0; k <i; k++)
								if (Zkusebni.spoj[k]) g->DrawLine(MyPen,x[k],y[k],x[i],y[i]);
						}
						MyPen->Color = Color::Red;
						for (int i = 0; i < MyTeleso->GetCount(); i++)
						{
							g->DrawLine(MyPen,x[i]-1,y[i],x[i]+1,y[i]);
							g->DrawLine(MyPen,x[i],y[i]-1,x[i],y[i]+1);
						}
						delete x;
						delete y;
					}
//					g->Dispose();
					pictureBox1->Image = BitIm;
					

				 }
	public: Teleso GetTeleso(){
				   return *MyTeleso;
			   }
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 Form3^ okno = gcnew Form3();
				 if (okno->ShowDialog()==System::Windows::Forms::DialogResult::OK)
					 MyTeleso->Add(okno->GetX(),okno->GetY(),okno->GetZ());
				 InitPictureBox1();
				 InitListView1();
			 }
	private: void InitListView1(){
				listView1->Items->Clear();
				Bod Novy;
				for (int i = 0;i<MyTeleso->GetCount();i++)
				{
					ListViewItem^ item = listView1->Items->Add(gcnew System::String((i+1).ToString()));
					Novy = MyTeleso->GetBod(i);
					item->SubItems->Add(Novy.x.ToString());
					item->SubItems->Add(Novy.y.ToString());
					item->SubItems->Add(Novy.z.ToString());

					
				}
			 }
	private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
			 if (listView1->SelectedItems->Count==0) return;
			 MyTeleso->Remove(Convert::ToInt32(listView1->SelectedItems[0]->Text)-1);
			 InitPictureBox1();
			 InitListView1();
			 Spoj = false;
			 Rozpoj = false;
		 }
	private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
			 if (listView1->SelectedItems->Count == 0) 
			 {
				 MessageBox::Show("Zvolte bod!!");
				 return;
			 }
			 Cis = Convert::ToInt32(listView1->SelectedItems[0]->Text)-1;
			 Spoj = true;
			 Rozpoj = false;
		 }
	private: System::Void listView1_ItemSelectionChanged(System::Object^  sender, System::Windows::Forms::ListViewItemSelectionChangedEventArgs^  e) {
			 listView1->Focus();
			 if ((Spoj==false&&Rozpoj==false)||listView1->SelectedItems->Count==0) return;
			 if (Spoj)
			 {
			 MyTeleso->Connect(Cis,Convert::ToInt32(listView1->SelectedItems[0]->Text)-1);
			 this->Spoj = false;
			 }
			 if (Rozpoj)
			 {
			 MyTeleso->Disconnect(Cis,Convert::ToInt32(listView1->SelectedItems[0]->Text)-1);
			 this->Rozpoj = false;
			 }			
			 this->InitPictureBox1();
		 }
	private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) {
			 if (listView1->SelectedItems->Count == 0) 
			 {
				 MessageBox::Show("Zvolte bod!!");
				 return;
			 }
			 Cis = Convert::ToInt32(listView1->SelectedItems[0]->Text)-1;
			 Spoj = false;
			 Rozpoj = true;
		 }
	private: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) {
			 
			 if (saveFileDialog1->ShowDialog()==System::Windows::Forms::DialogResult::OK)
			 MyTeleso->Save(saveFileDialog1->FileName);
		 }
	private: System::Void button6_Click(System::Object^  sender, System::EventArgs^  e) {
			 
			 if (openFileDialog1->ShowDialog()==System::Windows::Forms::DialogResult::OK&&openFileDialog1->CheckFileExists)
			 {
			 MyTeleso->Load(openFileDialog1->FileName);
			 MyTeleso->InitZobr();
			 this->InitPictureBox1();
			 this->InitListView1();
			 }
		 }
	private: System::Void listView1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
				 
				 if (e->KeyCode == Keys::Delete)
				 {
					if (listView1->SelectedItems->Count==0) return;
					  MyTeleso->Remove(Convert::ToInt32(listView1->SelectedItems[0]->Text)-1);
					  Spoj = false;
					  Rozpoj = false;
					  this->InitPictureBox1();
					  this->InitListView1();
				 }
			 }
};
}
