#include "CInsim.h"	//é preciso incluir o helder CInsim na biblioteca e nas opcões de directoria não esquecer de adicionar

#include <iostream>	 //as libraries e os includes.
#include <time.h> //para dizer a altura dos cliques e eventos
#include <string> // converter em string algumas variaveis
#include <stdio.h>
#include <fstream>// para ler os ficheiros

CInsim insim;			//CInsim object, handles the communication with LFS


int numero_alea(int de, int ate)
{
	//faz o numero aleatorio para o semafero verde
    int random;
    ate -= de;
    random = rand() % (ate + 1) + de;
    return random;
}

int semafero()
{
//SEMAFERO - a desenhar...

	int posx_sema=690; //posição x do semafero
	int posy_sema=55; //posição y do semafero
	bool partida=false;

darpartida:

  struct IS_BTN Btn2; //base do semafero
	  memset(&Btn2, 0, sizeof(Btn2));
	  Btn2.Size = sizeof(struct IS_BTN);
	  Btn2.Type = ISP_BTN;		
	  Btn2.ReqI = 1;
	  Btn2.UCID = 255;
	  Btn2.ClickID = 2;		
	  Btn2.BStyle = ISB_DARK;	// 	ISB_DARK botao escuro
	  Btn2.L = posx_sema;		
	  Btn2.T = posy_sema;	
	  Btn2.H = 65;	
	  Btn2.W = 10;
	  sprintf(Btn2.Text, "%s", ""); //para saber que é do semafero
	  insim.send_packet(&Btn2);	//Envia o pacote do botão 2


    struct IS_BTN Btn3; //1a luz
		  memset(&Btn3, 0, sizeof(Btn3));
		  Btn3.Size = sizeof(struct IS_BTN);
		  Btn3.Type = ISP_BTN;	
		  Btn3.ReqI = 1;
		  Btn3.UCID = 255;
		  Btn3.ClickID = 3;
		  Btn3.BStyle = 0;		
		  Btn3.L = posx_sema - 14;		
		  Btn3.T = posy_sema - 10;			
		  Btn3.H = 38;			
		  Btn3.W = 38;	
		  if (partida==false)
		  {
		  sprintf(Btn3.Text, "%s", "^2•");
			insim.send_packet(&Btn3);
			}
		  else if (partida==true) 
		  {
			  Sleep(7000);
		  sprintf(Btn3.Text, "%s", "^3•");
			insim.send_packet(&Btn3);
		  Sleep(1000);
		  }


    struct IS_BTN Btn4; //2a luz
		  memset(&Btn4, 0, sizeof(Btn4));
		  Btn4.Size = sizeof(struct IS_BTN);	
		  Btn4.Type = ISP_BTN;		
		  Btn4.ReqI = 1;
		  Btn4.UCID = 255;
		  Btn4.ClickID = 4;		
		  Btn4.BStyle = 0;			//parte trás do botão é invisivel
		  Btn4.L = posx_sema - 14;		
		  Btn4.T = posy_sema + 4;	
		  Btn4.H = 38;		
		  Btn4.W = 38;
			if (partida==false)
		  {
		  sprintf(Btn4.Text, "%s", "^2•");	
			insim.send_packet(&Btn4);
			}
		  else if (partida==true)  
		  {
		  sprintf(Btn4.Text, "%s", "^3•");
			insim.send_packet(&Btn4);
		  Sleep(1000);
		  }
  

    struct IS_BTN Btn5; //3a luz
		  memset(&Btn5, 0, sizeof(Btn5));	
		  Btn5.Size = sizeof(struct IS_BTN);
		  Btn5.Type = ISP_BTN;			
		  Btn5.ReqI = 1;
		  Btn5.UCID = 255;
		  Btn5.ClickID = 5;		
		  Btn5.BStyle = 0;			
		  Btn5.L = posx_sema - 14;		
		  Btn5.T = posy_sema + 18;			
		  Btn5.H = 38;			
		  Btn5.W = 38;			
			if (partida==false)
		  {
		  sprintf(Btn5.Text, "%s", "^2•");
			insim.send_packet(&Btn5);
			}
		  else if (partida==true) 
		  {
		  sprintf(Btn5.Text, "%s", "^3•");	
		  insim.send_packet(&Btn5);

		      //gera o tempo aleatorio para o sinal verde
		       int alea;
			   alea=numero_alea(1,1500);

			Sleep(3000+alea);
		  }


    struct IS_BTN Btn6; //4a luz - partida
		  memset(&Btn6, 0, sizeof(Btn6));
		  Btn6.Size = sizeof(struct IS_BTN);
		  Btn6.Type = ISP_BTN;		
		  Btn6.ReqI = 1;
		  Btn6.UCID = 255;
		  Btn6.ClickID = 6;		
		  Btn6.BStyle = 0;			
		  Btn6.L = posx_sema - 14;				
		  Btn6.T = posy_sema + 35;				
		  Btn6.H = 38;			
		  Btn6.W = 38;			
			if (partida==false)
		  {
		  sprintf(Btn6.Text, "%s", "^0•");	
		  insim.send_packet(&Btn6);
		  partida=true;
		  goto darpartida;
			}
		  else if (partida==true)   
		  {
		  sprintf(Btn6.Text, "%s", "^2•");
		  insim.send_packet(&Btn6);
		  Sleep(5000);

//SEMAFERO - a apagar...

	struct IS_BFN Bfn_base; //apaga o botao
		memset(&Bfn_base, 0,sizeof(Bfn_base));
		Bfn_base.Size = sizeof(struct IS_BFN);
		Bfn_base.Type = ISP_BFN;
		Bfn_base.SubT = BFN_DEL_BTN;
		Bfn_base.ReqI = 1;
		Bfn_base.UCID = 255;
		Bfn_base.ClickID = 2;	
		insim.send_packet(&Bfn_base);

		Bfn_base.ClickID = 3;	
		insim.send_packet(&Bfn_base);

		Bfn_base.ClickID = 4;	
		insim.send_packet(&Bfn_base);

		Bfn_base.ClickID = 5;	
		insim.send_packet(&Bfn_base);

		Bfn_base.ClickID = 6;	
		insim.send_packet(&Bfn_base);
		partida=false;
		  }

	return 0;
}

