#include "game/entities/bullet.hpp"

#include <memory>  // Para unique_ptr, make_unique
#include <string>  // Para basic_string, string

#include "core/resources/resource.hpp"  // Para Resource
#include "utils/param.hpp"              // Para Param, ParamGame, param

// Constructor
Bullet::Bullet(float x, float y, Type type, Color color, int owner)
    : sprite_(std::make_unique<AnimatedSprite>(Resource::get()->getTexture("bullet.png"), Resource::get()->getAnimation("bullet.ani"))),
      type_(type),
      owner_(owner),
      pos_x_(x),
      pos_y_(y) {
    vel_x_ = calculateVelocity(type_);
    sprite_->setCurrentAnimation(buildAnimationString(type_, color));

    collider_.r = WIDTH / 2;
    shiftColliders();
}

// Calcula la velocidad horizontal de la bala basada en su tipo
auto Bullet::calculateVelocity(Type type) -> float {
    switch (type) {
        case Type::LEFT:
            return VEL_X_LEFT;
        case Type::RIGHT:
            return VEL_X_RIGHT;
        default:
            return VEL_X_CENTER;
    }
}

// Construye el string de animación basado en el tipo de bala y color específico
auto Bullet::buildAnimationString(Type type, Color color) -> std::string {
    std::string animation_string;

    // Mapear color a string específico
    switch (color) {
        case Color::YELLOW:
            animation_string = "yellow_";
            break;
        case Color::GREEN:
            animation_string = "green_";
            break;
        case Color::RED:
            animation_string = "red_";
            break;
        case Color::PURPLE:
            animation_string = "purple_";
            break;
    }

    // Añadir dirección
    switch (type) {
        case Type::UP:
            animation_string += "up";
            break;
        case Type::LEFT:
            animation_string += "left";
            break;
        case Type::RIGHT:
            animation_string += "right";
            break;
        default:
            break;
    }

    return animation_string;
}

// Implementación de render
void Bullet::render() {
    if (type_ != Type::NONE) {
        sprite_->render();
    }
}

// Actualiza el estado del objeto
auto Bullet::update(float delta_time) -> MoveStatus {
    sprite_->update(delta_time);
    return move(delta_time);
}

// Implementación del movimiento usando MoveStatus
auto Bullet::move(float delta_time) -> MoveStatus {
    pos_x_ += vel_x_ * delta_time;
    if (pos_x_ < param.game.play_area.rect.x - WIDTH || pos_x_ > param.game.play_area.rect.w) {
        disable();
        return MoveStatus::OUT;
    }

    pos_y_ += VEL_Y * delta_time;
    if (pos_y_ < param.game.play_area.rect.y - HEIGHT) {
        disable();
        return MoveStatus::OUT;
    }

    shiftSprite();
    shiftColliders();

    return MoveStatus::OK;
}

auto Bullet::isEnabled() const -> bool {
    return type_ != Type::NONE;
}

void Bullet::disable() {
    type_ = Type::NONE;
}

auto Bullet::getOwner() const -> int {
    return owner_;
}

auto Bullet::getCollider() -> Circle& {
    return collider_;
}

void Bullet::shiftColliders() {
    collider_.x = pos_x_ + collider_.r;
    collider_.y = pos_y_ + collider_.r;
}

void Bullet::shiftSprite() {
    sprite_->setX(pos_x_);
    sprite_->setY(pos_y_);
}
