#include "Characters.h"
#include "Skills.h"
#include <algorithm>

// ===== Warrior 实现 =====

Warrior::Warrior(const std::string& name, int level) 
    : m_rage(0), m_maxRage(100) {
    m_name = name;
    m_className = "Warrior";
    m_level = level;
    
    // 战士属性: 高生命、高防御、低魔法
    m_maxHp = 150 + (level - 1) * 20;
    m_hp = m_maxHp;
    m_maxMp = 50 + (level - 1) * 5;
    m_mp = m_maxMp;
    m_attack = 15 + (level - 1) * 3;
    m_defense = 12 + (level - 1) * 2;
    m_speed = 8 + (level - 1);
    
    // 添加技能
    addSkill(std::make_shared<NormalAttack>());
    addSkill(std::make_shared<HeavyStrike>());
    addSkill(std::make_shared<DefensiveStance>());
}

void Warrior::takeDamage(int damage) {
    // 战士有格挡机制: 减免部分伤害
    int reduction = m_defense / 2;
    int actualDamage = std::max(1, damage - reduction);
    
    m_hp -= actualDamage;
    if (m_hp < 0) {
        m_hp = 0;
    }
    
    // 受到伤害时增加怒气
    addRage(actualDamage / 2);
}

void Warrior::heal(int amount) {
    m_hp += amount;
    if (m_hp > m_maxHp) {
        m_hp = m_maxHp;
    }
}

void Warrior::addRage(int amount) {
    m_rage += amount;
    if (m_rage > m_maxRage) {
        m_rage = m_maxRage;
    }
}

bool Warrior::consumeRage(int amount) {
    if (m_rage >= amount) {
        m_rage -= amount;
        return true;
    }
    return false;
}

// ===== Mage 实现 =====

Mage::Mage(const std::string& name, int level)
    : m_shield(0), m_maxShield(50) {
    m_name = name;
    m_className = "Mage";
    m_level = level;
    
    // 法师属性: 高魔法、高攻击、低生命
    m_maxHp = 100 + (level - 1) * 15;
    m_hp = m_maxHp;
    m_maxMp = 100 + (level - 1) * 10;
    m_mp = m_maxMp;
    m_attack = 20 + (level - 1) * 4;
    m_defense = 5 + (level - 1);
    m_speed = 10 + (level - 1);
    
    // 添加技能
    addSkill(std::make_shared<NormalAttack>());
    addSkill(std::make_shared<Fireball>());
    addSkill(std::make_shared<FrostNova>());
    addSkill(std::make_shared<ArcaneShield>());
}

void Mage::takeDamage(int damage) {
    // 法师有护盾机制: 护盾先承受伤害
    if (m_shield > 0) {
        if (m_shield >= damage) {
            m_shield -= damage;
            return;
        } else {
            damage -= m_shield;
            m_shield = 0;
        }
    }
    
    // 剩余伤害作用于生命值
    m_hp -= damage;
    if (m_hp < 0) {
        m_hp = 0;
    }
}

void Mage::heal(int amount) {
    m_hp += amount;
    if (m_hp > m_maxHp) {
        m_hp = m_maxHp;
    }
}

void Mage::addShield(int amount) {
    m_shield += amount;
    if (m_shield > m_maxShield) {
        m_shield = m_maxShield;
    }
}

void Mage::resetShield() {
    m_shield = 0;
}
