//    Copyright 2025 Bill Rossi
//    
//    This file is part of Hanafuda Hachi-Hachi.
//
//    Hanafuda Hachi-Hachi is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//
//    Hanafuda Hachi-Hachi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License along with Hanafuda Hachi-Hachi. If not, see <https://www.gnu.org/licenses/>. 
#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 };
}