#include <windows.h>
#include <iostream>       // std::cout

#include <tchar.h>
#include <strsafe.h>

#pragma once

namespace Testovanie_kniznice {

	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::Threading;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
     USB_IP_LIB::Class1 ^A;
	public:
		Form1(void)
		{
			InitializeComponent();
			A= gcnew USB_IP_LIB::Class1();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  pripoj;
	protected: 
	private: System::Windows::Forms::Button^  odpoj;

	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->pripoj = (gcnew System::Windows::Forms::Button());
			this->odpoj = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// pripoj
			// 
			this->pripoj->Location = System::Drawing::Point(12, 12);
			this->pripoj->Name = L"pripoj";
			this->pripoj->Size = System::Drawing::Size(104, 125);
			this->pripoj->TabIndex = 0;
			this->pripoj->Text = L"Pripoj";
			this->pripoj->UseVisualStyleBackColor = true;
			this->pripoj->Click += gcnew System::EventHandler(this, &Form1::pripoj_Click);
			// 
			// odpoj
			// 
			this->odpoj->Location = System::Drawing::Point(176, 12);
			this->odpoj->Name = L"odpoj";
			this->odpoj->Size = System::Drawing::Size(104, 125);
			this->odpoj->TabIndex = 0;
			this->odpoj->Text = L"Odpoj";
			this->odpoj->UseVisualStyleBackColor = true;
			this->odpoj->Click += gcnew System::EventHandler(this, &Form1::odpoj_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(292, 266);
			this->Controls->Add(this->odpoj);
			this->Controls->Add(this->pripoj);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void pripoj_Click(System::Object^  sender, System::EventArgs^  e) {
				 HANDLE  hThread;
				 DWORD   dwThreadId;
/*
				 USB_IP_LIB::Class1 A;
				 char* IP = "192.168.1.110";
			     char* meno = "1-1.3";
*/
				 Form1^ f=gcnew Form1;
				 Thread^ oThread = gcnew Thread( gcnew ThreadStart( f, &Form1::ThreadMethod ) );
				 oThread->Start();


				/* hThread = CreateThread( 
						NULL,                   // default security attributes
						0,                      // use default stack size  
						&Testovanie_kniznice::Form1::MyThreadFunction,       // thread function name
						&A,						// argument to thread function 
						0,                      // use default creation flags 
						&dwThreadId);   // returns the thread identifier 


				 int ret = A.attach_device(IP, meno);
				 */
			 }

	private: void ThreadMethod(/*Object^ state*/)
{   
	//USB_IP_LIB::Class1 A;
	//A = gcnew USB_IP_LIB::Class1();
	char* IP = "192.168.1.110";
	IP = "192.168.1.101";
	char* meno = "1-1.3";
	char* pass = "5d75193725cfb92ce9aee96b5380db06";

    int ret = A->attach_device(IP, meno,pass,2,0x00000000bdbe5751);
	//3 client
	//2 cez server
	//1 SERVER mode (pripaja sa RPI na počítač)

	MessageBox::Show(ret.ToString());
    //this->Invoke((Action^)delegate(){BtnStart->Text = "Hello";});
}
	private: System::Void odpoj_Click(System::Object^  sender, System::EventArgs^  e) {
				 A->PRIPOJENE = false;
			 }
};
}

