#include <Arduino.h>
#include <SD.h>

const int chipSelect = 15; // chipselect declaration
// const int chipSelect = 5; // chipselect declaration
char sdBuff [10] ={0};

typedef struct {
//   time_t prefdtm;
  uint16_t prefprobeCode;
  uint16_t preflandCode;
  uint16_t prefdataTake;
  uint16_t prefsampleCount;
  float preflat;
  float preflon;
  float preftemp;
  float prefhum;
  float prefec;
  float prefph;
  uint16_t prefnit;
  uint16_t prefpho;
  uint16_t prefkal;
  uint16_t prefdbNum;
} dataSD;

dataSD isiData;
dataSD dataTerbaca;


// Saveto SD Card
void saveDataToSD(const char *filename, dataSD *dataToSave) {
  File file = SD.open(filename, FILE_WRITE);
  if (file) {
    file.write((uint8_t *)dataToSave, sizeof(dataSD));
    file.close();
    Serial.println("Saving data to SD Card success");
  } else {
    Serial.println("Failed saving data to SD Card");
  }
}

//Read from SD card
  void readDataFromSD(const char *filename, dataSD *readData) {
  File file = SD.open(filename, FILE_READ);
  if (file) {
    file.read((uint8_t *)readData, sizeof(dataSD));
    file.close();
    Serial.println("Read from sdcard Success!");
  } else {
    Serial.println("Read from sdCard failed!");
  }
}

bool deleteFileFromSD(const char *filename) {
  if (SD.remove(filename)) {
    return true; // File berhasil dihapus
  } else {
    return false; // Gagal menghapus file
  }
}

void fillSDStruct()
{
// currentData.prefdtm;
isiData.prefprobeCode = 24199;
isiData.preflandCode = 0000;
isiData.prefdataTake = 0;
isiData.prefsampleCount = 0;
isiData.preflat = -3255;
isiData.preflon = 6000;
isiData.preftemp = 37;
isiData.prefhum = 50;
isiData.prefec = 200;
isiData.prefph = 7;
isiData.prefnit = 100;
isiData.prefpho = 200;
isiData.prefkal = 300;
isiData.prefdbNum = 1;
}

void serialPrintData()
{

Serial.print("Data Terbaca: \n");

  Serial.print("data number: ");
//   Serial.println(indexLoad);
//   Serial.println(dataTerbaca.prefdtm);
  Serial.println(dataTerbaca.prefprobeCode);
  Serial.println(dataTerbaca.preflandCode);
  Serial.println(dataTerbaca.prefdataTake);
  Serial.println(dataTerbaca.prefsampleCount);
  Serial.println(dataTerbaca.preflat);
  Serial.println(dataTerbaca.preflon);
  Serial.print("Temp: ");
  Serial.println(dataTerbaca.preftemp);
  Serial.print("Hum: ");
  Serial.println(dataTerbaca.prefhum);
  Serial.println(dataTerbaca.prefec);
  Serial.print("pH: ");
  Serial.println(dataTerbaca.prefph);
  Serial.print("Nit: ");
  Serial.println(dataTerbaca.prefnit);
  Serial.print("Pho: ");
  Serial.println(dataTerbaca.prefpho);
  Serial.print("Kal: ");
  Serial.println(dataTerbaca.prefkal);
  Serial.print("Num: ");
  Serial.println(dataTerbaca.prefdbNum);
  Serial.println("-------------------");
}



void setup() {
  Serial.begin(115200);

  if (!SD.begin(chipSelect)) {
    Serial.println("SD card Initialization failed!");
    // while (true);
    return;
  }

  Serial.println("SD card Initialization success!");
  fillSDStruct();

  int indexSD = 1;
  sprintf(sdBuff,"/data%02d.txt", indexSD);
  saveDataToSD(sdBuff, &isiData);
  Serial.println(sdBuff);
//   saveDataToSD("/data.txt", &isiData);
  
  // Read & show saved data on sd card
  // dataSD dataTerbaca;  
  indexSD = 1;
  sprintf(sdBuff,"/data%02d.txt", indexSD);
  readDataFromSD(sdBuff, &dataTerbaca);
//   readDataFromSD("/data.txt", &dataTerbaca);
//   Serial.print("Data Terbaca: ");
  serialPrintData();


  // sprintf(sdBuff,"/data%02d.txt", indexSD);
  // deleteFileFromSD(sdBuff);
  // sprintf(sdBuff,"/data%02d.txt", indexSD);
  // readDataFromSD(sdBuff, &dataTerbaca);

}

void loop() {
}