/***************************************************************************
 **
 **  Copyright (C) 2018-2023 wangjianjun <wangjianjun1018@qq.com>
 **  Contact: https://blog.csdn.net/wocanimei007?type=blog
 **
 **
****************************************************************************/
#include "frameless.h"
#include "frameless_p.h"
#include <QColor>
#include <QTextCodec>

#if defined(Q_OS_WIN32) && !defined(Q_CC_MSVC)
#include <Windows.h>
#include <shellapi.h>
#else
#include <QDir>
#include <QDesktopServices>
#include <QUrl>
#endif

namespace Frameless {

QColor argbToColor(const QString &argb)
{
    QColor color;
    bool ok = true;
    QRgb rgba = argb.toUInt(&ok, 16);

    if (ok) {
        color = QColor::fromRgba(rgba);
    } else {
        color = QColor(0, 0, 0);
    }

    return color;
}

QString colorToArgb(const QColor &color)
{
    QString argb =  QString("%1%2%3%4")
                    .arg(color.alpha(), 2, 16, QLatin1Char('0'))
                    .arg(color.red(), 2, 16, QLatin1Char('0'))
                    .arg(color.green(), 2, 16, QLatin1Char('0'))
                    .arg(color.blue(), 2, 16, QLatin1Char('0'));
    return argb;
}

void showInExplorer(QString fileName)
{
    Q_UNUSED(fileName);
#if defined(Q_OS_WIN32) && !defined(Q_CC_MSVC)
    QString argsFile = QString("/select, %1").arg(fileName.replace("/", "\\"));
    QTextCodec *codec = QTextCodec::codecForName("GB18030");
    ShellExecuteA(nullptr, "open", "explorer.exe", codec->fromUnicode(argsFile).constData(), nullptr, SW_SHOWDEFAULT);
#else
    QDesktopServices::openUrl(QUrl(QFileInfo(fileName).absolutePath(), QUrl::TolerantMode));
#endif
}

void setSysSettings(const QString &organization, const QString &application)
{
    FramelessPrivate::g_settingsOrganization = organization;
    FramelessPrivate::g_settingsApplication  = application;
}

} // namespace Frameless
