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

#include "player.h"

void pokerPlayerInit(poker_t *poker) {
  uint8_t i;
  pokerplayer_t *player;
  float w, h;

  for(i = 0; i < POKER_PLAYER_COUNT; i++) {
    player = poker->players + i;
    assetTextureLoad(&player->bodyTexture, "characters/penny/body.png");
    assetTextureLoad(&player->faceTexture, "characters/penny/face.png");

    w = 0.6;
    h=((float)player->faceTexture.width)/((float)player->faceTexture.height)*w;
    w = w / 2;
    h = h / 2;
    quadInit(&player->bodyPrimitive, 0,
      -w, -h, 0, 1,
       w,  h, 1, 0
    );
    quadInit(&player->facePrimitive, 0,
      -w, -h, 0, 1,
       w,  h, 1, 0
    );
  }
}

uint8_t pokerPlayerGetSeatForPlayer(uint8_t player) { 
  switch(player) {
    case 0x01:
      return POKER_SEAT_PLAYER1;
    case 0x02:
      return POKER_SEAT_PLAYER2;
    case 0x03:
      return POKER_SEAT_PLAYER3;
    case 0x04:
      return POKER_SEAT_PLAYER4;
    default:
      return POKER_SEAT_PLAYER0;
  }
}

void pokerPlayerRender(poker_t* poker, pokerplayer_t *player, uint8_t seat) {
  float x, z, angle;
  float w, h;

  // Determine position
  angle = POKER_SEAT_ANGLE(seat);
  x = sin(angle) * -1;
  z = cos(angle) * -1;

  shaderUsePosition(&poker->shader,  x,0.34,z,  0,angle,0);

  // Render Body
  shaderUseTexture(&poker->shader, &player->bodyTexture);
  primitiveDraw(&player->bodyPrimitive, 0, -1);

  // Render Face
  shaderUseTexture(&poker->shader, &player->faceTexture);
  primitiveDraw(&player->facePrimitive, 0, -1);
}