using System;
using System.Runtime.InteropServices;
using System.Text;
using Gtk;

public partial class MainWindow: Gtk.Window
{
//DLLImport nao tem EntryPoint, usa o proprio nome da rotina
//DllImport nao precisa	CallingConvention=CallingConvention.Cdecl, porque Cdecl é default nop Linux
//DLLImport nao precisa CharSet=CharSet.Ansi, porque Ansi é o default

	[DllImport ("sk_access")] static extern void Back_Space();
	[DllImport ("sk_access")] static extern int  Buffer_In_Data();
	[DllImport ("sk_access")] static extern void Carriage_Return();
	[DllImport ("sk_access")] static extern void Clear_Disp();
	[DllImport ("sk_access")] static extern void Clear_L1();
	[DllImport ("sk_access")] static extern void Clear_L2();
	[DllImport ("sk_access")] static extern void Cursor_Blink();
	[DllImport ("sk_access")] static extern void Cursor_Off();
	[DllImport ("sk_access")] static extern void Cursor_On();
	[DllImport ("sk_access")] static extern void Disable_Keyboard();
	[DllImport ("sk_access")] static extern void Disp(String PChar);
	[DllImport ("sk_access")] static extern void Enable_Keyboard();
	[DllImport ("sk_access")] static extern void Gotoxy(byte x, byte y);
	[DllImport ("sk_access")] static extern void Disp_Char(char data);
	[DllImport ("sk_access")] static extern void Unlock_Interface();
	[DllImport ("sk_access")] static extern int  Get_Current_Interface();
	[DllImport ("sk_access")] static extern byte Get_Cursor(ref byte x_pos, ref byte y_pos);
	[DllImport ("sk_access")] static extern int	 Get_Data(byte[] buffer, int buf_size);
	[DllImport ("sk_access")] static extern void Get_Dll_Version(StringBuilder ver);
	[DllImport ("sk_access")] static extern void Get_Firmware_Version(StringBuilder ver);
	[DllImport ("sk_access")] static extern void Get_Serial_Number(StringBuilder ser);
	[DllImport ("sk_access")] static extern byte Get_Status();
	[DllImport ("sk_access")] static extern void Keyb_Reset();
	[DllImport ("sk_access")] static extern void Line_Feed();
	[DllImport ("sk_access")] static extern void Redirect();
	[DllImport ("sk_access")] static extern void Reset_Interface();
	[DllImport ("sk_access")] static extern void Send_Data(byte data);
	[DllImport ("sk_access")] static extern void Send_Disp_Ctrl(byte data);
	[DllImport ("sk_access")] static extern void Set_Eco_Off();
	[DllImport ("sk_access")] static extern void Set_Eco_On();
	[DllImport ("sk_access")] static extern int  Set_Interface(String Pchar);
	[DllImport ("sk_access")] static extern void Set_Page0();
	[DllImport ("sk_access")] static extern void Set_Page1();
	[DllImport ("sk_access")] static extern void Set_Psw_Off();
	[DllImport ("sk_access")] static extern void Set_Psw_On();
	[DllImport ("sk_access")] static extern void Free_Sk_Access();
	[DllImport ("sk_access")] static extern void Set_Udg(byte caracter,byte def0,byte def1,byte def2,byte def3,
                                                                       byte def4,byte def5,byte def6,byte def7);
	[DllImport ("sk_access")] static extern void Log_Off();
	[DllImport ("sk_access")] static extern void Log_On(int level);
	[DllImport ("sk_access")] static extern void S_Mode_Off();
	[DllImport ("sk_access")] static extern void S_Mode_On();

//--------------------------
	public MainWindow (): base (Gtk.WindowType.Toplevel)
	{
		Build ();
		S_Mode_Off();
		Clear_Disp();
		Disp("Inicio de teste");
		//B_BackSpace.Clicked += new EventHandler(BackSpaceClick);
	}
	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Free_Sk_Access();
		Application.Quit ();
		a.RetVal = true;
	}
