#include <biwak/flash_spi.hpp>
#include <biwak/gdisplay_ili9341.hpp>
#include <lepto/log.h>
#include <fosh/command.h>
#include <string.h>

#define guint     uint32_t
#define guint8    uint8_t
// #include "logo2.c"
// #include "logo1.c"
// #include "../../media/splash_proto/logo1.c"


class CCommandGd:public CCommand
{
   CGraphicDisplayIli9341 &m_display;
public:
   CCommandGd( CGraphicDisplayIli9341 &display )
      :CCommand("gd", "Interact with graphical display")
      ,m_display(display)
   {
   };
   virtual int exec(int argc, const char * argv[]) const override
   {
      int factor=4;
      uint8_t *data=new uint8_t[(240*3)/factor];
#if 0
      if(!strcmp(argv[1], "dshow"))
      {
         m_display.setFrame(0,0,120-1,120-1);
         for(int i1=0; i1<gimp_image.height; i1++)
         {
            int lineSize=gimp_image.width * gimp_image.bytes_per_pixel;
            int offset=i1 * lineSize;
            //m_flash.readSector(offset, data, lineSize);
            m_display.drawPixels( (TRgb *)&gimp_image.pixel_data[offset], gimp_image.width );
         }
      }

      if(!strcmp(argv[1], "dfshow"))
      {
         m_display.setFrame(0,0,120-1,120-1);
         m_display.drawPixels( (TRgb *)&gimp_image.pixel_data, gimp_image.width * gimp_image.height );
      }
#endif


#if 0
      if(!strcmp(argv[1], "base64"))
      {
         int dest=strtol(argv[2], NULL, 0);
         int realSize;
         realSize=CBase64::decode( argv[3], strlen(argv[3]), data, 0x100);
         if(0)
         {
            m_flash.dumpData(data, realSize);
            lHint << "Input: " << argv[3];
         }
         lHint << "Destination address: " << dest;
         lHint << "Destination size: " << realSize;
         m_flash.write( dest, data, realSize );
      }
#endif

#if 0
      if(!strcmp(argv[1], "write"))
      {
         for(int i1=0;i1<0x100; i1++)
         {
            data[i1]=i1;
         }
         m_flash.write( 0x0, data, 0x100);
      }
#endif
#if 0
      if(!strcmp(argv[1], "red"))
      {
         uint8_t red[]={0xfe,1,2};
         for(int i1=0; i1<320*240; i1++)
         {
            m_flash.write( i1*3, red, 3);
         }
      }

      if(!strcmp(argv[1], "dump"))
      {
         int offset=0*240*3*(320/2);
         m_flash.read( offset, data, 0x100);
         m_flash.dumpData(data, 0x100);
      }

      if(!strcmp(argv[1], "info"))
      {
         lHint << "Id:  " << m_flash.readId();
         lHint << "Id:  " << m_flash.readJId();
         lHint << "ERP: " << m_flash.readErp();
         lHint << "STA: " << m_flash.readSta();
      }

      if(!strcmp(argv[1], "unlock"))
      {
         m_flash.unlock();
      }
#endif

      delete[] data;
      return(0);
   }
};

const CCommand *createInstance_gd( CGraphicDisplayIli9341 &display )
{
   return( new CCommandGd( display ) );
}
