#ifndef _LOGGING_H_
#define _LOGGING_H_

// SD Card Logging

#include <SPI.h> //SCK=13, MISO=12, MOSI=11
#include <SD.h> //Format sd cart with FAT or FAT16

#define SDCHIPSELECT 14
boolean datalogging=true;
String datalogging_filename="UNKNOWN.txt";

bool initLogging();
void loggingLoop(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm& escRear);
void writeLogComment(unsigned long time, String msg);


bool initLogging() {
    Serial.print("Initializing SD card...");
    // see if the card is present and can be initialized:
    if (!SD.begin(SDCHIPSELECT)) {
        Serial.println("Card failed, or not present");
        display.print(F("SD Init Fail!")); display.display();
        datalogging=false; //disable logging
        delay(1000);
        return false;
    }else{
        Serial.println("Card initialized.");
        display.print(F("LOG=")); display.display();
        if (datalogging){
        int filenumber=0;
        char buffer[6];
        sprintf(buffer, "%04d", filenumber);
        datalogging_filename="LOG_"+String(buffer)+".TXT";
        while(SD.exists(datalogging_filename) && filenumber<10000) {
            Serial.print(datalogging_filename); Serial.println(" exists");
            filenumber++;
            sprintf(buffer, "%04d", filenumber);
            datalogging_filename="LOG_"+String(buffer)+".TXT";

        }      
        Serial.print(datalogging_filename); Serial.println(" is free");
        display.print(datalogging_filename); display.display();
        }
    }
    return true;
}


void loggingLoop(unsigned long loopmillis,ESCSerialComm& escFront, ESCSerialComm& escRear) {

  static unsigned long last_datalogging_write=0;
  static boolean logging_headerWritten=false;
  if (datalogging) {
    #define LOGGINGINTERVAL 100
    if (loopmillis-last_datalogging_write>LOGGINGINTERVAL)
    {
      last_datalogging_write=loopmillis;
      
      File dataFile = SD.open(datalogging_filename, FILE_WRITE);

      if (dataFile) { // if the file is available, write to it
        if (!logging_headerWritten) {
          dataFile.print("time,cmd_FrontL,cmd_FrontR,cmd_RearL,cmd_RearR,");
          dataFile.print("current_FrontL,current_FrontR,current_RearL,current_RearR,");
          dataFile.print("rpm_FrontL,rpm_FrontR,rpm_RearL,rpm_RearR,");
          dataFile.print("temp_Front,temp_Rear,vbat_Front,vbat_Rear,");
          dataFile.print("currentAll,throttle,brake,speed,trip_Front,trip_Rear,currentConsumed_Front,currentConsumed_Rear,");
          dataFile.println("temp_ESCFront,temp_ESCRear,temp_Air,looptime_duration");
          dataFile.print("#TIMESTAMP:"); dataFile.println(now());
          logging_headerWritten=true;
        }
        dataFile.print(loopmillis/1000.0,3); dataFile.print(",");
        dataFile.print(escFront.getCmdL());  dataFile.print(",");
        dataFile.print(escFront.getCmdR());  dataFile.print(",");
        dataFile.print(escRear.getCmdL());  dataFile.print(",");
        dataFile.print(escRear.getCmdR());  dataFile.print(",");
        dataFile.print(escFront.getFiltered_curL(),3);  dataFile.print(",");
        dataFile.print(escFront.getFiltered_curR(),3);  dataFile.print(",");
        dataFile.print(escRear.getFiltered_curL(),3);  dataFile.print(",");
        dataFile.print(escRear.getFiltered_curR(),3);  dataFile.print(",");
        dataFile.print(escFront.getFeedback_speedL_meas());  dataFile.print(","); //+ //Todo: check if speed for R wheels needs to be negated
        dataFile.print(escFront.getFeedback_speedR_meas());  dataFile.print(","); //-
        dataFile.print(escRear.getFeedback_speedL_meas());  dataFile.print(","); //+
        dataFile.print(escRear.getFeedback_speedR_meas());  dataFile.print(","); //-
        dataFile.print(escFront.getFeedback_boardTemp());  dataFile.print(",");
        dataFile.print(escRear.getFeedback_boardTemp());  dataFile.print(",");
        dataFile.print(escFront.getFeedback_batVoltage());  dataFile.print(",");
        dataFile.print(escRear.getFeedback_batVoltage());  dataFile.print(",");
        dataFile.print(filtered_currentAll,3);  dataFile.print(",");
        dataFile.print(throttle_pos);  dataFile.print(",");
        dataFile.print(brake_pos);  dataFile.print(",");
        dataFile.print((escFront.getMeanSpeed()+escRear.getMeanSpeed())/2.0);  dataFile.print(",");
        dataFile.print(escFront.getTrip());  dataFile.print(",");
        dataFile.print(escRear.getTrip());  dataFile.print(",");
        dataFile.print(escFront.getCurrentConsumed(),3); dataFile.print(",");
        dataFile.print(escRear.getCurrentConsumed(),3); dataFile.print(",");
        dataFile.print(temp_ESCFront,2); dataFile.print(",");
        dataFile.print(temp_ESCRear,2); dataFile.print(",");
        dataFile.print(temp_Air,2); dataFile.print(",");
        dataFile.print(looptime_duration,0); dataFile.print(",");
        
        dataFile.println("");
        dataFile.close();
      }
    }
  }
}

void writeLogComment(unsigned long time, String msg) {
    //SerialRef.print("#"); SerialRef.print(time/1000.0,3); SerialRef.print(","); SerialRef.print(msg); SerialRef.println();
    if (datalogging) {
        File dataFile = SD.open(datalogging_filename, FILE_WRITE);

        if (dataFile) { // if the file is available, write to it
            dataFile.print("#");
            dataFile.print(time/1000.0,3); 
            dataFile.print(","); 
            dataFile.print(msg); 
            dataFile.println();
            dataFile.close();
        }
    }
}

String getLogFilename() {
  return datalogging_filename;
}

#endif