/*Troy Rosin*/

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL_ttf.h>
#include <mydebug.h>
#include <game.h>
#include <input.h>
#include <deck.h>
#include <main.h>
#include <host.h>
#include <err_check.h>


#define BOARDER_THICKNESS 20

int over_object;

char player_names[MAX_PLAYERS][MAX_NAMESZ];
char last_sent_name[MAX_NAMESZ];
int num_players = 1;

char *get_this_name(int i)
{
  if(i >= 0 && i < MAX_PLAYERS){ return player_names[i]; }
  return NULL;
}


void host_game(){
  int i;
  PRINT_M(HOST state)      
  
  SDL_Point mouse_pos;
    
  SDL_Rect set_name_rect, name_rect, player_name_rect;
  SDL_Rect player_rects[MAX_PLAYERS]; 
  SDL_Rect player_name_rects[MAX_PLAYERS]; 
  SDL_Rect start_rect; 
  
  over_object = NO_OBJECT;

  strcpy(player_names[0], "player_1");   
  strcpy(player_names[1], "no_player_2");
  strcpy(player_names[2], "no_player_3");
  strcpy(player_names[3], "no_player_4");

  TTF_Font* font = TTF_OpenFont("assets/font.ttf", 24);
  SDL_Color white = {255, 255, 255, 0};
  SDL_Surface *set_name_surf = TTF_RenderText_Solid(font, "set name", white);
  SDL_Surface *start_surf = TTF_RenderText_Solid(font, "start game", white);
  
  SDL_Texture *set_name_tex = SDL_CreateTextureFromSurface(renderer, set_name_surf); 
  SDL_Texture *start_tex = SDL_CreateTextureFromSurface(renderer, start_surf);
   


  set_name_rect.w = DEF_WINDOW_WIDTH >> 3;
  set_name_rect.h = DEF_WINDOW_HEIGHT >> 4;
  set_name_rect.x = DEF_WINDOW_WIDTH - (set_name_rect.w << 1);
  set_name_rect.y = DEF_WINDOW_HEIGHT - (set_name_rect.h << 2);

  name_rect.w = DEF_WINDOW_WIDTH - (set_name_rect.w << 2) - (BOARDER_THICKNESS << 1);
  name_rect.h = set_name_rect.h;
  name_rect.x = (set_name_rect.w << 1) + BOARDER_THICKNESS;
  name_rect.y = set_name_rect.y;
  
  start_rect.w = set_name_rect.w;
  start_rect.h = set_name_rect.h;
  start_rect.x = (DEF_WINDOW_WIDTH >> 1) - (start_rect.w >> 1);
  start_rect.y = set_name_rect.y + set_name_rect.h + BOARDER_THICKNESS;

  player_name_rect.w = DEF_WINDOW_WIDTH - (set_name_rect.w << 2) - (BOARDER_THICKNESS << 1);
  player_name_rect.h = set_name_rect.h;
  player_name_rect.x = (set_name_rect.w << 1) + BOARDER_THICKNESS;
  player_name_rect.y = set_name_rect.y;
  
  player_rects[0].w = player_rects[1].w = player_rects[2].w = player_rects[3].w
    = DEF_WINDOW_WIDTH - (set_name_rect.w << 1);
  player_rects[0].h = player_rects[1].h = player_rects[2].h = player_rects[3].h
    = ((DEF_WINDOW_HEIGHT - (DEF_WINDOW_HEIGHT - set_name_rect.y))
         - (BOARDER_THICKNESS << 2) - BOARDER_THICKNESS) >> 2;  
  player_rects[0].x = player_rects[1].x = player_rects[2].x = player_rects[3].x
    = set_name_rect.w;
 
  player_rects[0].y = BOARDER_THICKNESS;
  player_rects[1].y = player_rects[0].y + player_rects[0].h + BOARDER_THICKNESS; 
  player_rects[2].y = player_rects[1].y + player_rects[1].h + BOARDER_THICKNESS; 
  player_rects[3].y = player_rects[2].y + player_rects[2].h + BOARDER_THICKNESS; 

  for(i = 0; i < MAX_PLAYERS; i++){
    player_name_rects[i].w = player_rects[i].w;
    player_name_rects[i].h = player_rects[i].h - (BOARDER_THICKNESS << 1);
    player_name_rects[i].x = player_rects[i].x + BOARDER_THICKNESS;
    player_name_rects[i].y = player_rects[i].y + BOARDER_THICKNESS;
  }



  SDL_StartTextInput();
  SDL_SetTextInputRect(&name_rect);
  SDL_Surface *player_name_surfs[MAX_PLAYERS];
  SDL_Texture *player_name_texs[MAX_PLAYERS];


  while(running)
  {
  
    SDL_GetMouseState(&(mouse_pos.x), &(mouse_pos.y));  
    
    if(SDL_PointInRect(&mouse_pos, &start_rect))
    {
      over_object = START_GAME_BUTTON;
    }
    else if(SDL_PointInRect(&mouse_pos, &set_name_rect))
    {
      over_object = SET_NAME_BUTTON;
    }


    handle_event(&event);

    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    SDL_RenderClear(renderer);
    
    //set name button
    SDL_SetRenderDrawColor(renderer, 64, 64, 64,255);
    SDL_RenderFillRect(renderer, &set_name_rect);
    SDL_RenderCopy(renderer, set_name_tex, NULL, &(set_name_rect));    
    
    //player fields
    for(i = 0; i < MAX_PLAYERS; i++)
    {
      player_name_rects[i].w = (player_rects[0].w / MAX_NAMESZ) * strlen(player_names[i]);
      player_name_surfs[i] = TTF_RenderText_Solid(font, player_names[i], white);
      player_name_texs[i] = SDL_CreateTextureFromSurface(renderer, player_name_surfs[i]);
      SDL_RenderFillRect(renderer, &(player_rects[i]));
      SDL_RenderCopy(renderer, player_name_texs[i], NULL, &(player_name_rects[i]));
    }
    //name field
    player_name_rect.w = (name_rect.w / MAX_NAMESZ) * strlen(player_names[0]);
    SDL_RenderFillRect(renderer, &name_rect);
    SDL_RenderCopy(renderer, player_name_texs[0], NULL, &player_name_rect);
  
    //start game button
    SDL_RenderFillRect(renderer, &start_rect);
    SDL_RenderCopy(renderer, start_tex, NULL, &(start_rect));    
       
    
    SDL_RenderPresent(renderer);
    for(i = 0; i < MAX_PLAYERS; i++)
    {
      SDL_FreeSurface(player_name_surfs[i]);
      SDL_DestroyTexture(player_name_texs[i]);
    }
  }

  return;
}






