//This is the main file for the web writing functions
// Programed by Joshua Umholtz-Judson
#include 
#include 
#include "WebProto.h"

//Include external sensor values
extern float TBRad[];
extern float TARad[];
extern float flow;
extern EthernetClient kly;

//Write a specific web page or text as defined by the input value by select case
// NOTE: web page names seem to need to be 8 char or less
void WebProto::wriWebPages(EthernetClient wryKly, int webPath){
    //First lets do the usual stuff that is the same for all of them
    //Get a file primed and tell client to expect a file
    File tsFile;
    wryKly.println("HTTP/1.1 200 OK");
    wryKly.println("Content-Type: text/html");
    wryKly.println("Connection: close");
    wryKly.println();
    switch(webPath){
        case 1: // Write Main Page
            //Serial.println("Wrote Main Page");
            tsFile = SD.open("Main136.htm");            
        break;
    
        case 2: // Write web page for html code
            tsFile = SD.open("WebCode1.htm");
            //Serial.println("Wrote WebCode Page");  
        break;

        case 3: // Write HTML code into page
            tsFile = SD.open("WebFil.txt");
            //Serial.println("Wrote HTML Code file");
        break;

        case 4: // Send the Arduino Code Webpage here
            tsFile = SD.open("BackEnd1.htm");
            //Serial.println("Wrote Arduino Code Webpage");
        break;

        case 5: // Write Arduino Code File
            tsFile = SD.open("BackFil.txt");
            //Serial.println("Wrote Arduino Code File");
        break;

        case 6: // Send the CSS file here
            tsFile = SD.open("LILW3.css");
        break;      
    }; 
    //File is selected, write the file
    while (tsFile.available()){
        wryKly.write(tsFile.read());
    };
    tsFile.close();
    //Serial.println("Wrote a file");
    //Serial.println(wryKly.remoteIP());

    return;
}//End Web Page Protocols 

//Write images requested to the web page
void WebProto::wriImgs(EthernetClient imgKly, int imNum){
    //Create file to send
    File tsFile;

    //Acklowledge and get ready to send file
    imgKly.println("HTTP/1.1 200 OK");
    imgKly.println("");

    //Select which image to send here
    switch(imNum){
        case 1: // Send the Icon here or placeholder image
            tsFile = SD.open("CYBPLACE.jpg");
        break;

        case 2: // Send the Self image file here
            tsFile = SD.open("SELFPIC.jpg");
        break;
    }

    //Serial.println("Sending Picture");
    //Send whicever files was requested
    byte tBuff[64]; // create file buffer to use as iterator
    unsigned long lastP = 0; // last position
    if (tsFile) {
        while (tsFile.available()) {
            tsFile.read(tBuff, 64); //read file
            imgKly.write(tBuff, tsFile.position() - lastP); //Write file at position 
            lastP = tsFile.position();  // Update file position
        }
    };
    tsFile.close();
    return;
};//End Image Protocals

//Function for sending sensor data
void WebProto::wriSens(EthernetClient senKly, int sensPik){
    // Figure out which sensor they want and send it
    // No need to open a file for this
    //This bit is the same for all
    senKly.println("HTTP/1.1 200 OK");
    senKly.println("");
    // Writes the sensor data as requested
    switch (sensPik){
      case 1://Send the temperature going into the radiator
        senKly.println(TBRad[0]);
        //Serial.println(TBRad[0] + " TBRad"); // show output
        break;

      case 2://Send tmeperature from after radiator
        senKly.println(TARad[0]);
        //Serial.println(TARad[0] + " ARad");
        break;
      case 3://Send the flow speed, if its on else send OFF
        if(flow > 1.0){
            senKly.println(flow);
            //Serial.println(Flow + " Flow");      
        }
        else senKly.println("OFF");
        break;
    }

    //Done
    senKly.println("");
    return;
};//End sensor data function