// Display Library example for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
// Requires HW SPI and Adafruit_GFX. Caution: the e-paper panels require 3.3V supply AND data lines!
//
// Display Library based on Demo Example from Good Display: http://www.e-paper-display.com/download_list/downloadcategoryid=34&isMode=false.html
//
// Author: Jean-Marc Zingg
// upravil Jan Zerzán
//
// Version: see library.properties
//
// Library: https://github.com/ZinggJM/GxEPD2


#define ENABLE_GxEPD2_GFX 0

#include <GxEPD2_3C.h>
#include <Fonts/FreeSansBold9pt7b.h>
#include <Fonts/FreeSansBold12pt7b.h>
#include <Fonts/FreeSansBold18pt7b.h>
#include <Fonts/FreeSansBold24pt7b.h>

#include "GxEPD2_display_selection.h"

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <WiFiClientSecure.h>
//#include "Bitmaps.h"
#include <ESP8266HTTPClient.h>
#include <Arduino_JSON.h>

ESP8266WebServer server(80);
IPAddress myIP;       // IP address in your local wifi net

//#include "html.h"

int ledB = 0;
//int ledR = 16;
int nap = 0;
int analogPin = A0;
long t_start = 0;
long t_run = 0;
double u;
int Sleep = 16;

char* ssid = "Chillarna";
char* password = "KxenS6hwdjkz";

  
//WiFiClientSecure client;
WiFiClient client;
const char *serverName = "http://192.168.0.241:5000/data";



String dataReadings;
String dataArr[4];

void setup()
{
  pinMode(ledB, OUTPUT);
  digitalWrite(ledB, LOW);
 // pinMode(ledR, OUTPUT);
 // digitalWrite(ledR, LOW);

  pinMode(Sleep, INPUT);
  t_run = 0;
  
  Serial.begin(115200);
  Serial.println();
  Serial.println("--- SETUP ---");

  WiFi.begin(ssid, password);
  Serial.println("Connecting");  
  while(WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  
  Serial.println();
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(myIP = WiFi.localIP());
  
  delay(1000);
  display.init(115200);


  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }
  
  server.on("/data", getData);
  server.on("/push", pushData);
  server.begin();
  Serial.println("HTTP server started");

  Serial.println("setup done");
}

void loop()
{ 

  t_run = millis();
  if(t_run - t_start >= 10000){
    mereni();
    t_start = millis();
  }
  if(t_run  == 600000){
    Serial.println(t_run);
//    digitalWrite(ledB, LOW);
    GoToSleep();
    
  }
  server.handleClient();

  

}
//-------------------------------------------------------------------------------------------*/
String jmeno = "";
String krevskup = "";
String alergie = "";
String stav_pac = "";

void getData() {
  dataReadings = httpGETRequest(serverName);
  Serial.println(dataReadings);
  JSONVar myObject = JSON.parse(dataReadings);
  
  if (JSON.typeof(myObject) == "undefined") {
    Serial.println("Parsing input failed!");
    return;
  }
    
  Serial.print("JSON object = ");
  Serial.println(myObject);
   
 
  JSONVar keys = myObject.keys();
    
  for (int i = 0; i < keys.length(); i++) {
    JSONVar value = myObject[keys[i]];
    Serial.print(keys[i]);
    Serial.print(" = ");
    Serial.println(value);
      dataArr[i] = (value);
  }
  
  Serial.print("1 = ");
  Serial.println(dataArr[0]);
  Serial.print("2 = ");
  Serial.println(dataArr[1]);
  Serial.print("3 = ");
  Serial.println(dataArr[2]);
  Serial.print("4 = ");
  Serial.println(dataArr[3]);
      
  // Don't leave the server hanging
  server.send(200, "text/plain", "" + dataArr[0] + ", " + dataArr[1] + ", " + dataArr[2] + ", " + dataArr[3] + ", " );
}

String httpGETRequest(const char* serverName) {
  HTTPClient http;
    
  // Your IP address with path or Domain name with URL path 
  http.begin(serverName);
  
  // Send HTTP POST request
  int httpResponseCode = http.GET();
  
  String payload = "{}"; 
  
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

  return payload;
}

void pushData(){
  getData();
  Serial.print("push data");
  
  showFont();
  }
//-------------------------------------------------------------------------------*/

void showFont()
{
  display.setFullWindow();
  display.setRotation(0);
  display.setTextColor(GxEPD_BLACK);
  display.setFont(&FreeSansBold18pt7b);
  display.firstPage();
  do
  {
    drawFont();
  }
  while (display.nextPage());
}
void drawFont()
{ 
  //digitalWrite(ledR, HIGH);

  display.fillScreen(GxEPD_WHITE);
  display.setCursor(0, 0);
  
  display.println();
  display.setFont(&FreeSansBold9pt7b);
  display.setTextColor(GxEPD_RED);
  display.println("   Jmeno");
  display.print("   Prijmeni: ");
  display.setFont(&FreeSansBold24pt7b);
  display.setTextColor(GxEPD_BLACK);
  display.println(dataArr[0]);

  display.setFont(&FreeSansBold9pt7b);
  display.println();
  display.setFont(&FreeSansBold18pt7b);
  display.setTextColor(GxEPD_RED);
  display.print("  Krevni skupina: ");
  display.setFont(&FreeSansBold24pt7b);
  display.setTextColor(GxEPD_BLACK);
  display.println(dataArr[1]);
  
  display.setFont(&FreeSansBold18pt7b);
  display.setTextColor(GxEPD_RED);
  display.print("  Alergie: ");
  display.setFont(&FreeSansBold18pt7b);
  display.setTextColor(GxEPD_BLACK);
  display.println(dataArr[2]);

  display.setFont(&FreeSansBold18pt7b);
  display.setTextColor(GxEPD_RED);
  display.print("  Stav pacienta: ");
  display.setFont(&FreeSansBold18pt7b);
  display.setTextColor(GxEPD_BLACK);
  display.println(dataArr[3]);
  // chce to millis asi
  delay(500);
  //digitalWrite(ledR, LOW);
}

void mereni() {
  nap = analogRead(analogPin); 
  
  u = (3.3 + ((3.3 * nap))/1023); 
  Serial.println("Napětí: ");
  Serial.print(u);          
  Serial.println("V");
  if( u < 3.6){
  digitalWrite(ledB, HIGH);
  }
  else
  digitalWrite(ledB, LOW);
}

void GoToSleep() {
  Serial.println("jdu spat");
  ESP.deepSleep(0);
  
}
