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

#include "bet.h"

void _pokerGameActionBetOnStart(
  queue_t *queue, queueaction_t *action, uint8_t i
) {
  bool isHuman;
  pokergame_t *game = (pokergame_t *)action->data;

  // Reset the UI state.
  isHuman = game->poker.better == POKER_WORLD_HUMAN_INDEX;
  // if(isHuman) pokerUiBetShow(&game->ui);
}

void _pokerGameActionBetOnUpdate(
  queue_t *queue, queueaction_t *action, uint8_t i
) {
  // Restack
  bool isHuman;
  bool turnMade = false;
  pokerturn_t turn;
  pokergame_t *game = (pokergame_t *)action->data;
  pokerdiscussiondata_t discussion;
  
  // Are they human?
  isHuman = game->poker.better == POKER_WORLD_HUMAN_INDEX;

  // Handle as an AI
  if(isHuman) {
    // turn = game->ui.betTurn;
    // turnMade = game->ui.betTurnMade;
    turn = pokerTurnGetForPlayer(&game->poker, game->poker.better);
    turnMade = true;
  } else {
    turn = pokerTurnGetForPlayer(&game->poker, game->poker.better);
    turnMade = true;
  }

  // Now decide if we should do something.
  if(!turnMade) return;

  // Perform the action
  pokerTurnAction(&game->poker, game->poker.better, turn);

  // Speak
  discussion.reason = pokerDiscussionGetTypeFromTurnType(turn.type);
  discussion.poker = game;
  discussion.playerCause = game->poker.better;
  pokerDiscussionQueue(&discussion);

  // Next.
  queueNext(queue);
}

void _pokerGameActionBetOnEnd(
  queue_t *queue, queueaction_t *action, uint8_t i
) {
  pokergame_t *game = (pokergame_t *)action->data;

  // // Get which player is remaining to move.
  game->poker.better = pokerBetGetNextPlayer(&game->poker);

  // Restack
  pokerGameActionRestackAdd(game);

  // Are we waiting on any players?
  if(game->poker.better != 0xFF) {
    pokerGameActionLookAdd(game, game->poker.better);
    pokerGameActionBetAdd(game);
    return;
  }

  // Not waiting, do next action.
  pokerGameActionFlopAdd(game);
}

queueaction_t * pokerGameActionBetAdd(pokergame_t *game) {
  queueaction_t *action = pokerGameActionAdd(game);
  action->onStart = &_pokerGameActionBetOnStart;
  action->onUpdate = &_pokerGameActionBetOnUpdate;
  action->onEnd = &_pokerGameActionBetOnEnd;
  return action;
}