/**
 * Copyright (c) 2021 Dominic Masters
 * 
 * This software is released under the MIT License.
 * https://opensource.org/licenses/MIT
 */

#include "bet.h"

uint8_t pokerBetGetNextPlayer(poker_t *poker) {
  uint8_t i, j;
  for(i = 0; i < poker->playerCount; i++) {
    j = (i + poker->better + 1) % poker->playerCount;
    if(pokerPlayerDoesNeedToBetThisRound(poker, j)) return j;
  }
  return 0xFF;
}

uint8_t pokerBetGetRemainingBetterCount(poker_t *poker) {
  uint8_t i, count;
  count = 0;
  for(i = 0; i < poker->playerCount; i++) {
    if(!pokerPlayerDoesNeedToBetThisRound(poker, i)) continue;
    count++;
  }
  return count;
}

void pokerBet(
  poker_t *poker, pokerpot_t *pot, uint8_t playerIndex, int32_t chips
) {
  pokerplayer_t *player;
  player = poker->players + playerIndex;

  player->chips -= chips;
  player->currentBet += chips;

  pot->chips += chips;
  pot->call = mathMax(pot->call, player->currentBet);
  pokerPotAddPlayer(pot, playerIndex);

  player->state |= POKER_PLAYER_STATE_HAS_BET_THIS_ROUND;
  if(chips > 0) player->timesRaised++;
}

void pokerBetForPlayer(poker_t *poker, uint8_t playerIndex, int32_t chips) {
  pokerBet(
    poker, poker->pots + (poker->potCount - 1), playerIndex, chips
  );
}

int32_t pokerBetGetCurrentCallValue(poker_t *poker) {
  uint8_t i;
  int32_t call;
  call = 0;
  for(i = 0; i < poker->potCount; i++) {
    call = mathMax(call, poker->pots[i].call);
  }
  return call;
}

//eh
int32_t pokerPlayerGetCallBet(poker_t *poker, pokerplayer_t *player) {
  return pokerBetGetCurrentCallValue(poker) - player->currentBet;
}