#include <Arduino.h>
#include "log.h"

extern FsFile LOG_FILE;
extern SdFs SD;
extern ArduinoOutStream cout(Serial);
extern HDDIMG  img[NUM_SCSIID][NUM_SCSILUN]; 
extern SCSI_DEVICE scsi_device_list[NUM_SCSIID][NUM_SCSILUN];
/*
 * Setup initialization logfile
 */

/*
 * Setup initialization logfile
 */
void initFileLog(int success_mhz) {
  LOG_FILE = SD.open(LOG_FILENAME, O_WRONLY | O_CREAT | O_TRUNC);
  LOG_FILE.println("BlackSASI <-> SD");
  LOG_FILE.print("VERSION: ");
  LOG_FILE.println(VERSION);
  LOG_FILE.print("DEBUG:");
  LOG_FILE.print(DEBUG);
  LOG_FILE.print(" SDFAT_FILE_TYPE:");
  LOG_FILE.println(SDFAT_FILE_TYPE);
  LOG_FILE.print("SdFat version: ");
  LOG_FILE.println(SD_FAT_VERSION_STR);
  LOG_FILE.print("Sd Format: ");
  switch(SD.vol()->fatType()) {
    case FAT_TYPE_EXFAT:
    LOG_FILE.println("exFAT");
    break;
    case FAT_TYPE_FAT32:
    LOG_FILE.print("FAT32");
    case FAT_TYPE_FAT16:
    LOG_FILE.print("FAT16");
    default:
    LOG_FILE.println(" - Consider formatting the SD Card with exFAT for improved performance.");
  }
  LOG_FILE.print("SPI speed: ");
  LOG_FILE.print(success_mhz);
  LOG_FILE.println("Mhz");
  if (success_mhz <= 24) {
    LOG_FILE.println("*** Slow SPI connection to SD card may cause performance degradation");
  }
  LOG_FILE.print("SdFat Max FileName Length: ");
  LOG_FILE.println(MAX_FILE_PATH);
  LOG_FILE.println("Initialized SD Card - let's go!");
  LOG_FILE.sync();
}

void errorPrint() {
  if (SD.sdErrorCode()) {
    cout << F("SD errorCode: ") << hex << showbase;
    printSdErrorSymbol(&Serial, SD.sdErrorCode());
    cout << F(" = ") << int(SD.sdErrorCode()) << endl;
    cout << F("SD errorData = ") << int(SD.sdErrorData()) << endl;
  }
}
void dmpVol() {
  cout << F("\nScanning FAT, please wait.\n");
  uint32_t freeClusterCount = SD.freeClusterCount();
  if (SD.fatType() <= 32) {
    cout << F("\nVolume is FAT") << int(SD.fatType()) << endl;
  } else {
    cout << F("\nVolume is exFAT\n");
  }
  cout << F("sectorsPerCluster: ") << SD.sectorsPerCluster() << endl;
  cout << F("clusterCount:      ") << SD.clusterCount() << endl;
  cout << F("freeClusterCount:  ") << freeClusterCount << endl;
  cout << F("fatStartSector:    ") << SD.fatStartSector() << endl;
  cout << F("dataStartSector:   ") << SD.dataStartSector() << endl;

}
/*
 * Finalize initialization logfile
 */
void finalizeFileLog() {
  // View support drive map
  LOG_FILE.print("ID");
  for(int lun=0;lun<NUM_SCSILUN;lun++)
  {
    LOG_FILE.print(":LUN");
    LOG_FILE.print(lun);
  }
  LOG_FILE.println(":");
  //
  for(int id=0;id<NUM_SCSIID;id++)
  {
    LOG_FILE.print(" ");
    LOG_FILE.print(id);
    for(int lun=0;lun<NUM_SCSILUN;lun++)
    {
      SCSI_DEVICE *dev = &scsi_device_list[id][lun];
      if( (lun<NUM_SCSILUN) && (dev->m_file))
      {
        LOG_FILE.print((dev->m_blocksize<1000) ? ": " : ":");
        LOG_FILE.print(dev->m_blocksize);
      }
      else      
        LOG_FILE.print(":----");
    }
    LOG_FILE.println(":");
  }
  LOG_FILE.println("Finished initialization of SCSI Devices - Entering main loop.");
  LOG_FILE.sync();
  #if DEBUG < 2
  LOG_FILE.close();
  #endif
}