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

#include "pot.h"

void test_pokerPotAdd_should_AddAPot(void) {
  poker_t poker;
  pokerInit(&poker);

  TEST_ASSERT_EQUAL_UINT8(1, poker.potCount);
  TEST_ASSERT_EQUAL_UINT8(1, pokerPotAdd(&poker));
  TEST_ASSERT_EQUAL_UINT8(2, poker.potCount);
}

void test_pokerPotAdd_should_ResetThePot(void) {
  poker_t poker;
  pokerpot_t *pot;
  uint8_t i;
  pokerInit(&poker);

  i = pokerPotAdd(&poker);
  pot = poker.pots + i;
  TEST_ASSERT_EQUAL_UINT8(0, pot->playerCount);
  TEST_ASSERT_EQUAL_INT32(0, pot->chips);
  TEST_ASSERT_EQUAL_INT32(0, pot->call);
}

void test_pokerPotHasPlayer_should_DetermineIfPlayerInThePot(void) {
  poker_t poker;
  pokerpot_t *pot;
  uint8_t p0, p1, p2;

  pokerInit(&poker);
  p0 = pokerPlayerAdd(&poker);
  p1 = pokerPlayerAdd(&poker);
  p2 = pokerPlayerAdd(&poker);

  pot = poker.pots + 0;
  TEST_ASSERT_EQUAL(false, pokerPotHasPlayer(pot, p0));
  TEST_ASSERT_EQUAL(false, pokerPotHasPlayer(pot, p1));
  TEST_ASSERT_EQUAL(false, pokerPotHasPlayer(pot, p2));

  pokerPotAddPlayer(pot, p0);
  TEST_ASSERT_EQUAL(true, pokerPotHasPlayer(pot, p0));
  TEST_ASSERT_EQUAL(false, pokerPotHasPlayer(pot, p1));
  TEST_ASSERT_EQUAL(false, pokerPotHasPlayer(pot, p2));

  pokerPotAddPlayer(pot, p2);
  TEST_ASSERT_EQUAL(true, pokerPotHasPlayer(pot, p0));
  TEST_ASSERT_EQUAL(false, pokerPotHasPlayer(pot, p1));
  TEST_ASSERT_EQUAL(true, pokerPotHasPlayer(pot, p2));

  pokerPotAddPlayer(pot, p1);
  TEST_ASSERT_EQUAL(true, pokerPotHasPlayer(pot, p0));
  TEST_ASSERT_EQUAL(true, pokerPotHasPlayer(pot, p1));
  TEST_ASSERT_EQUAL(true, pokerPotHasPlayer(pot, p2));

  pokerPotAdd(&poker);
  pot = poker.pots + 1;

  pokerPotAddPlayer(pot, p1);
  TEST_ASSERT_EQUAL(false, pokerPotHasPlayer(pot, p0));
  TEST_ASSERT_EQUAL(true, pokerPotHasPlayer(pot, p1));
  TEST_ASSERT_EQUAL(false, pokerPotHasPlayer(pot, p2));
}

void test_pokerPotAddPlayer_should_AddAPlayer(void) {
  poker_t poker;
  pokerpot_t *pot;

  pokerInit(&poker);
  pokerPlayerAdd(&poker);
  pokerPlayerAdd(&poker);

  pot = poker.pots + 0;
  TEST_ASSERT_EQUAL_UINT8(0, pot->playerCount);

  pokerPotAddPlayer(pot, 1);
  TEST_ASSERT_EQUAL_UINT8(1, pot->playerCount);
  TEST_ASSERT_EQUAL_UINT8(0x01, pot->players[0]);

  pokerPotAddPlayer(pot, 0);
  TEST_ASSERT_EQUAL_UINT8(2, pot->playerCount);
  TEST_ASSERT_EQUAL_UINT8(0x00, pot->players[1]);
}

void test_pokerPotGetSumOfChipsForPlayer_should_SumPlayersPotsChips(){
  poker_t poker;
  uint8_t p0i, p1i;
  pokerplayer_t *p0;
  pokerplayer_t *p1;

  pokerInit(&poker);

  p0i = pokerPlayerAdd(&poker);
  p1i = pokerPlayerAdd(&poker);
  p0 = poker.players + p0i;
  p1 = poker.players + p1i;
  pokerPlayerChipsAdd(p0, 10000);
  pokerPlayerChipsAdd(p1, 10000);

  TEST_ASSERT_EQUAL_INT32(0, pokerPotGetSumOfChipsForPlayer(&poker, p0i));
  TEST_ASSERT_EQUAL_INT32(0, pokerPotGetSumOfChipsForPlayer(&poker, p1i));
  
  pokerBetForPlayer(&poker, p0i, 100);
  TEST_ASSERT_EQUAL_INT32(100, pokerPotGetSumOfChipsForPlayer(&poker, p0i));
  TEST_ASSERT_EQUAL_INT32(0, pokerPotGetSumOfChipsForPlayer(&poker, p1i));
  
  pokerBetForPlayer(&poker, p1i, 200);
  TEST_ASSERT_EQUAL_INT32(300, pokerPotGetSumOfChipsForPlayer(&poker, p0i));
  TEST_ASSERT_EQUAL_INT32(300, pokerPotGetSumOfChipsForPlayer(&poker, p1i));
}

int test_pot_h() {
  UNITY_BEGIN();

  RUN_TEST(test_pokerPotAdd_should_AddAPot);
  RUN_TEST(test_pokerPotAdd_should_ResetThePot);
  RUN_TEST(test_pokerPotHasPlayer_should_DetermineIfPlayerInThePot);
  RUN_TEST(test_pokerPotAddPlayer_should_AddAPlayer);
  RUN_TEST(test_pokerPotGetSumOfChipsForPlayer_should_SumPlayersPotsChips);

  return UNITY_END();
}