/*Troy Rosin*/

#include <main.h>
#include <game.h>
#include <input.h>
#include <overworld.h>

#include <mydebug.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL_ttf.h>

#define SPACING 20

SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event event;

int state = TITLE;
int running;


int selected_index = START_GAME;

int main(void) {
  int x1, x2, y1, y2;
  SDL_Point mouse_pos;
  SDL_Init(SDL_INIT_VIDEO);
  TTF_Init();
  window = SDL_CreateWindow("Shufflers", SDL_WINDOWPOS_UNDEFINED,
     SDL_WINDOWPOS_UNDEFINED, DEF_WINDOW_WIDTH,DEF_WINDOW_HEIGHT, 0);
  renderer = SDL_CreateRenderer(window, -1, 0);
  IMG_Init(IMG_INIT_PNG);
  SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);

  SDL_CaptureMouse(SDL_TRUE);  
 
  SDL_Texture* background = IMG_LoadTexture(renderer, "assets/mainscreen.png");
  
  if (!background) {
    printf("IMG_LoadTexture error: %s\n", IMG_GetError());
  } 

  
  
  TTF_Font* font = TTF_OpenFont("assets/font.ttf", 24);
  
  SDL_Color white = {255, 255, 255, 0};
  SDL_Surface* start_surface = TTF_RenderText_Solid(font, "START", white);
  SDL_Texture* start_text = SDL_CreateTextureFromSurface(renderer,
    start_surface); 
  SDL_Surface* host_surface = TTF_RenderText_Solid(font, "HOST", white);
  SDL_Texture* host_text = SDL_CreateTextureFromSurface(renderer,
    host_surface); 
  SDL_Surface* join_surface = TTF_RenderText_Solid(font, "JOIN", white);
  SDL_Texture* join_text = SDL_CreateTextureFromSurface(renderer,
    join_surface); 
  SDL_Surface* exit_surface = TTF_RenderText_Solid(font, "EXIT", white);
  SDL_Texture* exit_text = SDL_CreateTextureFromSurface(renderer,
    exit_surface); 
  SDL_Surface* options_surface = TTF_RenderText_Solid(font, "OPTIONS", white);
  SDL_Texture* options_text = SDL_CreateTextureFromSurface(renderer,
    options_surface); 
  
  SDL_Rect start_rect, host_rect, join_rect, exit_rect, options_rect;

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

  host_rect.w = join_rect.w = exit_rect.w = options_rect.w = start_rect.w;
  options_rect.w <<= 1;
  host_rect.h = join_rect.h = exit_rect.h = options_rect.h = start_rect.h;
  host_rect.x = join_rect.x = exit_rect.x = options_rect.x = start_rect.x;
  options_rect.x -= join_rect.w >> 1;

  host_rect.y = start_rect.y + start_rect.h + SPACING;
  join_rect.y = host_rect.y + host_rect.h + SPACING;
  options_rect.y = join_rect.y + join_rect.h + SPACING;
  exit_rect.y = options_rect.y + options_rect.h + SPACING;
    
  SDL_Rect mainmenu_rects[MAIN_SELECTIONS];
  
  mainmenu_rects[START_GAME] = start_rect;
  mainmenu_rects[HOST_GAME] = host_rect;
  mainmenu_rects[JOIN_GAME] = join_rect;
  mainmenu_rects[OPTIONS_MENU] = options_rect;
  mainmenu_rects[EXIT_GAME] = exit_rect;

  running = 1;

  
  while (running) 
  {
    handle_event(&event);
      
    SDL_RenderClear(renderer);
    
    
 
    SDL_SetRenderDrawColor(renderer, 0,196,0,255);

    SDL_RenderCopy(renderer, background, NULL, NULL);
    
    SDL_RenderCopy(renderer, start_text, NULL, &start_rect);
    SDL_RenderCopy(renderer, host_text, NULL, &host_rect);
    SDL_RenderCopy(renderer, join_text, NULL, &join_rect);
    SDL_RenderCopy(renderer, options_text, NULL, &options_rect);
    SDL_RenderCopy(renderer, exit_text, NULL, &exit_rect);
  
    SDL_GetMouseState(&(mouse_pos.x), &(mouse_pos.y));
    for(int i = 0; i<MAIN_SELECTIONS; i++)
    {
      if(SDL_PointInRect(&mouse_pos, &mainmenu_rects[i]))
      {
        selected_index = i;
        break;
      }
    }

 
    switch(selected_index)
    {
      case START_GAME:
        x1 = start_rect.x;
        x2 = x1 + start_rect.w;
        y1 = y2 = start_rect.y + start_rect.h;
        break;
      case HOST_GAME: 
        x1 = host_rect.x;
        x2 = x1 + host_rect.w;
        y1 = y2 = host_rect.y + host_rect.h; 
        break;
      case JOIN_GAME:
        x1 = join_rect.x;
        x2 = x1 + join_rect.w;
        y1 = y2 = join_rect.y + join_rect.h;
        break;
      case OPTIONS_MENU:
        x1 = options_rect.x;
        x2 = x1 + options_rect.w;
        y1 = y2 = options_rect.y + options_rect.h;
        break;
      case EXIT_GAME:
        x1 = exit_rect.x;
        x2 = x1 + exit_rect.w;
        y1 = y2 = exit_rect.y + exit_rect.h;
        break;       
    }

    SDL_RenderDrawLine(renderer, x1, y1, x2, y2);  

    SDL_RenderPresent(renderer);
    if(state)
    {
      switch(state)
      {
        case START:
          start_game();
          break;
        case HOST:
          PRINT_M(hosting state)
          host_game();
          break;
        case JOIN:
          PRINT_M(joining state)
          join_game();
          break;
        case OPTIONS:
          PRINT_M(options menu state)
          show_options();
          break;
      }
    }    
    
  }

  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  IMG_Quit();
  SDL_Quit();
  return 0;
}

