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

#include "round.h"

pokeraction_t actionRound() {
  return (pokeraction_t){
    .init = &actionRoundInit,
    .update = &actionRoundUpdate,
    .dispose = &actionRoundDispose
  };
}

void actionRoundInit(int32_t index, void *data) {
  uint8_t i;
  pokerplayer_t *player;

  logText("Round Start");

  // Look at the dealer.
  holdemRenderLookHand(&GAME_STATE.cameraLeft, HOLDEM_GAME_SEAT_PLAYER0);

  // Init the round and shuffle the deck
  cardDeckFill(GAME_STATE.deck);
  GAME_STATE.deckSize = CARD_DECK_SIZE;
  GAME_STATE.pot = 0;
  GAME_STATE.cardsFacing = 0;
  
  // Reset the players
  for(i = 0; i < POKER_PLAYER_COUNT; i++) {
    player = GAME_STATE.players + i;

    // Clear Round State(s)
    player->state &= ~(
      POKER_PLAYER_STATE_FOLDED | 
      POKER_PLAYER_STATE_SHOWING
    );

    player->cardCount = 0;
    player->currentBet = 0;

    player->chips = 1000;
    player->currentBet = 1000;
  }

  // Next action
  pokerActionRemove(index);
}

void actionRoundUpdate(int32_t index, void *data) {
}

void actionRoundAfterShuffle() {
  logText("Shuffle Done");
  int32_t i = pokerActionAdd(actionDeal());
}

void actionRoundDispose(int32_t index, void *data) {
  int32_t newI = pokerActionAdd(actionShuffle());
  shuffledata_t *newData=(shuffledata_t *)(GAME_STATE.actionData + newI);
  newData->done = &actionRoundAfterShuffle;
}