int main()
{
  char buffer[256], char buffer1[256], char buffer2[256], char buffer3[256], char buffer4[256], char buffer5[256], char buffer6[256], char buffer7[256];
  char buffer8[256], char buffer9[256];
  char buffer10[256], char buffer11[256], char buffer12[256], char buffer13[256];



  std::ifstream myfile ("insim.cfg");
  while (!myfile.eof())
  {
			myfile.getline (buffer,100);
			myfile.getline (buffer1,100);  //ip
			myfile.getline (buffer,100);
			myfile.getline (buffer,100);
			myfile.getline (buffer2,100);  //porta
			myfile.getline (buffer,100);
			myfile.getline (buffer,100);
			myfile.getline (buffer3,100);  //pass admin
			myfile.getline (buffer,100);
			myfile.getline (buffer,100);
			myfile.getline (buffer4,100);  //frase para o smile
			myfile.getline (buffer,100);
			myfile.getline (buffer,100);
			myfile.getline (buffer5,100);  //frase para o semafero
			myfile.getline (buffer,100);
			myfile.getline (buffer,100);
			myfile.getline (buffer6,100);  //indica o ip do servidor de teamspeak
			myfile.getline (buffer,100);
			myfile.getline (buffer,100);
			myfile.getline (buffer7,100);  //site do servidor
			myfile.getline (buffer,100);
			myfile.getline (buffer,100);
			myfile.getline (buffer8,100);  //messagem suplementar, webpage ou informação necessaria
			myfile.getline (buffer,100);
			myfile.getline (buffer,100);
			myfile.getline (buffer9,100);  //messagem exibida no quadro, webpage ou informação necessaria
			myfile.getline (buffer10,100);
			myfile.getline (buffer11,100);
			myfile.getline (buffer12,100);
			myfile.getline (buffer13,100);

			goto seguir;
}

seguir:

  int retVal = 0;
  struct IS_VER verPack;	//LFS can tell us info about it's version and InSim version, store that info in this struct.
  
  //Try to connect to LFS
  retVal = insim.init(buffer1,	//LFS will run on local machine
		      (word)atoi(buffer2),	//InSim will accept connection on port 29999
		      "Simplex v2",	//Short name for our app, will appear in LFS
		      buffer3,		//No admin password
		      &verPack,		//Pointer to IS_VERSION packet
		      '!',		//InSim command character
		      ISF_MCI,		//We're writing a local InSim app
		      1000,		//Receive updates every 1000 msecs
		      0);		//No UDP today
  
  //Could we connect to LFS?
  if(retVal < 0) //We could not
  {
    //Tell the user and exit
    std::cerr << "Erro ao conectar o LFS!" << std::endl;
    return -1;
  }
  
  //All OK, report version of LFS and InSim
  std::cout << "Insim simplex v2 by paparazzi 9.1.2012" << std::endl;
  std::cout << ": Conectado ao LFS " << verPack.Version << ", InSim ver. " << verPack.InSimVer << std::endl;
  
  //Now enter the main loop which will receive packets from LFS
  int recvErr = 0;
  int packetType = 0;
  bool canRun = true;	//Run the loop as long as this is true


  						struct IS_MST mst_inicio;
						memset(&mst_inicio, 0, sizeof(struct IS_MST));
						mst_inicio.Size = sizeof(struct IS_MST);
						mst_inicio.Type = ISP_MST;
						sprintf(mst_inicio.Msg, "/msg ^7InSim - TCP : Simplex v2 por paparazzi esta activo");
						insim.send_packet(&mst_inicio);

						sprintf(mst_inicio.Msg, "/msg ^8Comandos disponiveis: ^7!ts !site !inf !luz !sm !q1 !q0");
						insim.send_packet(&mst_inicio);

						sprintf(mst_inicio.Msg, "/msg ^8Teamspeak 3 (chat por mic): ^7%s", buffer6);
						insim.send_packet(&mst_inicio);

						sprintf(mst_inicio.Msg, "/msg ^7%s", buffer7);
						insim.send_packet(&mst_inicio);
  
  while(canRun)
  {
    //Get the next packet in the buffer
    recvErr = insim.next_packet();
    if(recvErr < 0)	//Something went wrong
    {
      //Tell the user and exit
      std::cerr << ": Erro ao receber pacotes no LFS!" << std::endl;
      return -1;
    }
    
    //We have a packet, check it's type
    packetType = insim.peek_packet();
    
    //Handle each type of packet
    switch(packetType)
    {
        case ISP_MSO:
			{

		struct IS_MSO* mso = (struct IS_MSO*)insim.get_packet();
		  if(mso->TextStart!=0)
		  {

			  std::string frase=mso->Msg;
			  int c_linha = frase.find_first_of('!'); //posição do caracter "!"
			  int c_textstart = frase.find_first_of("^L"); //começo da frase
			  int tam_frase=frase.size();  //tamanho total da frase

			  if(c_linha==c_textstart+2)
			  {
				  
				  std::string cmd_smile(frase,c_linha,3);
				  std::string cmd_ts(frase,c_linha,3);
				  std::string cmd_luz(frase,c_linha,4);
				  std::string cmd_inf(frase,c_linha,4);
				  std::string cmd_q1(frase,c_linha,3);
				  std::string cmd_q0(frase,c_linha,3);
				  std::string cmd_site(frase,c_linha,5);


				  //!ts = ?
				  if(cmd_ts=="!ts")
				  {
					  	struct IS_MST mst_ts;
						memset(&mst_ts, 0, sizeof(struct IS_MST));
						mst_ts.Size = sizeof(struct IS_MST);
						mst_ts.Type = ISP_MST;
				  		sprintf(mst_ts.Msg, "/msg ^8Teamspeak 3 (chat por mic): ^7%s", buffer6);
						insim.send_packet(&mst_ts);
				  }

				  //!smile = ?
				  if(cmd_smile=="!sm")
				  {
						struct IS_MST mst_smile;
						memset(&mst_smile, 0, sizeof(struct IS_MST));
						mst_smile.Size = sizeof(struct IS_MST);
						mst_smile.Type = ISP_MST;
						sprintf(mst_smile.Msg, "/msg %s",buffer4);
						insim.send_packet(&mst_smile);

				  }

				  //!luz = ?
				  if(cmd_luz=="!luz")
				  {
						struct IS_MST mst_luz;
						memset(&mst_luz, 0, sizeof(struct IS_MST));
						mst_luz.Size = sizeof(struct IS_MST);
						mst_luz.Type = ISP_MST;
						sprintf(mst_luz.Msg, "/msg %s",buffer5);
						insim.send_packet(&mst_luz);
						semafero();

				  }

				  if(cmd_inf=="!inf")
				  {
						struct IS_MST mst_inf;
						memset(&mst_inf, 0, sizeof(struct IS_MST));
						mst_inf.Size = sizeof(struct IS_MST);
						mst_inf.Type = ISP_MST;
						sprintf(mst_inf.Msg, "/msg ^7%s", buffer8);
						insim.send_packet(&mst_inf);

				  }

				  				  //!c1 = ?
				  if(cmd_q1=="!q1")
				  {
					  int y_comando=130;
					    struct IS_BTN Btn_q1; 
						  memset(&Btn_q1, 0, sizeof(Btn_q1));
						  Btn_q1.Size = sizeof(struct IS_BTN);
						  Btn_q1.Type = ISP_BTN;		
						  Btn_q1.ReqI = 1;
						  Btn_q1.UCID = 255;
						  Btn_q1.ClickID = 10;		
						  Btn_q1.BStyle = ISB_RIGHT;
						  Btn_q1.L = 100;		
						  Btn_q1.T = y_comando;	
						  Btn_q1.H = 5;	
						  Btn_q1.W = 100;

						  sprintf(Btn_q1.Text, "%s", buffer9);
						  insim.send_packet(&Btn_q1);

						  Btn_q1.ClickID = 11;
						  Btn_q1.T = y_comando+5;
						  sprintf(Btn_q1.Text, "%s", buffer10);
						  insim.send_packet(&Btn_q1);

						  Btn_q1.ClickID = 12;
						  Btn_q1.T = y_comando+10;
						  sprintf(Btn_q1.Text, "%s", buffer11);
						  insim.send_packet(&Btn_q1);

						  Btn_q1.ClickID = 13;
						  Btn_q1.T = y_comando+15;
						  sprintf(Btn_q1.Text, "%s", buffer12);
						  insim.send_packet(&Btn_q1);

						  Btn_q1.ClickID = 14;
						  Btn_q1.T = y_comando+20;
						  sprintf(Btn_q1.Text, "%s", buffer13); 
						  insim.send_packet(&Btn_q1);

				  }

				  				  //!c0 = ?
				  if(cmd_q0=="!q0")
				  {

					  	struct IS_BFN Bfn_q0; //apaga o botao
							memset(&Bfn_q0, 0,sizeof(Bfn_q0));
							Bfn_q0.Size = sizeof(struct IS_BFN);
							Bfn_q0.Type = ISP_BFN;
							Bfn_q0.SubT = BFN_DEL_BTN;
							Bfn_q0.ReqI = 1;
							Bfn_q0.UCID = 255;
							Bfn_q0.ClickID = 10;	
							insim.send_packet(&Bfn_q0);

							Bfn_q0.ClickID = 11;	
							insim.send_packet(&Bfn_q0);

							Bfn_q0.ClickID = 12;	
							insim.send_packet(&Bfn_q0);

							Bfn_q0.ClickID = 13;	
							insim.send_packet(&Bfn_q0);

							Bfn_q0.ClickID = 14;	
							insim.send_packet(&Bfn_q0);
				  }


				  			//!site = ?
				  if(cmd_site=="!site")
				  {
						struct IS_MST mst_site;
						memset(&mst_site, 0, sizeof(struct IS_MST));
						mst_site.Size = sizeof(struct IS_MST);
						mst_site.Type = ISP_MST;
						sprintf(mst_site.Msg, "/msg ^7%s", buffer7);
						insim.send_packet(&mst_site);

				  }


			  }
		  }
		  }
	break;

	case ISP_NCN:
		{

		struct IS_NCN* ncn = (struct IS_NCN*)insim.get_packet();
		if(ncn->UName[24]!=0)
		{
			  			struct IS_MST mst_2;
						memset(&mst_2, 0, sizeof(struct IS_MST));
						mst_2.Size = sizeof(struct IS_MST);
						mst_2.Type = ISP_MST;
						sprintf(mst_2.Msg, "/msg ^7Bem vindo! Simplex v2 está activo");
						insim.send_packet(&mst_2);

						sprintf(mst_2.Msg, "/msg ^8Comandos disponiveis: ^7!ts !site !nov !luz !sm !q1 !q0");
						insim.send_packet(&mst_2);

						sprintf(mst_2.Msg, "/msg ^8Teamspeak 3 (chat por mic): ^7%s", buffer6);
						insim.send_packet(&mst_2);

						sprintf(mst_2.Msg, "/msg ^7%s", buffer8);
						insim.send_packet(&mst_2);


		}
		}

	break;

    }
    
  }
  
  //Successful exit
  insim.isclose();
  return 0;
}
