
#include <logger.h>
#include "wifi.hpp"
#include "mqtt.hpp"
#include "config.hpp"
#include "lorahandler.hpp"
#include "display.hpp"
#include <WiFi.h>

Config config;
WiFiServer server(80);
WiFiClient wclient;
Mqtt mqtt(wclient); 
LoraHandler lora; 
Display display;
logging::Logger logger;

#define TRIGGER_PIN 0
void checkButton();

bool portalRunning = false;

void setup()
{
  Serial.begin(115200);

#ifndef DEBUG
  logger.setDebugLevel(logging::LoggerLevel::LOGGER_LEVEL_INFO);
#endif
  pinMode(TRIGGER_PIN, INPUT);

  WiFi.mode(WIFI_STA);
  config = Config();

  while (!config.isConfigLoaded)
  {
    delay(10); 
    logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "MAIN", "COnfig not loaded yet...");
  }
  delay(100); 
  display.setup_display();
  display.show_display("STARTUP");
  setupWifi("island_temp", false);

  mqtt.mqttSetup();
  lora.setup();
  delay(5*1000); // Show text in 5s
  display.cleanTFT();
}

void loop()
{
 ReceivedLoRaPacket packet = lora.receivePacket();
 if (packet.text.isEmpty())
 {
    packet.rssi = 69;
    packet.snr = 3.3;
 }

  if (WiFi.status() == WL_CONNECTED) { // Check WiFi connection
    IPAddress ip = WiFi.localIP();
    String line0 = "IP: " + ip.toString();
    display.show_message(line0.c_str(), 0);
  } else {
    // Optional: Display message indicating WiFi not connected
    display.show_message("WiFi not connected", 0);
  }
  int mqtt_pkt = 123455;
  String line1 = "Mqtt pkt: " +  String(mqtt_pkt);
  display.show_message(line1.c_str(), 1);
  
  String line2 = "Last lora, rssi: " + String(packet.rssi ) ;
  display.show_message(line2.c_str(), 2);
  display.show_message("Fourth", 3);
  display.show_message("Fifth", 4);
  sleep(5);
  /*
  mqtt.mqttRun();
  if (portalRunning)
  {
    wm.process();
  }
  mqtt.mqttPublish("test/sensor", "123");
  checkButton();
  sleep(10);*/
}

void checkButton()
{
  // check for button press
  if (digitalRead(TRIGGER_PIN) == LOW)
  {
    // poor mans debounce/press-hold, code not ideal for production
    delay(50);
    if (digitalRead(TRIGGER_PIN) == LOW)
    {
      logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "Button Pressed once, wait 3s");
      // still holding button for 3000 ms
      delay(3000); // reset delay hold
      if (digitalRead(TRIGGER_PIN) == LOW)
      {
        logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "Button Pressed, Starting Portal");
        wm.startWebPortal();
        portalRunning = true;
      }

      // start portal w delay
      logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "Starting config portal");
      Serial.println("Starting config portal");
      wm.setConfigPortalTimeout(120);

      if (!wm.startConfigPortal("island_temp"))
      {
        logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "failed to connect or hit timeout");
        delay(3000);
        // ESP.restart();
      }
      else
      {
        // if you get here you have connected to the WiFi
        logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "Main", "connected...yeey :)");
      }
    }
  }
}
