#include "SDCardManager.h"

SDCardManager::SDCardManager(const Pin csPin, SPISettings cfg) : _csPin(csPin), _spiCfg(cfg), _mounted(false)
{
  
}

double SDCardManager::getSize(const SizeUnit sizeUnit)
{
  uint64_t numberOf512BytesChunks = blocksPerCluster() * totalClusters();//cardSize();
  double result = 0;

  if(!isMounted()) return 0;
  
  switch(sizeUnit)
  {
    case KBIT:
      result = (double)numberOf512BytesChunks/2.0*8;
      break;
    case KBYTE:
      result = (double)numberOf512BytesChunks/2.0;
      break;
    case MBIT:
      result = (double)numberOf512BytesChunks/2.0/1024.0*8;
      break;
    case MBYTE:
      result = (double)numberOf512BytesChunks/2.0/1024.0;
      break;
    case GBIT:
      result = (double)numberOf512BytesChunks/2.0/1024.0/1024.0*8;
      break;
    case GBYTE:
      result = (double)numberOf512BytesChunks/2.0/1024.0/1024.0;
      break;
    default:
      result = (double)numberOf512BytesChunks/2.0/1024.0/1024.0;
  }
  return result;
}

boolean SDCardManager::mountSD()
{
  _mounted = this->begin(_csPin, _spiCfg);
  return _mounted;
}

void SDCardManager::unMountSD()
{
  _mounted = false;
  this->end();
}

boolean SDCardManager::isMounted()
{
  return _mounted;
}
