#include <Time.h>
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //I left the MAC address and IP address blank.
byte ip[] = {
  192,168,1,177 };                     // You will want to fill these in with your MAC and IP address.

EthernetServer server(80); // Assigning the port forwarded number. It's almost always 80.

String readString; // We will be using strings to keep track of things.

int LivingRoomLight = 9;

//////////////////////

#define W5200_CS  10
#define SDCARD_CS 4

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(SDCARD_CS,OUTPUT);
  digitalWrite(SDCARD_CS,HIGH);//Deselect the SD card
  // start the Ethernet connection and the server:
  pinMode(LivingRoomLight, OUTPUT);
  Ethernet.begin(mac, ip);
  Serial.println("Ethernet Begun");
  digitalWrite(LivingRoomLight, LOW); 
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {                     // This is all where we start up the server and strings.
        char c = client.read();                     
        if (readString.length() < 100) {
          readString += c;
        } 
        if (c == '\n') {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          Serial.println(readString);          // And here we begin including the HTML
          client.println();
          client.println(F(
            "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'>"
            "<html>"
            "<head>"
            "<meta name='viewport' content='width=device-width, initial-scale=1.0'>"
            "<title>"
            "Blajda Home Automation"
            "</title>"
            "<link rel='stylesheet' type='text/css' href='http://www.ryanblajda.com/wp-content/uploads/2014/09/webserver.css'/>"
            "</head>"
            "<body>"
            "<center>"
            "<div class='accordion'>"
            "<div id='tab-0'>"
            "<a href='#tab-0' class='tab'>Home</a>"
            "<div class='content'>"
            "Today is: "
            "<br></br>"));
            client.println(F("Temperature: " /*client.println(analogRead(A0));*/ "&deg;F"));
            
            client.println(F(
            "</div><!--content-->"
            "</div><!--tab0-->"
            "<div id='tab-1'>"
            "<a href='#tab-1' class='tab'>Lighting</a>"
            "<div class='content'>"
            "Living Room 1 <input class='left' type='submit' value='Off' onClick=window.location='/?livingoff1#tab-1\'><input class='right' type='submit' value='On' onClick=window.location='/?livingon1#tab-1\'>"
            "<br></br>"
            /// "Relay 7 <input class='left' type='submit' value='Off' onClick=window.location='/?relay7off#tab-1\'><input class='right' type='submit' value='On' onClick=window.location='/?relay7on#tab-1\'>"
            /// "Relay 6 <input class='left' type='submit' value='Off' onClick=window.location='/?relay6off#tab-1\'><input class='right' type='submit' value='On' onClick=window.location='/?relay6on#tab-1\'>"
            /// "Relay 5 <input class='left' type='submit' value='Off' onClick=window.location='/?relay5off#tab-1\'><input class='right' type='submit' value='On' onClick=window.location='/?relay5on#tab-1\'>"
            /// "Relay 4 <input class='left' type='submit' value='Off' onClick=window.location='/?relay4off#tab-1\'><input class='right' type='submit' value='On' onClick=window.location='/?relay4on#tab-1\'>"
            /// "Relay 3 <input class='left' type='submit' value='Off' onClick=window.location='/?relay3off#tab-1\'><input class='right' type='submit' value='On' onClick=window.location='/?relay3on#tab-1\'>"
            /// "Relay 2 <input class='left' type='submit' value='Off' onClick=window.location='/?relay2off#tab-1\'><input class='right' type='submit' value='On' onClick=window.location='/?relay2on#tab-1\'>"
            /// "Relay 1 <input class='left' type='submit' value='Off' onClick=window.location='/?relay1off#tab-1\'><input class='right' type='submit' value='On' onClick=window.location='/?relay1on#tab-1\'>"
            "</div><!--content-->"
            "</div><!--tab1-->"
            "<div id='tab-2'>"
            "<a href='#tab-2' class='tab'>IR Control</a>"
            "<div class='content'>"
            "Projector"
            "<br></br>"
            "<input class='left' type='submit' value='Off' onClick=window.location='/?projectoroff#tab-2\'><input class='right' type='submit' value='On' onClick=window.location='/?projectoron#tab-2\'>"
            "<br></br>"
            "Apple TV"
            "<br></br>"
            "<input class='center' type='submit' value='Up' onClick=window.location='/?projectorup#tab-2\'>"
            "<br></br>"
            "<input class='left' type='submit' value='Left' onClick=window.location='/?projectorleft#tab-2\'><input class='middle' type='submit' value='Select' onClick=window.location='/?projectorselect#tab-2\'><input class='right' type='submit' value='Right' onClick=window.location='/?projectorright#tab-2\'>"
            "<br></br>"
            "<input class='center' type='submit' value='Down' onClick=window.location='/?projectordown#tab-2\'>"
            "</div><!--content-->"
            "</div><!--tab2-->"
            "</div><!--accordion-->"
            "</center>"
            "</body>"
            "</html>"));

////LIGHTING SECTION

////LIVING ROOM LIGHT
          if (readString.indexOf("?livingon1") > 0) {
            digitalWrite(9, LOW);
            Serial.println("On");
          }

          else {
            if (readString.indexOf("?livingoff1") > 0) {
              digitalWrite(9, HIGH);
              Serial.println("Off");
            }
          }

/** UNNEEDED RELAY FUNCTIONS **/

/** RELAY SEVEN   
          if (readString.indexOf("?relay7on") > 0) {
            digitalWrite(8, LOW);
            Serial.println("On");
          }

          else {
            if (readString.indexOf("?relay7off") > 0) {
              digitalWrite(8, HIGH);
              Serial.println("Off");
            }
          }
          
////RELAY SIX          
          if (readString.indexOf("?relay6on") > 0) {
            digitalWrite(7, LOW);
            Serial.println("On");
          }

          else {
            if (readString.indexOf("?relay6off") > 0) {
              digitalWrite(7, HIGH);
              Serial.println("Off");
            }
          }

////RELAY FIVE          
          if (readString.indexOf("?relay5on") > 0) {
            digitalWrite(6, LOW);
            Serial.println("On");
          }

          else {
            if (readString.indexOf("?relay5off") > 0) {
              digitalWrite(6, HIGH);
              Serial.println("Off");
            }
          }

////RELAY FOUR          
          if (readString.indexOf("?relay4on") > 0) {
            digitalWrite(5, LOW);
            Serial.println("On");
          }

          else {
            if (readString.indexOf("?relay4off") > 0) {
              digitalWrite(5, HIGH);
              Serial.println("Off");
            }
          }

////RELAY THREE         
          if (readString.indexOf("?relay3on") > 0) {
            digitalWrite(4, HIGH);
            Serial.println("On");
          }

          else {
            if (readString.indexOf("?relay3off") > 0) {
              digitalWrite(4, LOW);
              Serial.println("Off");
            }
          }

////RELAY TWO          
          if (readString.indexOf("?relay2on") > 0) {
            digitalWrite(5, LOW);
            Serial.println("On");
          }

          else {
            if (readString.indexOf("?relay2off") > 0) {
              digitalWrite(5, HIGH);
              Serial.println("Off");
            }
          }

////RELAY ONE         
          if (readString.indexOf("?relay1on") > 0) {
            digitalWrite(4, HIGH);
            Serial.println("On");
          }

          else {
            if (readString.indexOf("?relay1off") > 0) {
              digitalWrite(4, LOW);
              Serial.println("Off");
            }
          }
**/

/*** UNNEEDED IR SECTION TO BE IMPLEMENTED SOON ***/

////IR SECTION

/** PROJECTOR
          if (readString.indexOf("?projectoroff") > 0) {
            //run ProjectorOn
            Serial.println("Projector On");
          }

          else {
            if (readString.indexOf("?projectoron") > 0) {
              //run ProjectorOff
              Serial.println("Projector Off");
            }
          }
**/
          delay(1);

          readString="";
          client.stop();            // End of session.

        }
      }
    }
  }

}