#include "funkce.h"
#include "stdlib.h"
#include "string.h"
#include "stdio.h"
#include "string.h"

unsigned __int8 data[820];
unsigned __int8* p_data = data;
unsigned __int8 radek;


using namespace System;
using namespace System::Runtime::InteropServices;
using namespace System::IO;

//deklarace struktury pro promenne pri praci s tabulkou

void naplneni_tabulky(System::Windows::Forms::DataGridView ^tabulka)
{
	unsigned __int8 n = 0, komp = 0, pin=0, kon=0;
	unsigned __int16 cislo = 0;
	char soucastka[10], konA[1], konB[2];
	tabulka->RowCount = 1;
	while (((*(p_data + 4 * n) != 0) | (*(p_data + 4 * n + 1) != 0)) & (n<204))
	//for (n = 0; n < 204;n++)
	{
		tabulka->Rows[0]->Cells[6]->Value = (*(p_data + (4 * n)));
		tabulka->Rows[0]->Cells[7]->Value = (*(p_data + (4 * n + 1)));
		
		//vypocet polozky cislo
		cislo = *(p_data + 4 * n);	//do cisla nahraje prvni bajt na poslednich osm bitu
		cislo = (cislo << 8) | *(p_data + 4 * n + 1);	//bitovy posun vlevo a pridani bajtu2
		cislo = (cislo & 65472);	//maskovani na prvnich deset bitu promenne
		cislo = (cislo >> 6);		//bitovy posun vpravo pro vypis
		if (cislo<1000) 	tabulka->Rows[0]->Cells[0]->Value = (cislo);	//vypis cisla do prvniho sloupce prvniho radku
		else
		{
			tabulka->Rows[0]->Cells[0]->Value = "----error----";
		}
		cislo = 0;
		//urceni soucastky
		komp = *(p_data + 4 * n + 1);
		komp = (komp & 60);
		switch (komp)
		{ 
		case 0:
			tabulka->Rows[0]->Cells[1]->Value = "1 propojení";
			break;
		case 4:
			tabulka->Rows[0]->Cells[1]->Value = "2 rezistor 220";
			break;
		case 8:
			tabulka->Rows[0]->Cells[1]->Value = "3 rez.+dioda";
			break;
		case 12:
				tabulka->Rows[0]->Cells[1]->Value = "4 relé";
				break;
		case 16:
			tabulka->Rows[0]->Cells[1]->Value = "5 spínač";
			break;
		default:
			tabulka->Rows[0]->Cells[1]->Value = "----error----";
			break;
		}
		komp = 0;
		
		//konektorA
		kon = *(p_data + 4 * n + 1);
		kon = (kon & 2);
		if ((*(p_data + 4 * n + 2)<205) & (*(p_data + 4 * n + 2)>102)) kon = kon + 4;
		switch (kon)
		{
		case 0:
			tabulka->Rows[0]->Cells[2]->Value = "A";
			break;
		case 4:
			tabulka->Rows[0]->Cells[2]->Value = "B";
			break;
		case 2:
			tabulka->Rows[0]->Cells[2]->Value = "C";
			break;
		case 6:
			tabulka->Rows[0]->Cells[2]->Value = "D";
			break;
		default:
			tabulka->Rows[0]->Cells[2]->Value = "---error---";
			break;
		}
		kon = 0;

		//konektorB
		kon = *(p_data + 4 * n + 1);
		kon = (kon & 1);
		if ((*(p_data + 4 * n + 3)<205) & (*(p_data + 4 * n + 3)>102)) kon = kon + 4;
		switch (kon)
		{
		case 0:
			tabulka->Rows[0]->Cells[4]->Value = "A";
			break;
		case 4:
			tabulka->Rows[0]->Cells[4]->Value = "B";
			break;
		case 1:
			tabulka->Rows[0]->Cells[4]->Value = "C";
			break;
		case 5:
			tabulka->Rows[0]->Cells[4]->Value = "D";
			break;
		default:
			tabulka->Rows[0]->Cells[3]->Value = "---error---";
			break;
		}
		//kon = 0;
		
		//kontrola pinA
		if (*(p_data + 4 * n + 2) > 102) pin = *(p_data + 4 * n + 2) - 102;
		else pin = *(p_data + 4 * n + 2);
		tabulka->Rows[0]->Cells[3]->Value = pin;
		if ((*(p_data + 4 * n + 2) >204) | (*(p_data + 4 * n + 2) == 0)) tabulka->Rows[0]->Cells[3]->Value = "----error----";
		pin = 0;

		//kontrola pinB
		if (*(p_data + 4 * n + 3) > 102) pin = *(p_data + 4 * n + 3) - 102;
		else pin = *(p_data + 4 * n + 3);
		tabulka->Rows[0]->Cells[5]->Value = pin;
		if ((*(p_data + 4 * n + 3) >204) | (*(p_data + 4 * n + 3) == 0)) tabulka->Rows[0]->Cells[5]->Value = "----error----";
		tabulka->Rows->Insert(0);	//vlozeni radku na pozici 0
		pin = 0;
		radek++;
		n++;
	}
}


