#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
#include "KADi_Config.h"
#include <stdio.h>

String messageId; //For storing the last messageId
unsigned char* qrCodeImageBuffer;

void Setup_WIFI() {
  // Applying SSID and password
  WiFi.begin(ssid, password);

  // Waiting the connection to a router
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  // Connection is complete
  Serial.println("");

  Serial.println("WiFi connected");
}

/**
* Sends a new "request_waiter" message
*/
void KADi_API_PostMessage() {
  //Check WiFi connection status
  if (WiFi.status() == WL_CONNECTED) {
    WiFiClient client;
    HTTPClient http;

    String serverPath = serverName + "/sendMessage";

    // Your Domain name with URL path or IP address with path
    http.begin(client, serverPath);

    // If you need an HTTP request with a content type: application/json, use the following:
    http.addHeader("Content-Type", "application/json");
    JSONVar requestBody;
    requestBody["tokenId"] = tokenId;
    requestBody["message"] = "";
    requestBody["messageType"] = "request_waiter";

    int httpResponseCode = http.POST(JSON.stringify(requestBody));

    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);

    if (httpResponseCode > 0) {
      Serial.println(httpResponseCode);  //Print return code
      JSONVar response = JSON.parse(http.getString()); //Get Response
      //Parse message
      messageId = response["messageId"];
      Serial.println(messageId);
    } else {
      Serial.print("Error on sending POST: ");
      Serial.println(httpResponseCode);
    }
    // Free resources
    http.end();
  } else {
    Serial.println("WiFi Disconnected");
  }
}

/**
* Sends retreives qr code
*/
void KADi_API_GetQRCode() {
  //Check WiFi connection status
  if (WiFi.status() == WL_CONNECTED) {
    WiFiClient client;
    HTTPClient http;

    String serverPath = qrCodeUrl;

    // Your Domain name with URL path or IP address with path
    http.begin(serverPath);
  
    int httpResponseCode = http.GET();

    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);

    if (httpResponseCode > 0) {
      Serial.println(httpResponseCode);  //Print return code
      String responseString = http.getString();
      // copy(responseString, responseString.c_str() + responseString.length(), qrCodeImageBuffer); //Get Response
    } else {
      Serial.print("Error on sending POST: ");
      Serial.println(httpResponseCode);
    }
    // Free resources
    http.end();
  } else {
    Serial.println("WiFi Disconnected");
  }
}

/**
* Calls the status of last sent message
* Not used yet, since the API was refactored
*/
bool KADi_API_GetMessageStatus() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;

    String serverPath = serverName + "/getMessageStatus";



    // Your Domain name with URL path or IP address with path
    http.begin(serverPath.c_str());

    // If you need an HTTP request with a content type: application/json, use the following:
    http.addHeader("Content-Type", "application/json");
    JSONVar requestBody;
    requestBody["tokenId"] = tokenId;
    requestBody["messageId"] = messageId;
    int httpResponseCode = http.POST(JSON.stringify(requestBody));
    
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);

    if (httpResponseCode > 0) {
      Serial.println(httpResponseCode);  //Print return code
      JSONVar response = JSON.parse(http.getString()); //Get Response
      //Parse message
      messageId = response["acknowledged"];
      Serial.println(messageId);
    } else {
      Serial.print("Error on sending POST: ");
      Serial.println(httpResponseCode);
    }
    // Free resources
    http.end();
  } else {
    Serial.println("WiFi Disconnected");
  }
}