#include <neoPixel.h>
#include "SDTools.h"
#include "bmpImage.h"
#include "filePath.h"
#include <resizeBuff.h>

#define SD_CS   4     // SD chip select pin number. Change to suit your setup.

neoPixel    display(32*32,2);
filePath    wd;

void setup() {

	Serial.begin(9600);                     // So we can talk.
	SD.begin(SD_CS);                        // So we can read data.
  wd.setPath("/System/icons/standard/");  // Where we read the data.
  display.begin();                        // Fire up the display.
}


void displayBmp(const char* bmpPath) {

  bmpImage  theImage(bmpPath);
  RGBpack   thePixel;
  colorObj  aColor;

  theImage.openDocFile(FILE_READ);            // Open this image file for reading.
  for (int y=0;y<32;y++) {                    // From top to bottom..
    for (int x=0;x<32;x++) {                  // Left to right..
      thePixel = theImage.getRawPixel(x,y);   // Grab a packed pixel color.
      aColor.setColor(&thePixel);             // Set the color object to this packed pixel color.
      display.setPixelColor(32*y+x,&aColor);  // Drop it into the neopixel call.
    }                                         //
  }                                           //
  theImage.closeDocFile();                    // All done? Close the file.
  display.show();                             // Let Adafruit blit out the image.
}


bool checkFile(pathItem*	item) {

  if (item->name[0]=='.') return false;             // Mac's add dots to begginings of many of it's files.
  if (!strstr(item->name,"32.BMP")) return false;   // All of these files end in 32.BMP. So check for this.
  return true;
}


void listDirectory(void) {

  pathItem*		trace;
  int         numBytes;
  char*       path;
  
  path = NULL;                                                          //
  if (wd.getPathType()==fileType) {                             // If this is pointing to a file..? 
    Serial.println("Sorry, this is a file, not a directory.");  // Just wrong, we can't list a file.
    return;                                                     // Just walk away..
  } else {                                                      // Else, not a file. Good!
    numBytes = wd.numPathBytes();                               // We will need to memory to store the full path.
    numBytes = numBytes + 14;                                   // Folder path name plus the max (14) bytes.
    if (resizeBuff(numBytes,&path)) {                           // If we can resize our buffer to this size.
      wd.refreshChildList();                                    // Make sure we have an up to date child list..
      if (wd.numChildItems()==0) {                              // Wait, no children to list?
        Serial.println("This directory is empty.");             // Tell the user.
        return;                                                 // And walk away.
      } else {                                                  // Else, we DO have kids to list.
        trace = wd.childList;																	  // Grab a pointer to the first child.
        while(trace) {																				  // While we have a non-NULL pointer..
          if (checkFile(trace)) {															  // Pass this child through the crucible of the user's filter function.
            strcpy(path,wd.getPath());                          // We need a fullpath, start with folder path..
            strcat(path,trace->getName());                      // Adding file name gives the full path.
            Serial.println(path);                               // Show mrs user.
            displayBmp(path);                                   // BLit it to the screen.
            delay(2000);                                        // Give them some time to "Ew and Ahh.."
          }                                                     //
          trace = (pathItem*)trace->dllNext;                    // Jump to the next item on the list.
        }
      }
      resizeBuff(0,&path);                                        // Release the memory we used for all this.
    } else {                                                      // Else.. Ran out of memory!
      Serial.println("Sorry, not enough RAM for this to work.");  // Tell Mrs user.
    }
  }
}


// Your loop where it parses out all your typings.
void loop(void) {
	listDirectory();
  delay(2000);
}