__int8 pridejpolozku(System::Windows::Forms::NumericUpDown ^cislo, System::Windows::Forms::ComboBox ^soucastka, 
					System::Windows::Forms::ComboBox ^konA, System::Windows::Forms::NumericUpDown ^pinA,
					System::Windows::Forms::ComboBox ^konB, System::Windows::Forms::NumericUpDown ^pinB,
					System::Windows::Forms::DataGridView ^tabulka)
{
	unsigned __int16 t_cislo = 0;
	unsigned __int8 t_soucastka = 0, t_pinA = 0, t_pinB = 0, vysledek=0;
	char *test, *t_konA, *t_konB;
	//cislo,soucastka,kon,pin,kon,pin, tabulka
	t_cislo = Convert::ToUInt16(cislo->Text);
	t_pinA = Convert::ToInt16(pinA->Text);
	t_pinB = Convert::ToInt16(pinB->Text);
	t_cislo = t_cislo << 6;
	test = (char*)Marshal::StringToHGlobalAnsi(Convert::ToString(soucastka->Text)).ToPointer();
	t_konA = (char*)Marshal::StringToHGlobalAnsi(Convert::ToString(konA->Text)).ToPointer();
	t_konB = (char*)Marshal::StringToHGlobalAnsi(Convert::ToString(konB->Text)).ToPointer();
	
	//urceni soucastky
	switch (*test)
	{
	case 49:
		t_soucastka = 0;
		break;
	
	case 50:
		t_soucastka = 4;
		break;

	case 51:
		t_soucastka = 8;
		break;

	case 52:
		t_soucastka = 12;
		break;

	case 53:
		t_soucastka = 16;
		break;

	default:
		vysledek = 1;
		break;
	}
	t_cislo = (t_cislo | t_soucastka);
	//urceni konektoru A a kodu pinu A

	if ((*t_konA == 66) | (*t_konA == 68)) t_pinA = t_pinA + 102;
	if ((*t_konA == 67) | (*t_konA == 68)) t_cislo = (t_cislo + 2);
	if (*t_konA == 0) vysledek = vysledek + 4;
	//urceni konektoru B a kodu pinu B
	if ((*t_konB == 66) | (*t_konB == 68)) t_pinB = t_pinB + 102;
	if ((*t_konB == 67) | (*t_konB == 68)) t_cislo = (t_cislo + 1);
	if (*t_konB == 0) vysledek = vysledek + 2;

	if (vysledek == 0)
	{
		tabulka->Rows[0]->Cells[0]->Value = (cislo->Text);
		tabulka->Rows[0]->Cells[1]->Value = (soucastka->Text);
		tabulka->Rows[0]->Cells[2]->Value = (konA->Text);
		tabulka->Rows[0]->Cells[3]->Value = (t_pinA);
		tabulka->Rows[0]->Cells[4]->Value = (konB->Text);
		tabulka->Rows[0]->Cells[5]->Value = (t_pinB);
		tabulka->Rows[0]->Cells[6]->Value = *(p_data + 4 * radek);
		tabulka->Rows[0]->Cells[7]->Value = *(p_data + 4 * radek + 1);
		tabulka->Rows->Insert(0);
		*(p_data + 4 * radek + 3) = t_pinB;
		*(p_data + 4 * radek + 2) = t_pinA;
		*(p_data + 4 * radek + 1) = t_cislo;
		t_cislo = t_cislo >> 8;
		*(p_data + 4 * radek) = t_cislo;
		radek++;
		return 0;
	}
	else
	{
		if (vysledek > 3)
		{
			tabulka->Rows[radek]->Cells[1]->Value = "---chyba---";
				vysledek = vysledek - 4;
		}
		if (vysledek > 1)
		{
			tabulka->Rows[radek]->Cells[2]->Value = "---chyba---";
			vysledek = vysledek - 2;
		}
		if (vysledek > 0)
		{
			tabulka->Rows[radek]->Cells[4]->Value = "---chyba---";
			vysledek = vysledek - 1;
		}
		vysledek = 0;
	}
}

void reset_tabulky(System::Windows::Forms::DataGridView ^tabulka)
{
	__int16 n = 0;
	for (n = 0; n<820; n++)
	{
		{
			*(p_data+n)= 0;
			
		}
	}
	tabulka->RowCount = 0;
	radek = 0;
}

void zapis_stream(System::String ^cesta)
{
	__int16 i = 0;
	unsigned __int8 znak = 0;
	UInt16 pismeno = 0;
	StreamWriter^ kronikar = gcnew StreamWriter(cesta);
	for (i = 0; i < 816; i++)
	{
		kronikar->Write(*(p_data + i));
		znak = *(p_data + i);

	}
		kronikar->Close();
}

void cteni_stream(System::String ^cesta, System::Windows::Forms::TextBox ^text)
{
	unsigned __int16 i = 0, kontrola=0;
	StreamReader^ ctenar = gcnew StreamReader(cesta);
	while (i<816)
	{
		*(p_data + i) = ctenar->Read();
		kontrola = *(p_data + i);
		i++;
	}
	ctenar->Close();
}