//
// Created by IgorBat on 21.04.2018.
//

#include "abstractfactory.h"
#include "spells/spell.h"

#include <iostream>
#include <algorithm>
#include <cassert>
#include <string>

#include <QFile>
#include <QString>
#include <QTextStream>
#include <spellsmanager.h>

Spell::Spell(QString parameters) : QObject(nullptr) {
    QStringList params = parameters.split("\n");
    assert(params.size() >= 1);
    spell_name_ = params[0];
    QString spell_folder = ":/assets/skills/" + spell_name_ + "/";

    loadSpellDescr(spell_folder);
    loadSpellIcon(spell_folder);
    loadSpellTraits(spell_folder);
}

void Spell::loadSpellDescr(QString spell_folder) {
    QFile file(spell_folder + "descr.txt");
    file.open(QIODevice::ReadOnly);
    QTextStream in(&file);
    in.setCodec("UTF-8");
    spell_descr_ = in.readAll();
}

void Spell::loadSpellIcon(QString spell_folder) {
    spell_icon_.load(spell_folder + "icon.png");
}

void Spell::loadSpellTraits(QString spell_folder) {
    QFile file(spell_folder + "traits.txt");
    file.open(QIODevice::ReadOnly);
    QTextStream in(&file);
    in.setCodec("UTF-8");
    QString traits_containts = in.readAll();
    QStringList params = traits_containts.split("\n");
    assert(params.size() >= 1);
    size_t countEffects = params[0].toInt();
    for (size_t i = 1; i <= countEffects; ++i){
        auto list = params[i].split('|');
        auto object_ptr =SpellManager::getInstance().createEffect(list[0]);
        effects_.push_back(object_ptr);
    }
}

QString Spell::getSpellName() const {
    return spell_name_;
}

QString Spell::getSpellDescr() const {
    return spell_descr_;
}

QImage Spell::getSpellIcon() const {
    return spell_icon_;
}

int Spell::getDistance(){
    return distance_;
}
void Spell::setDistance(int value){
    distance_ = value;
}

bool Spell::getForCell(){
    return forCell_;
}
void Spell::setForCell(bool value){
    forCell_ = value;
}

bool Spell::isNeirbor(Cell* destination, Cell* from){
    return from -> getleft() == destination ||
           from -> getleftDown() == destination ||
           from -> getleftUp() == destination ||
           from -> getright() == destination ||
           from -> getrightDown() == destination ||
           from -> getrightUp() == destination;
}
