#include <stdlib.h>
#include <math.h>

#include "../include/boss.h"
#include "../include/const.h"
#include "../include/entity.h"
#include "../include/effect.h"

boss_t *boss_create(pos_t pos, boss_e type) {
  boss_t *boss = malloc(sizeof(boss_t));
  *boss = (boss_t) {
    .pos = pos,
    .type = type,
    .on_ground = false,
    .velocity = 0.0,
    .gravity = 0.0,
    .dir = 1,
    .stunned = false,
    .frozen = false,
    .frozen_timer = 0,
    .fall_height = 0.0,
    .timer_walking = fz_timer_create(9, 8),
    .timer_sneaking = fz_timer_create(9, 16),
  };
  return boss;
}

void boss_update(boss_t *boss, game_t *game) {
  (void) boss;
  (void) game;
}

void boss_draw(boss_t *boss, game_t *game) {
  pos_t pos = pos_snap(boss->pos);
  DrawTextureRec(game->assets.entities, texture_rect_v(10, 6, 2, 1, ENEMY_WIDTH, ENEMY_HEIGHT), pos, WHITE);
}

void boss_kill(boss_t *boss, game_t *game) {
  for (int i = 0; i < game->entities_len; i++) {
    if (game->entities[i].type == ENTITY_BOSS && game->entities[i].boss == boss) {
      if (i + 1 < game->entities_len) {
        for (int j = i; j < game->entities_len - 1; j++) {
          game->entities[j] = game->entities[j + 1];
        }
      }
      game->entities_len--;
      game->entities = realloc(game->entities, sizeof(entity_t) * game->entities_len);
      game->xp += 10;
      break;
    }
  }
}

void boss_free(boss_t *boss) {
  fz_timer_free(boss->timer_walking);
  fz_timer_free(boss->timer_sneaking);
  free(boss);
}
