#include <solar/logger/zeverdata.hpp>
#include <spdlog/spdlog.h>

std::optional<ZeverData> ZeverData::ParseString(const std::string & str)
{
    ZeverData data;
    bool isValid = true;
    std::stringstream ss;
    ss.str(str);

    ss >> data.number0;
    ss >> data.number1;
    ss >> data.registeryID;
    ss >> data.registeryKey;
    ss >> data.hardwareVersion;
    std::string versionInfo;
    ss >> versionInfo;
    size_t splitPos = versionInfo.find('+');
    data.appVersion = versionInfo.substr(0, splitPos);
    ++splitPos;
    data.wifiVersion = versionInfo.substr(splitPos, versionInfo.size());

    ss >> data.timeValue;
    ss >> data.dateValue;
    ss >> data.zeverCloudStatus;
    ss >> data.number3;
    ss >> data.inverterSN;
    isValid = (ss >> data.watt && ss >> data.kilowattPerHour);
    ss >> data.OKmsg;
    ss >> data.ERRORmsg;

    if(!isValid)
    {
        spdlog::error("Error parsing zever data {}", str);
        return {};
    }

    return data;
}

ZeverData::ZeverData() : watt(0), kilowattPerHour(0.0) { }
