//troy Rosin

#include <SDL2/SDL_net.h>
#include <SDL2/SDL.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <mydebug.h>
#include <net.h>

#define SECS 1

char buf[PACKSZ];
char pack[PACKSZ];
packet msg;

int main(void)
{
  
  IPaddress ip;
  TCPsocket sock;
  int ret = 0;
 
  SDL_Init(0);

  SDLNet_Init();

  if(SDLNet_ResolveHost(&ip, SERVER, PORT) == -1)
  {
    printf("SDLNet_resolvehost error: %s\n", SDLNet_GetError());
    exit(1);
  }  

  sock = SDLNet_TCP_Open(&ip);
  if(!sock)
  {
    printf("SDL_Net_TCP_Open error: %s\n", SDLNet_GetError());
    exit(1);
  }
 
  PRINT_M("starting"); 
  
  msg.type = G_STATE;  
  write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
  PRINT_M("packed up!");
  SDLNet_TCP_Send(sock, pack, PACKSZ);
  PRINT_M("sent"); 
  sleep(SECS);

  msg.type = S_STATE;
  write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
  SDLNet_TCP_Send(sock, pack, PACKSZ);
  PRINT_M("sent"); 
  sleep(SECS);

  msg.type = HOSTS_REQ;
  write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
  SDLNet_TCP_Send(sock, pack, PACKSZ);
  PRINT_M("sent"); 
  sleep(SECS);

  msg.type = NEW_HOST;
  write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
  SDLNet_TCP_Send(sock, pack, PACKSZ);
  PRINT_M("sent"); 
  sleep(SECS);

  msg.type = JOIN_REQ;
  write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
  SDLNet_TCP_Send(sock, pack, PACKSZ);
  PRINT_M("sent"); 
  sleep(SECS);

  msg.type = U_NAME;
  write_to_buff((Uint32*)pack, (Uint32*)&msg, PACKSZ);
  SDLNet_TCP_Send(sock, pack, PACKSZ);
  PRINT_M("sent"); 
  sleep(SECS);

  SDLNet_TCP_Close(sock);
  SDLNet_Quit();
  SDL_Quit();

  puts("donzo");


  return 0;
}




