// SPDX-License-Identifier: Apache-2.0
// Copyright Pionix GmbH and Contributors to EVerest

#include "state.hpp"
#include <chrono>
#include <cstdint>
#include <nlohmann/json_fwd.hpp>
#include <string>

namespace module::state {
using std::chrono::milliseconds;
using std::chrono::system_clock;
using std::chrono::time_point_cast;

TimeStamp::TimeStamp() :
    time_stamp{time_point_cast<milliseconds>(system_clock::now()).time_since_epoch().count() / milliseconds_in_second} {
}

TimeStamp& TimeStamp::operator=(const double value) {
    time_stamp = static_cast<int64_t>(value);
    return *this;
}

TimeStamp::operator int64_t() const {
    return time_stamp;
}

std::string state_to_string(const ModuleState& module_state) {
    using state::State;

    const auto pwm = module_state.pwm_running ? '2' : '1';

    switch (module_state.current_state) {
    case State::STATE_DISABLED:
        return "Disabled";
    case State::STATE_A:
        return "A" + std::to_string(pwm);
    case State::STATE_B:
        return "B" + std::to_string(pwm);
    case State::STATE_C:
        return "C" + std::to_string(pwm);
    case State::STATE_D:
        return "D" + std::to_string(pwm);
    case State::STATE_E:
        return "E" + std::to_string(pwm);
    case State::STATE_F:
        return "F" + std::to_string(pwm);
    default:
        return "";
    }
}

void to_json(nlohmann::json& json, const PowermeterData& powermeter_data) {
    json = nlohmann::json{{"time_stamp", static_cast<uint64_t>(powermeter_data.time_stamp)},
                          {"total_importWattHr", powermeter_data.import_totalWattHr},
                          {"total_exportWattHr", powermeter_data.export_totalWattHr},
                          {"wattL1", powermeter_data.wattL1},
                          {"vrmsL1", powermeter_data.vrmsL1},
                          {"irmsL1", powermeter_data.irmsL1},
                          {"import_wattHrL1", powermeter_data.import_wattHrL1},
                          {"export_wattHrL1", powermeter_data.export_wattHrL1},
                          {"tempL1", powermeter_data.tempL1},
                          {"freqL1", powermeter_data.freqL1},

                          {"wattL2", powermeter_data.wattL2},
                          {"vrmsL2", powermeter_data.vrmsL2},
                          {"irmsL2", powermeter_data.irmsL2},
                          {"import_wattHrL2", powermeter_data.import_wattHrL2},
                          {"export_wattHrL2", powermeter_data.export_wattHrL2},
                          {"tempL2", powermeter_data.tempL2},
                          {"freqL2", powermeter_data.freqL2},

                          {"wattL3", powermeter_data.wattL3},
                          {"vrmsL3", powermeter_data.vrmsL3},
                          {"irmsL3", powermeter_data.irmsL3},
                          {"import_wattHrL3", powermeter_data.import_wattHrL3},
                          {"export_wattHrL3", powermeter_data.export_wattHrL3},
                          {"tempL3", powermeter_data.tempL3},
                          {"freqL3", powermeter_data.freqL3},

                          {"irmsN", powermeter_data.irmsN}};
}

} // namespace module::state
