#include "Skills.h"
#include "Characters.h"
#include <sstream>
#include <random>

// 随机数生成器
static std::random_device rd;
static std::mt19937 gen(rd());

// ===== NormalAttack 实现 =====

NormalAttack::NormalAttack() {
    m_name = "NormalAttack";
    m_description = "Basic attack";
    m_manaCost = 0;
    m_cooldown = 0;
}

std::string NormalAttack::execute(ICharacter& caster, ICharacter& target) {
    int damage = caster.getAttack();
    target.takeDamage(damage);
    
    std::ostringstream oss;
    oss << caster.getName() << " attacks " << target.getName() 
        << " for " << damage << " damage!";
    return oss.str();
}

// ===== HeavyStrike 实现 =====

HeavyStrike::HeavyStrike() {
    m_name = "HeavyStrike";
    m_description = "Powerful strike consuming rage";
    m_manaCost = 10;
    m_cooldown = 0;
}

std::string HeavyStrike::execute(ICharacter& caster, ICharacter& target) {
    Warrior* warrior = dynamic_cast<Warrior*>(&caster);
    if (!warrior || !caster.consumeMana(m_manaCost)) {
        return caster.getName() + " failed to use " + m_name + "!";
    }
    
    // 造成 1.5 倍伤害
    int damage = static_cast<int>(caster.getAttack() * 1.5);
    target.takeDamage(damage);
    
    std::ostringstream oss;
    oss << caster.getName() << " uses [Heavy Strike] on " << target.getName()
        << " for " << damage << " damage!";
    return oss.str();
}

// ===== DefensiveStance 实现 =====

DefensiveStance::DefensiveStance() {
    m_name = "DefensiveStance";
    m_description = "Heal and boost defense";
    m_manaCost = 15;
    m_cooldown = 0;
}

std::string DefensiveStance::execute(ICharacter& caster, ICharacter& target) {
    if (!caster.consumeMana(m_manaCost)) {
        return caster.getName() + " failed to use " + m_name + "!";
    }
    
    int healAmount = 20;
    caster.heal(healAmount);
    
    std::ostringstream oss;
    oss << caster.getName() << " uses [Defensive Stance] and heals " 
        << healAmount << " HP!";
    return oss.str();
}

// ===== Fireball 实现 =====

Fireball::Fireball() {
    m_name = "Fireball";
    m_description = "Launch a fireball";
    m_manaCost = 20;
    m_cooldown = 0;
}

std::string Fireball::execute(ICharacter& caster, ICharacter& target) {
    if (!caster.consumeMana(m_manaCost)) {
        return caster.getName() + " failed to use " + m_name + "!";
    }
    
    // 造成 1.8 倍魔法伤害
    int damage = static_cast<int>(caster.getAttack() * 1.8);
    target.takeDamage(damage);
    
    std::ostringstream oss;
    oss << caster.getName() << " casts [Fireball] at " << target.getName()
        << " for " << damage << " magic damage!";
    return oss.str();
}

// ===== FrostNova 实现 =====

FrostNova::FrostNova() {
    m_name = "FrostNova";
    m_description = "Freeze enemies in an area";
    m_manaCost = 25;
    m_cooldown = 0;
}

std::string FrostNova::execute(ICharacter& caster, ICharacter& target) {
    if (!caster.consumeMana(m_manaCost)) {
        return caster.getName() + " failed to use " + m_name + "!";
    }
    
    int damage = static_cast<int>(caster.getAttack() * 1.3);
    target.takeDamage(damage);
    
    std::ostringstream oss;
    oss << caster.getName() << " casts [Frost Nova] on " << target.getName()
        << " for " << damage << " frost damage!";
    return oss.str();
}

// ===== ArcaneShield 实现 =====

ArcaneShield::ArcaneShield() {
    m_name = "ArcaneShield";
    m_description = "Create a protective shield";
    m_manaCost = 30;
    m_cooldown = 0;
}

std::string ArcaneShield::execute(ICharacter& caster, ICharacter& target) {
    Mage* mage = dynamic_cast<Mage*>(&caster);
    if (!mage || !caster.consumeMana(m_manaCost)) {
        return caster.getName() + " failed to use " + m_name + "!";
    }
    
    int shieldAmount = 30;
    mage->addShield(shieldAmount);
    
    std::ostringstream oss;
    oss << caster.getName() << " creates [Arcane Shield] absorbing " 
        << shieldAmount << " damage!";
    return oss.str();
}

// ===== Heal 实现 =====

Heal::Heal() {
    m_name = "Heal";
    m_description = "Restore health";
    m_manaCost = 15;
    m_cooldown = 0;
}

std::string Heal::execute(ICharacter& caster, ICharacter& target) {
    if (!caster.consumeMana(m_manaCost)) {
        return caster.getName() + " failed to use " + m_name + "!";
    }
    
    int healAmount = 30;
    target.heal(healAmount);
    
    std::ostringstream oss;
    oss << caster.getName() << " casts [Heal] on " << target.getName()
        << " restoring " << healAmount << " HP!";
    return oss.str();
}
