#include <TFT_eSPI.h>
#include <WiFiClient.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include "text.h"        //Display digits and units
#include "connection.h"  //Text Images for no-internet and connected
#include "intro.h"       //Text Image for Intro
#include "dog_animation.h" //Dog Animation

const char* ssid = "WIFI_NAME";
const char* password = "WIFI_PASSWORD";
WiFiClientSecure client;
bool is_internet = false;

unsigned long previousMillis = 0;
const long interval = 3000;

TFT_eSPI tft = TFT_eSPI();
TFT_eSprite sprite = TFT_eSprite(&tft);

void dog_animation() {
  while (WiFi.status() != WL_CONNECTED) {
    tft.pushImage(0, 0, 240, 240, dog_000);
    delay(50);
    tft.pushImage(0, 0, 240, 240, dog_001);
    delay(50);
    tft.pushImage(0, 0, 240, 240, dog_002);
    delay(50);
    tft.pushImage(0, 0, 240, 240, dog_001);
    delay(50);
  }
}


int measureInternetSpeed() {

  HTTPClient http;
  client.setInsecure();
  String url = "https://httpbin.org/bytes/10000000";
  http.begin(client, url);  // Begin connection to the URL
  http.addHeader("User-Agent", "ESP32/1.0");
  http.addHeader("Accept", "*/*");
  unsigned long startTime = millis();
  int httpCode = http.GET();
  if (httpCode == HTTP_CODE_OK) {
    WiFiClient* stream = http.getStreamPtr();
    unsigned long endTime = millis();
    unsigned long totalBytes = 10000000;
    float timeTaken = (endTime - startTime) / 1000.0;
    float speed = (totalBytes * 8) / timeTaken;
    int internet_speed = speed / 1000000;
    return internet_speed;
  } else {
    return 400;
  }
  http.end();
}

void setup() {
  tft.begin();
  tft.setSwapBytes(true);
  tft.setRotation(0);
  tft.fillScreen(TFT_BLACK);
  WiFi.begin(ssid, password);
  Serial.begin(115200);

  // Intro
  tft.pushImage(60, 47, 120, 140, intro[0]);
  delay(2000);
  tft.fillScreen(TFT_BLACK);
  dog_animation();
  delay(200);
  tft.fillScreen(TFT_BLACK);
  tft.pushImage(51, 105, 136, 28, connected[0]);
  delay(1000);
  tft.fillScreen(TFT_BLACK);
}


void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    if (WiFi.status() == WL_CONNECTED) {
      int signal_strength = WiFi.RSSI() * (-1);
      previousMillis = currentMillis;
      int net_spd = measureInternetSpeed();
      if (net_spd == 400) {
        tft.pushImage(70, 60, 25, 40, digits[10]);
        tft.pushImage(95, 60, 25, 40, digits[(signal_strength % 100) / 10]);
        tft.pushImage(120, 60, 25, 40, digits[signal_strength % 10]);
        tft.pushImage(147, 78, 40, 27, units[0]);
        tft.pushImage(26, 140, 195, 40, no_internet[0]);
        if (is_internet == false) {
          tft.fillScreen(TFT_BLACK);
          is_internet = true;
        }
      } else {
        // WiFi Strength
        if (is_internet == true) {
          tft.fillScreen(TFT_BLACK);
          is_internet = false;
        }
        tft.pushImage(70, 60, 25, 40, digits[10]);
        tft.pushImage(95, 60, 25, 40, digits[(signal_strength % 100) / 10]);
        tft.pushImage(120, 60, 25, 40, digits[signal_strength % 10]);
        tft.pushImage(147, 78, 40, 27, units[0]);
        // WiFi Speed
        tft.pushImage(95, 129, 25, 40, digits[(net_spd % 100) / 10]);
        tft.pushImage(120, 129, 25, 40, digits[net_spd % 10]);
        tft.pushImage(148, 148, 40, 27, units[1]);
      }
    } else {
      tft.fillScreen(TFT_BLACK);
      dog_animation();
      tft.fillScreen(TFT_BLACK);
    }
  }
}