//--------------------------
	protected void BackSpaceCLick (object sender, EventArgs e)
	{
		Back_Space();
	}

	protected void B_inDataClick (object sender, EventArgs e)
	{
		Entry_Disp.Text=Convert.ToString(Buffer_In_Data());
	}
	
	protected void CarrRetClick (object sender, EventArgs e)
	{
		Carriage_Return();
	}

	protected void ClearDispClick (object sender, EventArgs e)
	{   
		Clear_Disp();
	}
	protected void Clear_L1Click (object sender, EventArgs e)
	{
		Clear_L1();
	}
	
	protected void Clear_L2Click (object sender, EventArgs e)
	{
		Clear_L2();
	}

	protected void Cursor_BlinkClick (object sender, EventArgs e)
	{
		Cursor_Blink();
	}
	
	protected void Cursor_OffClick (object sender, EventArgs e)
	{
		Cursor_Off();
	}
	
	protected void Cursor_Onclick (object sender, EventArgs e)
	{
		Cursor_On();
	}

	protected void DisableClick (object sender, EventArgs e)
	{
		Disable_Keyboard();
	}

	protected void DispClick (object sender, EventArgs e)
	{
//		var Msg = new MessageDialog(this,DialogFlags.Modal,MessageType.Info,ButtonsType.Ok,"Hello World !");
//		if ((ResponseType)Msg.Run()==ResponseType.Ok)
//			Msg.Destroy();
		Disp (Entry_Disp.Text.ToString());
	}

	protected void EnableClick (object sender, EventArgs e)
	{
		Enable_Keyboard();
	}

	protected void Goto_xyClick (object sender, EventArgs e)
	{
		Gotoxy(Convert.ToByte(Entry_X.Text), Convert.ToByte(Entry_Y.Text));
	}

	protected void Disp_CharClick (object sender, EventArgs e)
	{
		Disp_Char(Convert.ToChar(Entry_Char.Text));
	}

	protected void UnlockClick (object sender, EventArgs e)
	{
		Unlock_Interface();
	}

	protected void GetIntClick (object sender, EventArgs e)
	{
		Entry_Stat.Text=Convert.ToString(Get_Current_Interface());
	}

	protected void Get_CursorClick (object sender, EventArgs e)
	{
		byte pos=0,x=0,y=0;
		pos=Get_Cursor(ref x,ref y);
		Entry_X.Text=Convert.ToString(x);
		Entry_Y.Text=Convert.ToString(y);
	    Entry_Stat.Text=Convert.ToString(pos); 
	}
	//Rotina nao implementada
	protected void Get_DataClick (object sender, EventArgs e)
	{
		Byte[] buffer;
		buffer = new Byte[10];
		int c = 0, d = 0;

		c = Get_Data (buffer, 3);

		if (c > 0)                 //Se ha dados a serem lidos
			for (d=0; d<c; d++) { 
			Entry_Disp.Text += buffer[d].ToString("X2");
			}
	}

	protected void Dll_VerClick (object sender, EventArgs e)
	{
		StringBuilder _Version = new StringBuilder (256);
		Get_Dll_Version(_Version);
		Entry_Disp.Text=Convert.ToString(_Version);
	}

	protected void Get_FirmClick (object sender, EventArgs e)
	{
		StringBuilder _Version = new StringBuilder (256);
		Get_Firmware_Version(_Version);
		Entry_Disp.Text=Convert.ToString(_Version);
	}

	protected void Get_SNClick (object sender, EventArgs e)
	{
		StringBuilder _Serial = new StringBuilder (256);
		Get_Serial_Number(_Serial);
		Entry_Disp.Text=Convert.ToString(_Serial);
	}

	protected void Get_StatusClick (object sender, EventArgs e)
	{
		switch (Get_Status()) {
			case 0x00: Entry_Stat.Text="OK";
			break;
			case 0x01: Entry_Stat.Text="FALHA";
			break;
		    case 0xff: Entry_Stat.Text="TIME-OUT";
			break;
		}
	}

	protected void ResetClick (object sender, EventArgs e)
	{
		Keyb_Reset();
	}

	protected void Line_FeedClick (object sender, EventArgs e)
	{
		Line_Feed();
	}

	protected void RedirectClick (object sender, EventArgs e)
	{
		Redirect();
	}

	protected void ResetIntCLick (object sender, EventArgs e)
	{
		Reset_Interface();
	}

	protected void Send_DataClick (object sender, EventArgs e)
	{
		Send_Data(0xA5);
		Send_Data(0x41);
	}

	protected void Disp_CtrlClick (object sender, EventArgs e)
	{
		Send_Disp_Ctrl(0x01); //Clear
	}

	protected void Eco_OffClick (object sender, EventArgs e)
	{
		Set_Eco_Off();
	}

	protected void Eco_OnCLick (object sender, EventArgs e)
	{
		Set_Eco_On();
	}

	protected void SetIntClick (object sender, EventArgs e)
	{
		var result=Set_Interface(Entry_Stat.Text.ToString());
		if(result==-1) L_SI.LabelProp="Nack";
		if(result>=0) L_SI.LabelProp="OK";

	}

	protected void Set_Page0Click (object sender, EventArgs e)
	{
		Set_Page0();
	}

	protected void Set_Page1Click (object sender, EventArgs e)
	{
		Set_Page1();
	}

	protected void Psw_OffClick (object sender, EventArgs e)
	{
		Set_Psw_Off();
	}

	protected void Psw_OnClick (object sender, EventArgs e)
	{
		Set_Psw_On();
	}

	protected void UDGClick (object sender, EventArgs e)
	{
		byte c=0;
		char ch= '\0';

		Cursor_Off();;
		Disp_Char(ch);                                         //Mostra o caracter #0

		for(c=0;c<2;c++){
			Set_Udg(0,0x3F,0x11,0x11,0x3F,0x11,0x11,0x11,0x00);//Redesenha o caracter #0
			System.Threading.Thread.Sleep(100);
			Set_Udg(0,0x11,0x11,0x3F,0x11,0x11,0x11,0x00,0x3F);
			System.Threading.Thread.Sleep(100);
			Set_Udg(0,0x11,0x3F,0x11,0x11,0x11,0x00,0x3F,0x11);
			System.Threading.Thread.Sleep(100);
			Set_Udg(0,0x3F,0x11,0x11,0x11,0x00,0x3F,0x11,0x11);
			System.Threading.Thread.Sleep(100);
			Set_Udg(0,0x11,0x11,0x11,0x00,0x3F,0x11,0x11,0x3F);
			System.Threading.Thread.Sleep(100);
			Set_Udg(0,0x11,0x11,0x00,0x3F,0x11,0x11,0x3F,0x11);
			System.Threading.Thread.Sleep(100);
			Set_Udg(0,0x11,0x00,0x3F,0x11,0x11,0x3F,0x11,0x11);
			System.Threading.Thread.Sleep(100);
			Set_Udg(0,0x00,0x3F,0x11,0x11,0x3F,0x11,0x11,0x11);
			System.Threading.Thread.Sleep(100);
		}
		Cursor_Blink();
	}
}
