/***************************************************************************

    source::worx xtree
    Copyright © 2024-2025 c.holzheuer
    christoph.holzheuer@gmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


#include <xqappdata.h>

#include <QDebug>
#include <QApplication>
#include <QStyle>
#include <QIcon>
#include <QMap>
#include <QMetaEnum>
#include <QPushButton>

namespace XQAppData
{
  template<typename E>
  constexpr auto to_underlying(E e) noexcept
  {
    return static_cast<std::underlying_type_t<E>>(e);
  }



  class XQAppIconMap : public QMap<QString,QIcon>
  {
  public:

    XQAppIconMap()
    {

    }

    void init()
    {
      namedInsert(  "DirIcon" ,         QStyle::SP_DirIcon );
      namedInsert(  "FileDialogBack",   QStyle::SP_FileDialogBack );
      namedInsert(  "FileDialogContentsView",  QStyle::SP_FileDialogContentsView );
      namedInsert(  "FileDialogDetailedView",  QStyle::SP_FileDialogDetailedView );
      namedInsert(  "icn05Dummy",  QStyle::SP_FileDialogEnd );
      namedInsert(  "icn06Dummy",  QStyle::SP_FileDialogInfoView );
      namedInsert(  "icn07Dummy",  QStyle::SP_FileDialogListView );
      namedInsert(  "icn08Dummy",  QStyle::SP_FileDialogNewFolder );
      namedInsert(  "icn09Dummy",  QStyle::SP_FileDialogStart );
      namedInsert(  "icn10Dummy",  QStyle::SP_FileDialogToParent );
      namedInsert(  "icn11Dummy",  QStyle::SP_ArrowBack );
      namedInsert(  "icn12Dummy",  QStyle::SP_DirIcon );
      namedInsert(  "icn13Dummy",  QStyle::SP_MediaSkipBackward );
      namedInsert(  "icn14Dummy",  QStyle::SP_ArrowDown );
      namedInsert(  "icn15Dummy",  QStyle::SP_DirLinkIcon );
      namedInsert(  "icn16Dummy",  QStyle::SP_MediaSkipForward );
      namedInsert(  "icn17Dummy",  QStyle::SP_ArrowForward );
      namedInsert(  "icn18Dummy",  QStyle::SP_DirOpenIcon );
      namedInsert(  "icn19Dummy",  QStyle::SP_MediaStop );
      namedInsert(  "icn20Dummy",  QStyle::SP_ArrowLeft );
      namedInsert(  "icn21Dummy",  QStyle::SP_DockWidgetCloseButton );
      namedInsert(  "icn22Dummy",  QStyle::SP_MediaVolume );
      namedInsert(  "icn23Dummy",  QStyle::SP_ArrowRight );
      namedInsert(  "icn24Dummy",  QStyle::SP_DriveCDIcon );
      namedInsert(  "icn25Dummy",  QStyle::SP_MediaVolumeMuted );
      namedInsert(  "icn26Dummy",  QStyle::SP_ArrowUp );
      namedInsert(  "icn27Dummy",  QStyle::SP_DriveDVDIcon );
      namedInsert(  "icn28Dummy",  QStyle::SP_MessageBoxCritical );
      namedInsert(  "icn29Dummy",  QStyle::SP_BrowserReload );
      namedInsert(  "icn30Dummy",  QStyle::SP_DriveFDIcon );
      namedInsert(  "icn31Dummy",  QStyle::SP_MessageBoxInformation );
      namedInsert(  "BrowserStop",  QStyle::SP_BrowserStop );
      namedInsert(  "icn33Dummy",  QStyle::SP_DriveHDIcon );
      namedInsert(  "icn34Dummy",  QStyle::SP_MessageBoxQuestion );
      namedInsert(  "CommandLink",  QStyle::SP_CommandLink );
      namedInsert(  "icn36Dummy",  QStyle::SP_DriveNetIcon );
      namedInsert(  "MessageBoxWarning",  QStyle::SP_MessageBoxWarning );
      namedInsert(  "ComputerIcon",  QStyle::SP_ComputerIcon );
      namedInsert(  "icn39Dummy",  QStyle::SP_FileDialogBack );
      namedInsert(  "icn40Dummy",  QStyle::SP_TitleBarCloseButton );
      namedInsert(  "icn42Dummy",  QStyle::SP_FileDialogContentsView );
      namedInsert(  "icn43Dummy",  QStyle::SP_TitleBarContextHelpButton );
      namedInsert(  "DesktopIcon",  QStyle::SP_DesktopIcon );
      namedInsert(  "icn45Dummy",  QStyle::SP_FileDialogDetailedView );
      namedInsert(  "icn46Dummy",  QStyle::SP_TitleBarMaxButton );
      namedInsert(  "icn47Dummy",  QStyle::SP_DialogApplyButton );
      namedInsert(  "icn48Dummy",  QStyle::SP_FileDialogEnd );
      namedInsert(  "icn49Dummy",  QStyle::SP_TitleBarMenuButton );
      namedInsert(  "icn50Dummy",  QStyle::SP_DialogCancelButton );
      namedInsert(  "icn51Dummy",  QStyle::SP_FileDialogInfoView );
      namedInsert(  "icn52Dummy",  QStyle::SP_TitleBarMinButton );
      namedInsert(  "icn53Dummy",  QStyle::SP_DialogCloseButton );
      namedInsert(  "icn54Dummy",  QStyle::SP_FileDialogListView );
      namedInsert(  "icn55Dummy",  QStyle::SP_TitleBarNormalButton );
      namedInsert(  "icn56Dummy",  QStyle::SP_DialogDiscardButton );
      namedInsert(  "icn57Dummy",  QStyle::SP_FileDialogNewFolder );
      namedInsert(  "icn58Dummy",  QStyle::SP_TitleBarShadeButton );
      namedInsert(  "icn59Dummy",  QStyle::SP_DialogHelpButton );
      namedInsert(  "icn60Dummy",  QStyle::SP_FileDialogStart );
      namedInsert(  "icn61Dummy",  QStyle::SP_TitleBarUnshadeButton );
      namedInsert(  "icn62Dummy",  QStyle::SP_DialogNoButton );
      namedInsert(  "icn63Dummy",  QStyle::SP_FileDialogToParent );
      namedInsert(  "icn64Dummy",  QStyle::SP_ToolBarHorizontalExtensionButton );
      namedInsert(  "icn65Dummy",  QStyle::SP_DialogOkButton );
      namedInsert(  "FileIcon",    QStyle::SP_FileIcon );
      namedInsert(  "icn67Dummy",  QStyle::SP_ToolBarVerticalExtensionButton );
      namedInsert(  "icn68Dummy",  QStyle::SP_DialogResetButton );
      namedInsert(  "icn70Dummy",  QStyle::SP_FileLinkIcon );
      namedInsert(  "TrashIcon",   QStyle::SP_TrashIcon );
      namedInsert(  "icn72Dummy",  QStyle::SP_DialogSaveButton );
      namedInsert(  "icn73Dummy",  QStyle::SP_MediaPause );
      namedInsert(  "VistaShield", QStyle::SP_VistaShield );
      namedInsert(  "icn75Dummy",  QStyle::SP_DialogYesButton );
      namedInsert(  "MediaPlay",   QStyle::SP_MediaPlay );
      namedInsert(  "icn77Dummy",  QStyle::SP_DirClosedIcon );
      namedInsert(  "icn79Dummy",  QStyle::SP_MediaSeekBackward );
      namedInsert(  "DirHomeIcon", QStyle::SP_DirHomeIcon );
      namedInsert(  "icn81Dummy",  QStyle::SP_MediaSeekForward );
      /*
      auto from = to_underlying(QIcon::ThemeIcon::AddressBookNew);
      auto to =  to_underlying(QIcon::ThemeIcon::NThemeIcons);
      for (auto i = from; i < to; ++i)
      {
        QIcon::ThemeIcon f = static_cast<QIcon::ThemeIcon>(i);
        QIcon icon = QIcon::fromTheme(f);
        // Nur hinzufügen, wenn das Icon existiert
        if (!icon.isNull())
          insert(icon.name(), icon);
      }
      */

    }

    void namedInsert( const QString& key, QStyle::StandardPixmap pixmapID )
    {
      QIcon icon = QApplication::style()->standardIcon( pixmapID );
      insert( key, icon );
      _keysToNames.insert( icon.cacheKey(), key );
    }

    QString iconName( const QIcon& icon )
    {
      return _keysToNames[icon.cacheKey()];
    }

  protected:

    QMap<qint64,QString> _keysToNames;
  };



  /*
  Nein, so nicht! QApplication::style() gibts hier noch nicht -> aua!
  static XQAppIconMap s_IconMap
  {
      { "icnFolder" ,  QApplication::style()->standardIcon(QStyle::SP_DirIcon) }
  }
  */


  static XQAppIconMap s_IconMap;


  QIcon typeIcon(const QString& key )
  {
    if(s_IconMap.isEmpty())
      s_IconMap.init();
    if( s_IconMap.contains(key) )
      return s_IconMap[key];

    return  QApplication::style()->standardIcon( QStyle::SP_TrashIcon);
  }

  QString iconName( const QIcon& icon )
  {
    if(s_IconMap.isEmpty())
      s_IconMap.init();
    return s_IconMap.iconName(icon);
  }

}; // namespace XQAppData


