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

#include "bet.h"

void pokerRoundBetPlayerNext(poker_t *poker) {
  // Go to next player, keep contained.
  poker->bet.better = (poker->bet.better + 1) % POKER_PLAYER_COUNT;

  // Did we go full circle?
  if(poker->bet.better == poker->roundSmallBlind) {
    if(poker->round == POKER_ROUND_BET3) {
      pokerWinnerInit(poker);
      return;
    }

    pokerFlopInit(poker);
    return;
  }

  // Init the next player
  pokerRoundBetPlayerInit(poker, poker->players + poker->bet.better);
}


void pokerRoundBetPlayerInit(poker_t *poker, pokerplayer_t *player) {
  // Check the player state (to see if we even can init, e.g. folded/not)
  if(!pokerPlayerIsAlive(player)) {
    pokerRoundBetPlayerNext(poker);
    return;
  }
  
  printf("Betting round player %u\n", poker->bet.better);
  if(pokerPlayerIsHuman(poker, player)) {
    pokerRoundBetPlayerNext(poker);
    return;
  }

  pokerRoundBetPlayerNext(poker);
}

void pokerRoundBetPlayerUpdate(poker_t *poker, pokerplayer_t *player) {
  
}

void pokerRoundBetInit(poker_t *poker) {
  printf("Betting round start\n");
 
  if(poker->round == POKER_ROUND_DEAL) {
    poker->round = POKER_ROUND_BET0;
    printf("Betting 0\n");
  } else if(poker->round == POKER_ROUND_FLOP) {
    poker->round = POKER_ROUND_BET1;
    printf("Betting 1\n");
  } else if(poker->round == POKER_ROUND_TURN) {
    poker->round = POKER_ROUND_BET2;
    printf("Betting 2\n");
  } else if(poker->round == POKER_ROUND_RIVER) {
    poker->round = POKER_ROUND_BET3;
    printf("Betting 3\n");
  }
  
  // Set the inital player
  poker->bet.better = poker->roundSmallBlind;
  pokerRoundBetPlayerInit(poker, poker->players + poker->bet.better);
}

void pokerRoundBetUpdate(poker_t *poker) {
  // Take the current player
  pokerRoundBetPlayerUpdate(poker, poker->players + poker->bet.better);
}