#include "units/warrior.h"
#include "units/unit.h"
#include <cassert>

Warrior::Warrior(QString parameters) : Unit(parameters)
{

}

int Warrior::reduceIncomingDamage(std::string damageType, int damage) { //returns damage after reducing by defence
    assert("Incorrect damage type in call reduceIncomingDamage(), expected" &&
        (damageType[0] == 'p' || damageType[0] == 'P' || damageType[0] == 'm' || damageType[0] == 'M'));
    assert("Magic defence of unit is incorrectly high (>40), but must be" && magic_defence_ <= 40);
    assert("Physic defence of unit is incorrectly high (>40), but must be" && physic_defence_ <= 40);
    int near_same = this->theSameNear();


    int active_defence;
    if (damageType[0] == 'p' || damageType[0] == 'P') {
        active_defence = physic_defence_;
    }
    else if (damageType[0] == 'm' || damageType[0] == 'M') {
        active_defence = magic_defence_;
    } else {
        // THIS PART SHOULD BE UNREACHABLE
        active_defence = physic_defence_;
    }

    //return (damage - 2.5 * damage * active_defence / 100);
    int reduced_damage;
    switch (near_same) {
    case 0:
        reduced_damage = damage - 2.5 * double(damage) * double(active_defence) / 100.0;
        break;
    case 1:
        reduced_damage = damage - 2.9 * double(damage) * double(active_defence) / 100.0;
        break;
    case 2:
        reduced_damage = damage - 3.3 * double(damage) * double(active_defence) / 100.0;
        break;
    case 3:
        reduced_damage = damage - 3.7 * double(damage) * double(active_defence) / 100.0;
        break;
    default:
        reduced_damage = damage - 4.1 * double(damage) * double(active_defence) / 100.0;
        break;
    }
    return reduced_damage;
}
