#include "fgc/ui/UiSnapshot.h"

#include <cmath>
#include <cstdio>

namespace fgc {

const char* axisStateLabel(AxisState s) {
    switch (s) {
        case AxisState::Boot:   return "BOOT";
        case AxisState::Reset:  return "RESET";
        case AxisState::Homing: return "HOMING";
        case AxisState::Ready:  return "READY";
        case AxisState::Error:  return "ERROR";
        case AxisState::Unknown:
        default:                return "----";
    }
}

UiColor axisStateColor(AxisState s) {
    switch (s) {
        case AxisState::Ready:  return UiColor::Green;
        case AxisState::Homing: return UiColor::Yellow;
        case AxisState::Error:  return UiColor::Red;
        case AxisState::Boot:
        case AxisState::Reset:  return UiColor::Dim;
        case AxisState::Unknown:
        default:                return UiColor::Default;
    }
}

std::string formatDegrees(double deg) {
    char buf[32];
    std::snprintf(buf, sizeof(buf), "%.1f\xC2\xB0", deg);  // UTF-8 degree sign
    return buf;
}

std::string formatTimeAgo(long long now_ms, long long then_ms) {
    if (then_ms <= 0) return "\xE2\x80\x94";  // em dash: never
    long long d = now_ms - then_ms;
    if (d < 0) d = 0;
    long long s = d / 1000;
    char buf[32];
    if (s < 60)        std::snprintf(buf, sizeof(buf), "%llds ago", s);
    else if (s < 3600) std::snprintf(buf, sizeof(buf), "%lldm ago", s / 60);
    else               std::snprintf(buf, sizeof(buf), "%lldh ago", s / 3600);
    return buf;
}

SensorsView pendingSensorsView() {
    SensorsView v;
    // Xsens MTi-28 AHRS: drift-free Euler orientation + device internal temp.
    v.imu.title  = "MTi (orientation)";
    v.imu.status = "pending";
    v.imu.fields.push_back({"Roll",  "--.-", "\xC2\xB0", false});
    v.imu.fields.push_back({"Pitch", "--.-", "\xC2\xB0", false});
    v.imu.fields.push_back({"Yaw",   "--.-", "\xC2\xB0", false});
    v.imu.fields.push_back({"Temp",  "--.-", "\xC2\xB0""C", false});  // MTi internal
    // DHT11 temperature & humidity (Aosong): 0-50 °C ±2 °C, 20-90 %RH ±5 % (ambient).
    v.dht.title  = "DHT11 (ambient)";
    v.dht.status = "pending";
    v.dht.fields.push_back({"Temp",  "--.-", "\xC2\xB0""C", false});
    v.dht.fields.push_back({"Humid", "--",   "%RH",         false});
    return v;
}

}  // namespace fgc
