#include "power_manager.h"
#include "config.h"
#include <WiFi.h>
#include <esp_wifi.h>
#include <esp_bt.h>
#include <esp_sleep.h>

static constexpr float BATTERY_DIVIDER = 2.0f;
static constexpr float ADC_REF_V = 3.3f;

void power_sender_init() {
  setCpuFrequencyMhz(80);
  WiFi.mode(WIFI_OFF);
  esp_wifi_stop();
  esp_wifi_deinit();
  btStop();
  analogReadResolution(12);
  pinMode(PIN_BAT_ADC, INPUT);
}

void power_receiver_init() {
  btStop();
  analogReadResolution(12);
  pinMode(PIN_BAT_ADC, INPUT);
}

void power_configure_unused_pins_sender() {
  // Board-specific: only touch pins that are known unused and safe on TTGO LoRa32 v1.6.1
  const uint8_t pins[] = {32, 33};
  for (uint8_t pin : pins) {
    pinMode(pin, INPUT_PULLDOWN);
  }
}

void read_battery(MeterData &data) {
  uint32_t sum = 0;
  uint16_t samples[5] = {};
  for (uint8_t i = 0; i < 5; ++i) {
    samples[i] = analogRead(PIN_BAT_ADC);
    sum += samples[i];
  }
  float avg = static_cast<float>(sum) / 5.0f;
  float v = (avg / 4095.0f) * ADC_REF_V * BATTERY_DIVIDER * BATTERY_CAL;
  if (SERIAL_DEBUG_MODE) {
    Serial.printf("bat_adc: %u %u %u %u %u avg=%.1f v=%.3f\n",
                  samples[0], samples[1], samples[2], samples[3], samples[4],
                  static_cast<double>(avg), static_cast<double>(v));
  }

  data.battery_voltage_v = v;
  data.battery_percent = battery_percent_from_voltage(v);
}

uint8_t battery_percent_from_voltage(float voltage_v) {
  if (isnan(voltage_v)) {
    return 0;
  }
  struct LutPoint {
    float v;
    uint8_t pct;
  };
  static const LutPoint kCurve[] = {
      {4.20f, 100},
      {4.15f, 95},
      {4.11f, 90},
      {4.08f, 85},
      {4.02f, 80},
      {3.98f, 75},
      {3.95f, 70},
      {3.91f, 60},
      {3.87f, 50},
      {3.85f, 45},
      {3.84f, 40},
      {3.82f, 35},
      {3.80f, 30},
      {3.77f, 25},
      {3.75f, 20},
      {3.73f, 15},
      {3.70f, 10},
      {3.65f, 5},
      {3.60f, 2},
      {2.90f, 0},
  };
  if (voltage_v >= kCurve[0].v) {
    return kCurve[0].pct;
  }
  if (voltage_v <= kCurve[sizeof(kCurve) / sizeof(kCurve[0]) - 1].v) {
    return 0;
  }
  for (size_t i = 0; i + 1 < sizeof(kCurve) / sizeof(kCurve[0]); ++i) {
    const LutPoint &hi = kCurve[i];
    const LutPoint &lo = kCurve[i + 1];
    if (voltage_v <= hi.v && voltage_v >= lo.v) {
      float span = hi.v - lo.v;
      if (span <= 0.0f) {
        return lo.pct;
      }
      float t = (voltage_v - lo.v) / span;
      float pct = lo.pct + t * (hi.pct - lo.pct);
      if (pct < 0.0f) {
        pct = 0.0f;
      }
      if (pct > 100.0f) {
        pct = 100.0f;
      }
      return static_cast<uint8_t>(pct + 0.5f);
    }
  }
  return 0;
}

void light_sleep_ms(uint32_t ms) {
  if (ms == 0) {
    return;
  }
  esp_sleep_enable_timer_wakeup(static_cast<uint64_t>(ms) * 1000ULL);
  esp_light_sleep_start();
}

void go_to_deep_sleep(uint32_t seconds) {
  esp_sleep_enable_timer_wakeup(static_cast<uint64_t>(seconds) * 1000000ULL);
  esp_deep_sleep_start();
}
