#include "game.h"
#include "card.h"
#include "points.h"
#include "special_cases.h"

int hand_count_chaff(Hand *hand) {
  int chaff = 0;
  for (int i = 0; i < hand->count; i++) {
    Card *c = hand->cards[i];
    if (c->type == CHAFF || c->month == NOVEMBER) chaff++;
  }
  return chaff;
}

SpecialCase calculate_special_case(Game *g) {
  int player_points = hand_points(&g->player.scored);
  int right_points = hand_points(&g->right.scored);
  int left_points = hand_points(&g->left.scored);

  if (player_points == 0 &&
      right_points == 0 &&
      left_points == 0) {
    return (SpecialCase) { SPECIAL_CASE_ALL_EIGHTS, SPECIAL_CASE_TARGET_DEALER, 10 };
  }

  if (player_points >= 80)
    return (SpecialCase) { SPECIAL_CASE_DOUBLE_EIGHTS, SPECIAL_CASE_TARGET_PLAYER, player_points - 70 };
  if (right_points >= 80)
    return (SpecialCase) { SPECIAL_CASE_DOUBLE_EIGHTS, SPECIAL_CASE_TARGET_RIGHT, right_points - 70 };
  if (left_points >= 80)
    
    return (SpecialCase) { SPECIAL_CASE_DOUBLE_EIGHTS, SPECIAL_CASE_TARGET_LEFT, left_points - 70 };

  int player_chaff = hand_count_chaff(&g->player.scored);
  if (player_chaff >= 16)
    return (SpecialCase) { SPECIAL_CASE_SIXTEEN_CHAFF, SPECIAL_CASE_TARGET_PLAYER, (2 * player_chaff) - 20 }; 

  int right_chaff = hand_count_chaff(&g->right.scored);
  if (right_chaff >= 16)
    return (SpecialCase) { SPECIAL_CASE_SIXTEEN_CHAFF, SPECIAL_CASE_TARGET_RIGHT, (2 * right_chaff) - 20 }; 

  int left_chaff = hand_count_chaff(&g->left.scored);
  if (left_chaff >= 16)
    return (SpecialCase) { SPECIAL_CASE_SIXTEEN_CHAFF, SPECIAL_CASE_TARGET_LEFT, (2 * left_chaff) - 20 }; 

  return (SpecialCase) { SPECIAL_CASE_NONE, SPECIAL_CASE_TARGET_NONE, 0 };
}

