#include "loapptools.h"
#include "loappconfig.h"
#include <wProgramManager/wEditProgram/wpageitem.h>
#include <LoUIClass/x_uimsgboxok.h>
LoAppTools* LoAppTools::m_instance = nullptr;

LoAppTools::LoAppTools(QObject *parent) :
    QObject (parent)
{
    m_tick = new QTimer(this);
    connect(m_tick, SIGNAL(timeout()), this, SIGNAL(sTick()));
    m_tick->start(TICK_INTERVAL);
}

LoAppTools* LoAppTools::getInstance()
{
    if(m_instance == nullptr)
        m_instance = new LoAppTools(qApp);
    return m_instance;
}

QBrush LoAppTools::getBrush(const QColor& color)
{
    QBrush bsh;
    if(color.alpha() == 0) {
        bsh = Qt::NoBrush;
    } else {
        bsh = QBrush(color);
    }
    return bsh;
}

int LoAppTools::color2Int(const QColor& color)
{
    int res = 0;
    res |= (color.red()   & 0xFF) << 24;
    res |= (color.green() & 0xFF) << 16;
    res |= (color.blue()  & 0xFF) <<  8;
    res |= (color.alpha() & 0xFF);
    return res;
}

QColor LoAppTools::int2Color(int value)
{
    QColor res;
    res.setRed  ((value >> 24) & 0xFF);
    res.setGreen((value >> 16) & 0xFF);
    res.setBlue ((value >>  8) & 0xFF);
    res.setAlpha((value      ) & 0xFF);
    return res;
}

bool LoAppTools::checkFileConflicted(const QString &fileNew)
{
    QStringList flist = m_page->filesList();
    flist.removeDuplicates();
    QFileInfo fiNew(fileNew);
    foreach(QString fileCur, flist) {
        QFileInfo fiCur(fileCur);
        if(fiNew.fileName() == fiCur.fileName() && fiNew.size() != fiCur.size()) {
            return true;
        }
    }
    return false;
}

QString LoAppTools::selectFile(const QString &filter, QWidget *parent)
{
    LoAppConfig *cfg = LoAppConfig::getInstance();
    QString file = QFileDialog::getOpenFileName(parent, tr("Select File"), cfg->SearchPath(), filter);
    if(file.isNull()) return QString::Null();
    if(checkFileConflicted(file)) {
        auto dlg = new X_UIMsgBoxOk(tr("File name conflicted"),"", parent,0);
        dlg->exec();
        return QString::Null();
    }
    cfg->setSearchPath(QFileInfo(file).absolutePath());
    return file;
}
QStringList LoAppTools::selectPhotoFile(const QString &filter, QWidget *parent)
{
    LoAppConfig *cfg = LoAppConfig::getInstance();
    QString strs;
    QStringList file_list, output_name;
    QStringList str_path_list = QFileDialog::getOpenFileNames(parent, tr("Select File"), cfg->SearchPath(), filter);
    for (int i = 0; i < str_path_list.size(); i++){
        QString str_path = str_path_list[i];
        //单个文件路径
        qDebug() << "path=" << str_path;
        QFileInfo file = QFileInfo(str_path);
        //获得文件名
        QString file_name = file.fileName();
        file_list.append(str_path);
        output_name.append(file_name);
        strs.append(file_name);
        strs += "\n";
    }
    return str_path_list;
}
QString LoAppTools::selectFile(const QString &filter, QWidget *parent,bool bSame)
{
    LoAppConfig *cfg = LoAppConfig::getInstance();
    QString file = QFileDialog::getOpenFileName(parent, tr("Select File"), cfg->SearchPath(), filter);
    if(file.isNull()) return QString::Null();
    if(bSame)
    {

    }
    else {
        if(checkFileConflicted(file)) {
            auto dlg = new X_UIMsgBoxOk(tr("File name conflicted"),"", parent,0);
            dlg->exec();
            return QString::Null();
        }

    }
    cfg->setSearchPath(QFileInfo(file).absolutePath());
    return file;
}
QString LoAppTools::selectStr(bool f, const QString &s0, const QString &s1)
{
    return f ? s0 : s1;
}

//QString LoAppTools::convertFileSize(quint64 n)
//{
//    QString res;
//    if(n < 1024) {
//        res = QString("%1B").arg(n);
//    } else if(n < 1024 * 1024) {
//        res = QString("%1KB").arg(n/1024);
//    } else if(n < 1024 * 1024 * 1024) {
//        res = QString("%1MB").arg(n/(1024 * 1024));
//    } else {
//        res = QString("%1GB").arg(n/(1024 * 1024 * 1024));
//    }
//    return res;
//}
QString LoAppTools::convertFileSize(const qlonglong & bytes)
{
    float num = bytes;
    QStringList list;
    list << "KB" << "MB" << "GB" << "TB";

    QStringListIterator i(list);
    QString unit("bytes");

    while(num >= 1024.0 && i.hasNext())
    {
     unit = i.next();
     num /= 1024.0;
    }
    return QString().setNum(num,'f',2)+" "+unit;
}
//QString LoAppTools::convertFileSize(const qlonglong & bytes)
//{

//    QString number;

//    if(bytes < 0x400) //If less than 1 KB, report in B
//    {
//     number = QLocale::system().toString(bytes);
//     number.append(" B");
//     return number;
//    }
//    else
//    {
//     if(bytes >= 0x400 && bytes < 0x100000) //If less than 1 MB, report in KB, unless rounded result is 1024 KB, then report in MB
//     {
//      qlonglong result = (bytes + (0x400/2))/0x400;

//      if(result < 0x400)
//      {
//       number = QLocale::system().toString(result);
//       number.append(" KB");
//       return number;
//      }
//      else
//      {
//       qlonglong result = (bytes + (0x100000/2))/0x100000;
//       number = QLocale::system().toString(result);
//       number.append(" MB");
//       return number;
//      }
//     }
//     else
//     {
//      if(bytes >= 0x100000 && bytes < 0x40000000) //If less than 1 GB, report in MB, unless rounded result is 1024 MB, then report in GB
//      {
//       qlonglong result = (bytes + (0x100000/2))/0x100000;

//       if(result < 0x100000)
//       {
//        number = QLocale::system().toString(result);
//        number.append(" MB");
//        return number;
//       }
//       else
//       {
//        qlonglong result = (bytes + (0x40000000/2))/0x40000000;
//        number = QLocale::system().toString(result);
//        number.append(" GB");
//        return number;
//       }
//      }
//      else
//      {
//       if(bytes >= 0x40000000 && bytes < 0x10000000000) //If less than 1 TB, report in GB, unless rounded result is 1024 GB, then report in TB
//       {
//        qlonglong result = (bytes + (0x40000000/2))/0x40000000;

//        if(result < 0x40000000)
//        {
//         number = QLocale::system().toString(result);
//         number.append(" GB");
//         return number;
//        }
//        else
//        {
//         qlonglong result = (bytes + (0x10000000000/2))/0x10000000000;
//         number = QLocale::system().toString(result);
//         number.append(" TB");
//         return number;
//        }
//       }
//       else
//       {
//        qlonglong result = (bytes + (0x10000000000/2))/0x10000000000; //If more than 1 TB, report in TB
//        number = QLocale::system().toString(result);
//        number.append(" TB");
//        return number;
//       }
//      }
//     }
//    }
//}
