#include "Arduino.h"
#include <ESP8266WiFi.h>
#include "Gsender.h"
                                  //Docs at: http://learn.linksprite.com/arduino/shields/esp8266-gmail-sender/
#pragma region Globals
const char* ssid = "WIREFREEWWU";                // WIFI network name  ( I think this is correct for the dorm.)
const char* password = "";                       // WIFI network password
uint8_t connection_state = 0;                    // Connected to WIFI or not
uint16_t reconnect_interval = 10000;             // If not connected wait time to try again
#pragma endregion Globals

uint8_t WiFiConnect(const char* nSSID = nullptr, const char* nPassword = nullptr)
{
    static uint16_t attempt = 0;
    Serial.print("Connecting to ");
    if(nSSID) {
        WiFi.begin(nSSID, nPassword);
        Serial.println(nSSID);
    } else {
        WiFi.begin(ssid, password);
        Serial.println(ssid);
    }

    uint8_t i = 0;
    while(WiFi.status()!= WL_CONNECTED && i++ < 50)
    {
        delay(200);
        Serial.print(".");
    }
    ++attempt;
    Serial.println("");
    if(i == 51) {
        Serial.print("Connection: TIMEOUT on attempt: ");
        Serial.println(attempt);
        if(attempt % 2 == 0)
            Serial.println("Check if access point available or SSID and Password\r\n");
        return false;
    }
    Serial.println("Connection: ESTABLISHED");
    Serial.print("Got IP address: ");
    Serial.println(WiFi.localIP());
    return true;
}

void Awaits()
{
    uint32_t ts = millis();
    while(!connection_state)
    {
        delay(50);
        if(millis() > (ts + reconnect_interval) && !connection_state){
            connection_state = WiFiConnect();
            ts = millis();
        }
    }
}

void setup()
{
    Serial.begin(115200);
    pinMode(BUILTIN_LED, OUTPUT);  // initialize onboard LED as output
    Serial.println();
    Serial.print("MAC: ");
    Serial.println(WiFi.macAddress());
    //connection_state = WiFiConnect();  // This is how you log in if you wish to login to WIREFREEWWU
    const char* mac = WiFi.macAddress().c_str();
    connection_state = WiFiConnect("WWU-Aruba-HWauth",mac); // This is how you login when the unit is in KRH, CSP, or Rigby.
    if(!connection_state)  // if not connected to WIFI
        Awaits();          // constantly trying to connect

    Gsender *gsender = Gsender::Instance();    // Getting pointer to class instance
    String subject = "Subject is optional!";
    if(gsender->Subject("Test message!")->Send("firstname.lastname@wallawalla.edu", "Setup testing worked!")) {
        Serial.println("Message send.");
    } else {
        Serial.print("Error sending message: ");
        Serial.println(gsender->getError());
    }
}

void loop(){                        // Blink LED.  Ths LED is useful for debugging.
  digitalWrite(BUILTIN_LED, HIGH);  // turn on LED with voltage HIGH
  Serial.println("ON");
  delay(1000);                      // wait one second
  digitalWrite(BUILTIN_LED, LOW);   // turn off LED with voltage LOW
  Serial.println("OFF");
  delay(1000);                      // wait one second
}
