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

#include "pokerdiscussion.h"

void pokerDiscussionGet(
  pokerdiscussion_t *discussion, pokerdiscussiondata_t *data
) {
  discussion->count = 0;
  memset(discussion->players, 0xFF,
    sizeof(uint8_t) * POKER_DISCUSSION_MESSAGE_COUNT_MAX
  );

  switch(data->reason) {
    // Match Start Conversations
    case POKER_DISCUSSION_REASON_MATCH_START:
      discussion->count++;
      discussion->messages[0] = "Match Start";
      discussion->players[0] = POKER_WORLD_DEALER_INDEX;
      discussion->emotions[0] = VN_CHARACTER_EMOTION_BORED;
      break;
    
    // Round Start Conversations
    case POKER_DISCUSSION_REASON_ROUND_START:
      discussion->count++;
      discussion->messages[0] = "Round Start";
      discussion->players[0] = POKER_WORLD_DEALER_INDEX;
      discussion->emotions[0] = VN_CHARACTER_EMOTION_BORED;
      break;
    
    // Round Start Conversations
    case POKER_DISCUSSION_REASON_BLINDS_TAKEN:
      discussion->count++;
      discussion->messages[0] = "Blinds\nhave been taken.";
      discussion->players[0] = POKER_WORLD_DEALER_INDEX;
      discussion->emotions[0] = VN_CHARACTER_EMOTION_BORED;
      break;

    // Deal 
    case POKER_DISCUSSION_REASON_DEAL:
      discussion->count++;
      discussion->messages[0] = "Cards dealt.";
      discussion->players[0] = POKER_WORLD_DEALER_INDEX;
      discussion->emotions[0] = VN_CHARACTER_EMOTION_BORED;
      break;
    
    // Betting conversations
    case POKER_DISCUSSION_REASON_PLAYER_FOLDING:
      discussion->count++;
      discussion->messages[0] = "I fold.";
      discussion->players[0] = data->playerCause;
      discussion->emotions[0] = VN_CHARACTER_EMOTION_ANGRY;
      break;
    
    case POKER_DISCUSSION_REASON_PLAYER_CHECKING:
      discussion->count++;
      discussion->messages[0] = "I check.";
      discussion->players[0] = data->playerCause;
      discussion->emotions[0] = VN_CHARACTER_EMOTION_ANGRY;
      break;

    case POKER_DISCUSSION_REASON_PLAYER_CALLING:
      discussion->count++;
      discussion->messages[0] = "I call.";
      discussion->players[0] = data->playerCause;
      discussion->emotions[0] = VN_CHARACTER_EMOTION_SMUG;
      break;
    
    case POKER_DISCUSSION_REASON_PLAYER_RAISING:
      discussion->count++;
      discussion->messages[0] = "I raise.";
      discussion->players[0] = data->playerCause;
      discussion->emotions[0] = VN_CHARACTER_EMOTION_BOASTFUL;
      break;
    
    case POKER_DISCUSSION_REASON_PLAYER_ALL_IN:
      discussion->count++;
      discussion->messages[0] = "All in.";
      discussion->players[0] = data->playerCause;
      discussion->emotions[0] = VN_CHARACTER_EMOTION_ANGRY_PROUD;
      break;
    
    // Flops
    case POKER_DISCUSSION_REASON_FLOP:
      discussion->count++;
      discussion->messages[0] = "Doing the flop.";
      discussion->players[0] = POKER_WORLD_DEALER_INDEX;
      discussion->emotions[0] = VN_CHARACTER_EMOTION_ANIME_MOM;
      break;

    case POKER_DISCUSSION_REASON_BETTING_DONE:
      discussion->count++;
      discussion->messages[0] = "Winner Winner Chicken Dinner";
      discussion->players[0] = POKER_WORLD_DEALER_INDEX;
      discussion->emotions[0] = VN_CHARACTER_EMOTION_SMUG_VERY;
      break;

    // Fallback
    default:
      discussion->count++;
      discussion->messages[0] = "Hmm, this seems to be an error message.";
      discussion->players[0] = POKER_WORLD_DEALER_INDEX;
      discussion->emotions[0] = VN_CHARACTER_EMOTION_CONCERNED_WORRIED;
      break;
  }
}

uint8_t pokerDiscussionGetTypeFromTurnType(uint8_t winningType) {
  switch(winningType) {
    case POKER_TURN_TYPE_BET:
      return POKER_DISCUSSION_REASON_PLAYER_RAISING;

    case POKER_TURN_TYPE_CALL:
      return POKER_DISCUSSION_REASON_PLAYER_CALLING;

    case POKER_TURN_TYPE_ALL_IN:
      return POKER_DISCUSSION_REASON_PLAYER_ALL_IN;
    
    case POKER_TURN_TYPE_FOLD:
      return POKER_DISCUSSION_REASON_PLAYER_FOLDING;
      break;

    case POKER_TURN_TYPE_CHECK:
      return POKER_DISCUSSION_REASON_PLAYER_CHECKING;
    
    default:
      return POKER_DISCUSSION_REASON_TEST;
  }
}

void pokerDiscussionQueue(pokerdiscussiondata_t *data) {
  pokerdiscussion_t discussion;
  uint8_t i, player;

  // Fetch the discussion item for the given data.
  pokerDiscussionGet(&discussion, data);

  // Now for each discussion item, queue a task.
  for(i = 0; i < discussion.count; i++) {
    player = discussion.players[i];
    
    pokerGameActionLookAdd(data->poker, player);
    vnConversationTalk(&data->poker->scene.conversation,
      discussion.messages[i],
      data->poker->scene.characters + player,
      discussion.emotions[i]
    );
  }
}