// #include <SPI.h>
// #include <mySD.h>
#include "FS.h"
#include "FileLib.h"

File myFile;

// sample call
// int16_t datapac[1000];
// SD_FileWrite(&datapac, "/PARMS.ADI",sizeof(datapac));
// if we do it like this.. the data dosen't matter if byte 16 or 32.
// just might have to dink with the pointers


bool SD_FileExists(char *filename)
{
  if (SD.exists(filename))
    return (1);
  else
    return (0);
}

void SD_ParmWrite(int uploadHTTPs)
{
  int i, j;

  //FS fileLibSPIFFS = FL_SPIFFS;

  const char *filename = "/PARMS.ADI";
  if (fileLibSPIFFS.exists(filename))
    fileLibSPIFFS.remove(filename);
  File SD_File = fileLibSPIFFS.open(filename, FILE_WRITE);

  Serial.println("Writing Parm file");

  // if the file opened okay, write to it:
  qualifyParms();

  // calc CSUM
  parm[0] = 0;
  for (i = 1; i < (PARMSIZE - 1); i++)
    parm[0] += parm[i];

  if (SD_File)
  {
    Serial.println(String("Open Parms"));

    for (i = 0; i < PARMSIZE; i++)
    {
      SD_File.write((parm[i] >> 24) & 0xFF);
      SD_File.write((parm[i] >> 16) & 0xFF);
      SD_File.write((parm[i] >> 8) & 0xFF);
      SD_File.write(parm[i] & 0xFF);
    }

    displayText("Parm File Save");

    // close the file:
    SD_File.close();
    Serial.println("SD Close");
    restoreFault(7);
    if (uploadHTTPs)
      UpdateParmHTTPs(); // update cloud
  }
  else
  {
    // if the file didn't open, print an error:
    Serial.println("SD Error");
    qualifyFault(1,0);
  }
}

byte SD_ParmRead(void)
{
  long ll;
  byte err = 0;
  int i;

  Serial.println("Reading Parm File");

  //FS fileLibSPIFFS = FL_SPIFFS;

  // open the file for reading:
  File SD_File = fileLibSPIFFS.open("/PARMS.ADI");
  if (SD_File)
  {
    // read from the file until there's nothing else in it:
    if (SD_File.available())
    {
      /*
        for(i = 0;i < 48;i++)
             SDinstr[i] = SD_File.read();
            memcpy(&ssid[0],&SDinstr[0],48);
            for(i = 0;i < 48;i++)
             SDinstr[i] = SD_File.read();
            memcpy(&pass[0],&SDinstr[0],48);
      */

      for (i = 0; i < PARMSIZE; i++)
      {
        parm[i] = SD_File.read() << 24;
        parm[i] = parm[i] | SD_File.read() << 16;
        parm[i] = parm[i] | SD_File.read() << 8;
        parm[i] = parm[i] | SD_File.read();

        Serial.println(parm[i]);
      }
      restoreFault(1);
    }
    else
    {
      qualifyFault(1,0);
    }

    // close the file:
    SD_File.close();

    displayText("Parm File Read");

    // calc/check CSUM
    ll = 0;
    for (i = 1; i < (PARMSIZE - 1); i++)
      ll += parm[i];

    if (ll != parm[0])
    {
      Serial.print("CSUM ERR: ");
      err = 1;
      Serial.println(String(ll));
    }
    else
    {

      Serial.println("CSUM OK");
      Serial.print("Serial: ");
      Serial.println(String(parm[64]));
    }
  }
  else
  {
    // if the file didn't open, print an error:
    Serial.println("SD Error");
    err = 1;
    qualifyFault(1,0);
  }
  // delay(20);
  if (err != 0)
    parms.factoryReset();

  Serial.print("SD Read ");
  Serial.println(err);
  return (err);
}

void SD_RemoteParmWrite(char *filename)
{
  int i, j;

  FS fs = SD;

  Serial.print("Saving ");
  Serial.println(filename);

  if (fs.exists(filename))
    fs.remove(filename);
  File SD_File = fs.open(filename, FILE_WRITE);

  // if the file opened okay, write to it:

  if (SD_File)
  {
    Serial.println("Parms Open");

    for (i = 0; i < PARMSIZE; i++)
    {
      SD_File.write((remoteParm[i] >> 24) & 0xFF);
      SD_File.write((remoteParm[i] >> 16) & 0xFF);
      SD_File.write((remoteParm[i] >> 8) & 0xFF);
      SD_File.write(remoteParm[i] & 0xFF);
    }

    displayText("Parm File Save");

    // close the file:
    SD_File.close();
    Serial.println("SD Close");
    restoreFault(7);
  }
  else
  {
    // if the file didn't open, print an error:
    Serial.println("SD Error");
    qualifyFault(1,0);
  }
}
