//----------------------------------------------------------------------------
///
/// \file   icon.cpp
///
/// \brief  class for graphical icon
///
/// \date   20220919
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Documentation------------------------------------------------------------


//---Includes -----------------------------------------------------------------


//---General--------------------------


//--- Own ----------------------------

#undef LEPTO_LOG_DEBUG
//#define LEPTO_LOG_DEBUG

#include <ocelli/icon.hpp>
#include <lepto/log.h>
#include <biwak/biwak.h>         // biwakEventLoop()


//---Declaration---------------------------------------------------------------


CFlash *OIconRef::m_pFlash = nullptr;
TRgb OIconRef::m_line[ OIconRef::bufferLineWidth ];
SRgba OIconRef::m_inLine[ OIconRef::bufferLineWidth ];

bool OIconRef::find( const SIconDesc& desc )
{
   if(!m_pFlash)
   {
      lCritical("Flash not set");
      return(false);
   }
   SFlashIconHeader header;
   address32_t addr = desc.m_headerAddress;
   lDebug("Initial header address: 0x%X", addr);

   lDebug("Searching icon T=%d C=%d S=%d", desc.m_type, desc.m_content, desc.m_size);
   
   // Already loaded
   if( m_format != EImageFormat::Null )
   {
      lDebug("   was loaded");
      return( true );
   }

   do{
      int sta=m_pFlash->readData(addr, &header, sizeof(header));
      if( sta != 0 )
      {
         lCritical ("Could not read Flash: %d", sta);
      }
      else
      if( header.magic == ICON_MAGIC )
      {
         lDebug("Found icon: @0x%X", addr);
         #if 0
            lDebug("   Magic: %X", header.magic);
            lDebug("   Type: %d", (int)header.type);
            lDebug("   Content: %d", header.content);
            lDebug("   Next: %d", header.next);
         #endif

         if(      ( header.type == desc.m_type )
             &&   ( header.content == desc.m_content )
             //&&   ( header.size == desc.m_size )
         )
         {
            m_dataAddress=header.startData;
            m_width=header.width;
            m_height=header.height;
            m_format=(EImageFormat)header.format;
            lDebug("   Matches");
            return(true);
         }
         else
         {
            lDebug("   No match");
         }
         addr=header.nextHeader;
      }
      else
      {
         lWarning("Magic invalid: is:0x%X vs. 0x%X", header.magic, ICON_MAGIC);
         lWarning("   addr=0x%X", addr);
         addr=0;
      }
      //biwakEventLoop();
   } while(addr);
   
   if(!addr)
   {
      lDebug("No icon matches");
   }

   return(false);
}

bool OIconRef::find( ) const
{
   if(!m_pFlash)
   {
      lCritical("Flash not set");
      return(false);
   }
   
   lDebug("Searching icon");
   
   // Already loaded
   if( m_format != EImageFormat::Null )
   {
      lDebug("   was loaded");
      return( true );
   }
   
   return(false);
}

int OIconRef::usage()
{
   if(!m_pFlash)
   {
      lCritical("Flash not set");
      return(false);
   }
   SFlashIconHeader header;
   address32_t addr=0;
   unsigned int _usage=0;

   do{
      int sta=m_pFlash->readData(addr, &header, sizeof(header));
      if( sta != 0 )
      {
         lCritical ("Could not read Flash: %d", sta);
      }
      else
      if( header.magic == ICON_MAGIC )
      {
         if( header.startData )
         {
            _usage=MAX( _usage, header.startData + header.dataSize );
            _usage=MAX( _usage, addr + sizeof(SFlashIconHeader) );
         }
         addr=header.nextHeader;
      }
      else
      {
         addr=0;
      }
   }while(addr);
   lInfo("Flash usage: %d", _usage);
   return(_usage);
}


//---fin-----------------------------------------------------------------------
