#include "character.h"

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>

Character::Character(){
    for(int i = 0; i<6; i++){skills.append(0);}
    for(int i = 0; i<6; i++){skillsMod.append(0);}
    for(int i = 0; i<18; i++){stats.append(0);}
    image = "data/images/logo.png";
}

Character::Character(QFile *file){
    if (!file->open(QIODevice::ReadOnly)) {
        qWarning() << "Could not open file:" << file->errorString();
        return;
    }
    QByteArray data = file->readAll();
    file->close();
    QJsonDocument doc = QJsonDocument::fromJson(data);
    if (doc.isNull()) {
        qWarning() << "Failed to create JSON document";
        return;
    }

    QJsonObject json = doc.object();
    nom = json["name"].toString();
    age = json["age"].toInt();
    race = json["race"].toString();
    classe = json["classe"].toString();
    niveau = json["niveau"].toInt();
    sexe = json["sexe"].toString();
    taille = json["taille"].toDouble();
    vie = json["vie"].toInt();
    image = json["image"].toString();
    texteBlocNote = json["blocNote"].toString();
    texteBackPack = json["backPack"].toString();

    QJsonArray skillArray = json["skills"].toArray();
    for (int i = 0; i < skillArray.size(); ++i) {
        int skillValue = skillArray[i].toInt();
        skills.append(skillValue);
    }
    QJsonArray skillModArray = json["skillsMod"].toArray();
    for (int i = 0; i < skillModArray.size(); ++i) {
        int skillModValue = skillModArray[i].toInt();
        skillsMod.append(skillModValue);
    }
    QJsonArray statArray = json["stats"].toArray();
    for (int i = 0; i < statArray.size(); ++i) {
        int statValue = statArray[i].toInt();
        stats.append(statValue);
    }
}
