#include "eicon.h"
#include <QFileInfo>
#include <QDirIterator>
#include <QDebug>
#include <QSettings>
#include <QCache>
#include <QtCore/QCoreApplication>
#include <QApplication>
//#include "qiconfix/qiconloader_p.h"
#include "eiconengin.h"
QT_BEGIN_NAMESPACE

Q_GLOBAL_STATIC(EIcon, EIconInstance)
void EIcon::seTemeName(const QString  &name)
{
    QIcon::setThemeName(name);
}

EIcon *EIcon::instance()
{
   return EIconInstance();
}
//______________________________________________________________________________________
QIcon EIcon::fromTheme(const QString  &iconName, const QString &fallback)
{


    QIcon iconF;


    ///------file Name--if exists
    if(QFile::exists(iconName)&& !iconName.isEmpty()){

        QString sufix=QFileInfo(iconName).suffix();
        if(sufix.toLower()=="png"
                ||sufix.toLower()=="svg"
                ||sufix.toLower()=="xpm"
                ||sufix.toLower()=="svgz"){
            iconF=QIcon(iconName);
            if(!iconF.isNull())
                return iconF;
        }
    }


    if(iconName.isEmpty())
        return QIcon::fromTheme("unknown");

    if(instance()->cachIcon.contains(iconName))
            return instance()->cachIcon.value(iconName);

    else if(QIcon::hasThemeIcon(iconName))
        iconF =  QIcon(new EIconEngin(iconName));

    else if(hasPathIcon(iconName,THEM_PIXMAP))
        iconF =  QIcon(new EIconEngin(iconName,THEM_PIXMAP));


    else if(!fallback.isEmpty() && QIcon::hasThemeIcon(fallback))
        iconF =  QIcon(new EIconEngin(fallback));

    else if(hasPathIcon(iconName,THEM_BACK))
        iconF =  QIcon(new EIconEngin(iconName,THEM_BACK));


    if(!iconF.availableSizes().isEmpty()){
        instance()->cachIcon.insert(iconF.name(),iconF);
        return iconF;
    }

    iconF=QIcon(":/icons/"+iconName);
    if(!iconF.pixmap(128).isNull())  return iconF;

    iconF=QIcon(":/icons/"+fallback);
    if(!iconF.pixmap(128).isNull())   return iconF;

    return QIcon::fromTheme(APP_EXE);

    /*
    if(QIcon::hasThemeIcon(iconName))      //from iconTheme   1
        iconF =  QIcon(new EIconEngin(iconName));
    if(!iconF.availableSizes().isEmpty()){
//        qDebug()<<"insert"<<iconName;
        instance()->cachIcon.insert(iconName,iconF);
        return iconF;
    }

    //iconF=getIconFromCharPixmap(iconName);  //from /usr/share/pixmaps 2
    if(EIconEngin::hasPathIcon(iconName,"/usr/share/pixmaps"))
    iconF =  QIcon(new EIconEngin(iconName,"/usr/share/pixmaps"));
    if(!iconF.availableSizes().isEmpty()){
        instance()->cachIcon.insert(iconName,iconF);
        return iconF;
    }

    if(QIcon::hasThemeIcon(fallback))       //fallback from iconTheme 3
    iconF =  QIcon(new EIconEngin(fallback));
    if(!iconF.availableSizes().isEmpty()){
        instance()->cachIcon.insert(iconName,iconF);
        return iconF;
    }

    //iconF=iconsFallbackTheme(iconName,THEM_BACK);//frome elokab icontheme
    if(EIconEngin::hasPathIcon(iconName,THEM_BACK))   //4
    iconF =  QIcon(new EIconEngin(iconName,THEM_BACK));
    if(!iconF.availableSizes().isEmpty()){
        instance()->cachIcon.insert(iconName,iconF);
        return iconF;
    }

    iconF=QIcon(":/icons/"+iconName);
    if(!iconF.pixmap(128).isNull())  return iconF;

    iconF=QIcon(":/icons/"+fallback);
    if(!iconF.pixmap(128).isNull())        return iconF;

    iconF =QIcon::fromTheme(APP_EXE);
    return iconF;
*/
}
//-------------------------------------------------------------------
bool EIcon::hasPathIcon(const QString &name,const QString &path)
{

    QString mPath=path;

    foreach (QString p, QIcon::themeSearchPaths())
    {
        QDir dirS(p);
        if(dirS.exists(p+"/"+path))
        {
            mPath=  p+"/"+path;
            break;
        }

    }

    return searchIcon(name,mPath);

}

bool EIcon::searchIcon(const QString &name,const QString &path)
{
    QStringList files = QDir(path).entryList(QStringList(name+".*"),
                        QDir::Files | QDir::NoDotAndDotDot);

    if(files.count()>0)
        return true;

    QDirIterator it( path,QDir::AllDirs |QDir::NoDotAndDotDot ,
                     QDirIterator::Subdirectories);

    while(it.hasNext())
    {
       // qDebug()<<"it.filePath()"<<it.filePath();
        it.next();
        if(hasPathIcon(name,it.filePath()))
            return true;
    }

    return false;
}
//______________________________________________________________________________________

QT_END_NAMESPACE
