/*Troy Rosin*/

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <mydebug.h>
#include <game.h>
#include <input.h>
#include <deck.h>
#include <main.h>
#include <battle.h>

#define BOARDER_THICKNESS 10
//do not let dx or dy be anything less than 2
#define DX 5
#define DY 5
#define DRAW_TIMEOUT 10

SDL_Rect mouse_rect;
SDL_Rect bot_rect, hand_rect;
SDL_Point mouse_pos;

int check_col(int i);
int selected_card;

int dragging;

void start_battle(){
  int i, cardoff_x, cardoff_y;
  int mx, my;
  state = BATTLE;
  selected_card = -1;
  dragging = 0;  
  PRINT_M(STARTING GAME LOOP);
 
  //SDL_Texture *maroon = IMG_LoadTexture(renderer, "assets/maroon.png");
  SDL_Texture *grid = IMG_LoadTexture(renderer, "assets/battle_grid.png");
  SDL_Texture *card = IMG_LoadTexture(renderer, "assets/default_card.png");
  SDL_Texture *selected = IMG_LoadTexture(renderer, "assets/selected_card.png");
  SDL_Texture *enemy_grid = IMG_LoadTexture(renderer, "assets/enemy_grid.png");
  SDL_Texture *friendly_grid = IMG_LoadTexture(renderer, "assets/friendly_grid.png");
  SDL_Texture *friendly_far = IMG_LoadTexture(renderer, "assets/friendly_far.png");
  SDL_Texture *selected_grid = IMG_LoadTexture(renderer, "assets/selected_grid.png");


  mouse_rect.x = 0;
  mouse_rect.y = 0;
  mouse_rect.w = 5;
  mouse_rect.h = 5;

  bot_rect.w = DEF_WINDOW_WIDTH - 2 * BOARDER_THICKNESS;
  bot_rect.x = BOARDER_THICKNESS;
  bot_rect.h = DEF_WINDOW_HEIGHT >> 3;
  bot_rect.y = DEF_WINDOW_HEIGHT - BOARDER_THICKNESS - bot_rect.h;
  
  hand_rect.w = bot_rect.w >> 1;
  hand_rect.x = DEF_WINDOW_WIDTH >> 2;
  hand_rect.y = bot_rect.y - BOARDER_THICKNESS;
  hand_rect.h = bot_rect.h;
  

 
  mouse_pos.x = mouse_pos.y = 0;
  
  if(!grid || !card || !selected)
  {
    printf("IMG_LOAD err: %s\n", IMG_GetError());
  }

  Uint64 lastdraw = SDL_GetTicks64() + DRAW_TIMEOUT;
  
  HAND hand;
  hand.handsize = 7;
  for(i=0; i<7; i++){
    hand.cards[i] = card;
    hand.card_rects[i].w = CARD_SIZE;
    hand.card_rects[i].h = CARD_SIZE;
  }  
  
  battle_rows[0] = enemy_backrow;
  battle_rows[1] = enemy_frontrow;
  battle_rows[2] = friendly_frontrow;
  battle_rows[3] = friendly_midrow;
  battle_rows[4] = friendly_backrow;
  battle_rows[5] = friendly_farrow;

  //battle grid setup
  //enemy setup
  int grid_w, grid_h;
  int side_buffer = DEF_WINDOW_WIDTH / (BATTLE_COLS + 2);
  enemy_frontrow[0].x = enemy_backrow[0].x = side_buffer;
  enemy_frontrow[0].w = grid_w = enemy_backrow[0].w = side_buffer;
  enemy_backrow[0].y = GRID_SPACING;
  enemy_frontrow[0].h = grid_h = enemy_backrow[0].h = side_buffer - (side_buffer >> 2 );
  enemy_frontrow[0].y = (GRID_SPACING * 2) + grid_h;

  for(i = 1; i < BATTLE_COLS; i++)
  { 
    enemy_frontrow[i].x = enemy_backrow[i].x = enemy_backrow[i-1].x + grid_w + GRID_SPACING;
    enemy_frontrow[i].w = enemy_backrow[i].w = grid_w;
    enemy_backrow[i].y = GRID_SPACING;
    enemy_frontrow[i].h = enemy_backrow[i].h = grid_h;
    enemy_frontrow[i].y = enemy_frontrow[0].y;
  }
  //friendly side setup
  for(i=0; i < BATTLE_COLS; i++)
  {
    friendly_farrow[i].x = friendly_backrow[i].x = friendly_midrow[i].x =
       friendly_frontrow[i].x = enemy_frontrow[i].x;
    friendly_farrow[i].w = friendly_backrow[i].w = friendly_midrow[i].w =
       friendly_frontrow[i].w = grid_w;
     friendly_backrow[i].h = friendly_midrow[i].h =
       friendly_frontrow[i].h = grid_h;
    friendly_farrow[i].h = grid_h >> 1;
    friendly_frontrow[i].y = enemy_frontrow[i].y + grid_h + (GRID_SPACING * 3);
    friendly_midrow[i].y = friendly_frontrow[i].y + grid_h + GRID_SPACING; 
    friendly_backrow[i].y = friendly_midrow[i].y + grid_h + GRID_SPACING; 
    friendly_farrow[i].y = friendly_backrow[i].y + grid_h + GRID_SPACING; 
  }
  
  int check = 1;
  int inside = 0;
  while(running)
  {
    handle_event(&event);

    SDL_GetMouseState(&mx, &my);
    mouse_pos.x = mouse_rect.x = mx;
    mouse_pos.y = mouse_rect.y = my;
    //handle resizing hand box
    inside = SDL_PointInRect(&mouse_pos, &hand_rect);
    if(SDL_GetTicks64() > lastdraw)
    {
      lastdraw = SDL_GetTicks() + DRAW_TIMEOUT;
      if(inside)
      {
        if(hand_rect.w < (DEF_WINDOW_WIDTH >> 1) + (DEF_WINDOW_WIDTH >> 3) )
        { //growing
          hand_rect.x -= DX>>1;
          hand_rect.w += DX;
          hand_rect.y -= DY;
          hand_rect.h += DY;
        }
      }else if(hand_rect.w > bot_rect.w >> 1){
        //shrinking
        hand_rect.x += DX>>1;
        hand_rect.w -= DX;
        hand_rect.y += DY;
        hand_rect.h -= DY;
        if(!dragging){ selected_card = -1; }
      }
    }
    int useable_hand_space = hand_rect.w - (BOARDER_THICKNESS << 1) - CARD_W;
    int spacing;
    if(hand.handsize == 1)
    {
      spacing = useable_hand_space;
    }else
    { 
      spacing  = useable_hand_space / (hand.handsize - 1);
    }
    check = 1;
    if(selected_card == -1) { check = 1;}
    else if(SDL_PointInRect(&mouse_pos, &(hand.card_rects[selected_card]))){ check = 0;}
    else { selected_card = -1; }
    for(i = 0; i < hand.handsize; i++)
    {
      hand.card_rects[i].x = hand_rect.x + BOARDER_THICKNESS + ( spacing * i)
         - ((CARD_SIZE - CARD_W)>>1) ;
      hand.card_rects[i].y = hand_rect.y + BOARDER_THICKNESS;
      if(check && SDL_PointInRect(&mouse_pos, &(hand.card_rects[i])))
      {
        selected_card = i; 
      }
    }
    
    //handle dragging cards
    if(dragging)
    {
      if(dragging == GRAB_OLD)
      {
        hand.card_rects[selected_card].x = mouse_pos.x - cardoff_x;
        hand.card_rects[selected_card].y = mouse_pos.y - cardoff_y;
      }
      else if(dragging == GRAB_NEW)
      {
        if(selected_card != -1)
        {
          cardoff_x = mouse_pos.x - hand.card_rects[selected_card].x;
          cardoff_y = mouse_pos.y - hand.card_rects[selected_card].y;
          dragging = GRAB_OLD;
        } else
        {
          dragging = NOT_DRAG;
        }
      }
      else if(dragging == LETGO && selected_card != -1){
        dragging = NOT_DRAG;
        if(!inside)
        {
          hand.handsize--;
          for(i = selected_card; i < hand.handsize; i++)
          {
            hand.cards[i] = hand.cards[i+1];
          }
          selected_card = -1;
          hand.cards[hand.handsize + 1] = NULL;
        }
      }
    }
    

    SDL_RenderClear(renderer);
    //background this isnt a grid anymore
    SDL_RenderCopy(renderer, grid, NULL, NULL);
    //render battle grid
    check = 1;
    int sel_row = 0;
    for(i = 0; i < BATTLE_COLS; i++)
    { 
      SDL_RenderCopy(renderer, enemy_grid, NULL, &(enemy_backrow[i]));
      SDL_RenderCopy(renderer, enemy_grid, NULL, &(enemy_frontrow[i]));
      SDL_RenderCopy(renderer, friendly_grid, NULL, &(friendly_frontrow[i]));
      SDL_RenderCopy(renderer, friendly_grid, NULL, &(friendly_midrow[i]));
      SDL_RenderCopy(renderer, friendly_grid, NULL, &(friendly_backrow[i]));
      SDL_RenderCopy(renderer, friendly_far, NULL, &(friendly_farrow[i]));
      if(check && (sel_row = check_col(i)))
      {
        check = 0;
        SDL_RenderCopy(renderer, selected_grid, NULL, &(battle_rows[sel_row-1][i]));
      }
    }
    
      

    //bottom box for ui shit
    SDL_SetRenderDrawColor(renderer, 0,0,0,255);
    SDL_RenderFillRect(renderer, &bot_rect);    
    
    //box for hand of cards
    SDL_SetRenderDrawColor(renderer, 64,64,64,255);
    SDL_RenderFillRect(renderer, &hand_rect);    
    
    //mouse follower box
    SDL_SetRenderDrawColor(renderer, 0,255,0,255);
    SDL_RenderFillRect(renderer, &mouse_rect);    
    
    //render cards
    for(int i = 0; i < hand.handsize; i++)
    {
      if(selected_card != i)
      {
        SDL_RenderCopy(renderer, hand.cards[i], NULL, &(hand.card_rects[i]));
      }
    }
    //render selected card last
    if(selected_card != -1)
    { 
      if(!dragging){ hand.card_rects[selected_card].y -= BOARDER_THICKNESS; }
      SDL_RenderCopy(renderer, hand.cards[selected_card], NULL,
        &(hand.card_rects[selected_card]));
      SDL_RenderCopy(renderer, selected, NULL,
        &(hand.card_rects[selected_card]));
    }

    SDL_RenderPresent(renderer);
  }

  return;
}

//bad but im lazy lol fix in future
int check_col(int i){
  int ret = SDL_PointInRect(&mouse_pos, &(enemy_backrow[i])) * 1;
  ret |= SDL_PointInRect(&mouse_pos, &(enemy_frontrow[i])) * 2;
  ret |= SDL_PointInRect(&mouse_pos, &(friendly_frontrow[i])) * 3;
  ret |= SDL_PointInRect(&mouse_pos, &(friendly_midrow[i])) * 4;
  ret |= SDL_PointInRect(&mouse_pos, &(friendly_backrow[i])) * 5;
  ret |= SDL_PointInRect(&mouse_pos, &(friendly_farrow[i])) * 6;
  return ret;
}


