#include <SPI.h>
#include <EthernetENC.h>
#include <Dns.h>
#include <FastLED.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#define OLED_SDA 21
#define OLED_SCL 22

// Declaration for an SH1106 display connected to I2C (SDA, SCL pins)
Adafruit_SH1106 display(21, 22);

// How many leds in your strip?
#define NUM_LEDS 4 
#define DATA_PIN 2 // pin out esp32
#define BRIGHTNESS 25

// Define the array of leds
CRGB leds[NUM_LEDS];

byte mac[] = {  0x00, 0xC0, 0x18, 0x5A, 0x44, 0x4E}; // A familiar MAC address ;)

char website[] = "example.com"; //
IPAddress TimeServerIP; // Test something besides Google is probably a good idea
IPAddress noIP(0,0,0,0);

// Initialize the Ethernet client library
EthernetClient client;
 
void setup() {
  //initialize led
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);  // GRB ordering is typical
  FastLED.setBrightness(BRIGHTNESS);
  //pinMode(A0, INPUT); //+V Green LED from Ethernet RJ45 jack wired to A0
  
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {;}

  //initialize display
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  
       //configure the CS pin
    Ethernet.init(5);   // MKR ETH Shield

    //verifify ethernet module spi conection
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
          Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
          while (true) {
            delay(1); // do nothing, no point running without Ethernet hardware
          }
        }
    Serial.println("Ethernet shield found");
    delay(1000);
      
  //start testing
  //first test link
if (Ethernet.linkStatus() == LinkOFF) {
    leds[0] = CRGB::Red;
    FastLED.show();
    Serial.println("Ethernet link is DOWN!");
    while (true) {
    delay(1); // do nothing, no point running without UTP cable conected
    }
    }else{
    leds[0] = CRGB::Green;
    FastLED.show();
    Serial.println("Ethernet link is active!");
        
    // testing DHCP
  if (Ethernet.begin(mac) == 0) {
    leds[1] = CRGB::Red;
    FastLED.show();
    Serial.println("Failed to configure Ethernet using DHCP");
  }
  else{
    leds[1] = CRGB::Green;
    FastLED.show();
    
    // print your local IP address:  
    Serial.print("DHCP Success! Local IP: ");
    Serial.println(Ethernet.localIP());
    Serial.println();

    //print IP address to display
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 0);
    // Display static text
    display.print("IP: ");display.println(Ethernet.localIP());
    display.println();
    display.print("Mask: ");display.println(Ethernet.subnetMask());
    display.println();
    display.print("GW: ");display.println(Ethernet.gatewayIP());
    display.println();
    display.print("DNS: ");display.println(Ethernet.dnsServerIP());
    display.display(); 
  
    //testind DNS rezolve
    DNSClient dns;
    dns.begin(Ethernet.dnsServerIP());
    dns.getHostByName("0.pool.ntp.org",TimeServerIP);
   if(TimeServerIP==noIP){
    Serial.println(F("DNS lookup failed")); 
    leds[2] = CRGB::Red;
    FastLED.show(); 
   }
  else {
    Serial.print(F("ntp = "));
    Serial.println(TimeServerIP);
    leds[2] = CRGB::Green;
    FastLED.show();
   }
   
  //Testing Internet
   client.connect(website, 80);
   while (!client.connected()) {
    // if you didn't get a connection to the server:
    Serial.println("Connection to website failed");
    leds[3] = CRGB::Red;
    FastLED.show();
    client.connect(website, 80);
    delay(1000);
   }
    Serial.println("Connected to website");
    leds[3] = CRGB::Green;
    FastLED.show();

    //send to sleep for battery econom
    esp_deep_sleep_start();
  }
 }
}
void loop() {
}
