// This code has been modified by Torbjorn. Released with MIT License.
// It probably is closer to "mutilated" but who cares...
// Original code was made by Jason Millward.
// Expect a lot of game references!

#include "ESP8266Wifi.h"
//#include "WiFi.h" //For ESP32, only for testing

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);
  digitalWrite(2, HIGH);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  
  Serial.println("Setup done, use Newline mode else things will not work");  
}
void loop() {
  Serial.println("Waiting for command");
  String SerialBuffer = String(Serial.readStringUntil('\n'));
  Serial.setTimeout(5000);
      if (SerialBuffer == "Hello"){
      Serial.println("Hello!");
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "About"){
      digitalWrite(2, LOW);
      Serial.println("This whole thing was made by Torbjorn");
      Serial.println("But he was a bad code poet so he pilaged");
      Serial.println("Look for this project at Hackaday.io");
      Serial.println("If you want to know more about this shitshow");
      delay(500);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Test"){
      Serial.println("OK");
      digitalWrite(2, LOW);
      digitalWrite(12, HIGH);
      digitalWrite(13, HIGH);
      delay(500);
      digitalWrite(2, HIGH);
      digitalWrite(12, LOW);
      digitalWrite(13, LOW);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Portals"){
      digitalWrite(2, LOW);
      Serial.println("I hate portals!");
      digitalWrite(13, HIGH);
      delay(250);
      digitalWrite(13, LOW);
      delay(250);
      digitalWrite(13, HIGH);
      delay(250);
      digitalWrite(13, LOW);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Aard"){
        digitalWrite(2, LOW);
      Serial.println("He makes quite an impact!");
      digitalWrite(13, HIGH);
      delay(1600);
      digitalWrite(13, LOW);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Igni"){
        digitalWrite(2, LOW);
      Serial.println("He is quite a hot-headed person!");
      digitalWrite(12, HIGH);
      delay(1000);
      digitalWrite(12, LOW);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Yrden"){
        digitalWrite(2, LOW);
      Serial.println("When I am with him, the time seems to go so fast. Too fast, even.");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Quen"){
        digitalWrite(2, LOW);
      Serial.println("He can take several beating and still not collapse. How does he do that?");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Axii"){
        digitalWrite(2, LOW);
      Serial.println("He is not known for being loved by animals but he can make them love him!");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Vesemir"){
        digitalWrite(2, LOW);
      Serial.println("He is probably 200 years old... And may die any day.");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Lambert"){
        digitalWrite(2, LOW);
      Serial.println("He hates being a Witcher and he is rude, but he will go to hell and back for his friends.");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Eskel"){
        digitalWrite(2, LOW);
      Serial.println("He probably is as old as Geralt. A very good friend of Geralt too!");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Iorveth"){
        digitalWrite(2, LOW);
      Serial.println("He deserves more love, in my opinion. An elf bloke with some loose ends to tie.");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Dandelion"){
        digitalWrite(2, LOW);
      Serial.println("Ah, the sloppy bard. Still a good friend of Geralt, but could he stop being the princess in Mario games?!");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Letho"){
        digitalWrite(2, LOW);
      Serial.println("Big guy who can and will rip you apart given half the chance and Crowns. Roche really hates him.");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Yennefer"){
        digitalWrite(2, LOW);
      Serial.println("The classic dangerous girlfriend, do not play with her feelings!");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Triss"){
        digitalWrite(2, LOW);
      Serial.println("Wonder what it feels like to get stuck in a love triangle? Play The Witcher 3!");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Avallach"){
        digitalWrite(2, LOW);
      Serial.println("Elf bloke who came out of Uma. I personally want Uma back.");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Dijkstra"){
        digitalWrite(2, LOW);
      Serial.println("Big and dangerous. That's all.");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Roche"){
        digitalWrite(2, LOW);
      Serial.println("A very patriotic puppy-faced person. A good friend of Geralt.");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Zoltan"){
        digitalWrite(2, LOW);
      Serial.println("I love him a lot. He really deserves more love... But he isn't getting any. Like Torbjorn. Or me.");
      Serial.println("Now I am sad.");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Hjalmar"){
        digitalWrite(2, LOW);
      Serial.println("His name is read like YAL-MAR AN CRATE. Very Nordic name, indeed!");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Cleaver"){
        digitalWrite(2, LOW);
      Serial.println("Dwarf who can tenderize your face in 10 seconds flat. Dangerous and small!");
      Serial.println("He is also a racist and to spoil his fate a bit, he later gets caught and burnt to death by witch hunters.");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Baron"){
        digitalWrite(2, LOW);
      Serial.println("What a big lad. An abusive lover too.");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Radovid"){
        digitalWrite(2, LOW);
      Serial.println("He is quite crazy about politics. A bit too much.");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Geralt"){
        digitalWrite(2, LOW);
      Serial.println("Ah, our snarky lovable mutant. He is pretty cool.");
      Serial.println("He probably is the closest in conforming to modern standards");
      Serial.println("in terms of sanitary and fashion department among Witchers.");
      Serial.println("Apart from Vesemir and Lambert he is the only one with least");
      Serial.println("amount of facial damages. Also I wonder how Witchers would look");
      Serial.println("in modern clothes. Hmmm...");
      Serial.println("Anyway, he is one of my favorite character and he has many friends.");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else if (SerialBuffer == "Credits"){
        digitalWrite(2, LOW);
      Serial.println(" ##############                    ");
      Serial.println("#              #     -  CREDITS -  ");
      Serial.println("#  ##      ##  #                   ");
      Serial.println("#  ##      ##  #                   ");
      Serial.println("# #          # #                   ");
      Serial.println("#  ##########  #       #TheWitcher ");
      Serial.println(" ##############            #CP2077 ");
      Serial.println("                                   ");
      Serial.println("          Shoutout goes to         ");
      Serial.println("     Hackaday     | Jason Millward ");
      Serial.println("ESP8266 Community | Clive Mitchell ");
      Serial.println("      DEFCON      |    FizzOtter   ");
      Serial.println("    RAZOR1911     | CD PROJEKT RED ");
      Serial.println("      GitHub      |    Espressif   ");
      Serial.println("Arduino Community |    DIY Tech    ");
      Serial.println("      JLCPCB      |  Julian Illet  ");
      Serial.println("    GreatScott!   |     And...     ");
      Serial.println("  Thank you to everyone who have   ");
      Serial.println("  helped this project become real! ");
      Serial.println("Especially CDPR - Love your games! ");
      Serial.println("  Please do not ruin CP2077! xoxo  ");
      Serial.println("    Love, hugs and kisses from     ");
      Serial.println("       Torbjorn - South Korea      ");
      Serial.println("                                   ");
      Serial.println("   ==== END OF TRANSMISSION ====   ");
      delay(100);
      digitalWrite(2, HIGH);
      SerialBuffer = "0000";
      }
      else{
        digitalWrite(2, LOW);
        delay(100);
      digitalWrite(2, HIGH);
      delay(100);
      Serial.println("Probably not a command, here is the buffer: ");
      Serial.println(SerialBuffer);
      SerialBuffer = "0000";
      digitalWrite(2, LOW);
      delay(100);
      digitalWrite(2, HIGH);
      delay(100);
      digitalWrite(2, LOW);
      delay(100);
      digitalWrite(2, HIGH);
  Serial.println("Scan start");  
  bool foundOpen = false;  
  int networks   = WiFi.scanNetworks();
  Serial.println("Scan done");
  digitalWrite(2, LOW);
   delay(100);
      digitalWrite(2, HIGH);
    if (networks != 0) {    
    for (int i = 0; i < networks; ++i) {
      //if ( WiFi.encryptionType(i) == WIFI_AUTH_OPEN ) { //USE THIS FOR ESP32
        if ( WiFi.encryptionType(i) == ENC_TYPE_NONE ) {  //USE THIS FOR ESP8266
        delay(100);
            digitalWrite(2, LOW);
            Serial.print("Found open network: ");
            Serial.println(WiFi.SSID(i));
            delay(100);
            digitalWrite(2, HIGH);
        foundOpen = true;
      }      
    }
  }
  if (foundOpen) {
    eyeGlow();
  }
  Serial.println("End of report");
    }
    digitalWrite(2, LOW);
    delay(100);
    digitalWrite(2, HIGH);
    delay(100); 
    digitalWrite(2, LOW);
    delay(100);
    digitalWrite(2, HIGH);
    delay(100); 
    digitalWrite(2, LOW);
    delay(100);
    digitalWrite(2, HIGH);
    delay(100); 
    digitalWrite(2, LOW);
    delay(100);
    digitalWrite(2, HIGH);
    delay(100); 
    digitalWrite(2, LOW);
    delay(100);
    digitalWrite(2, HIGH);
    delay(100); 
  delay(1000 * 10);
  //ESP.deepSleep(30000000, WAKE_RF_DEFAULT);
 
}
void eyeGlow() {
  for (int i = 0; i <= 5; i++) {     
    for (int i = 0; i <= 1000; i++) { 
      digitalWrite(12, HIGH);
      digitalWrite(13, HIGH);
      delayMicroseconds(i); 
      digitalWrite(12, LOW);
      digitalWrite(13, LOW);
      delayMicroseconds(1000 - i);
    }
    for (int i = 1000; i >= 0; i--) { 
      digitalWrite(12, HIGH);
      digitalWrite(13, HIGH);
      delayMicroseconds(i); 
      digitalWrite(12, LOW);
      digitalWrite(13, LOW);
      delayMicroseconds(1000 - i); 
    }
  }
}

