// #include "AVPlayer2/mainwindowa.h"

#include <QApplication>
#include <QDateTime>
#include <QDebug>
#include <QFile>
#include <QLoggingCategory>
#include <QMutex>
#include <QTextStream>
#include <QVBoxLayout>
#include <qendian.h>
#include <qmath.h>


#include "AVPlayer/vframe.h"
#include "mainwindow.h"
#include "thememanager.h"
#include "themesettingswidget.h"

#include "tlogger.h"

#include "AVPlayer/avplayerwidget.h"
#include "AVPlayer/vframe.h"
#include "ui/av_recorder.h"
#include "widgets/recorderwidget.h"

namespace avrecorder::video {
void InitWinRTCapture();
}

#undef ERROR
// Qt输出（用于兼容现有Qt日志系统）

namespace TC {
class QtOutput : public LogOutput
{
public:
    void write(LogLevel level, const std::string &message) override;
};

void QtOutput::write(LogLevel level, const std::string &message)
{
    const QString qmsg = QString::fromUtf8(message.c_str());
    switch (level) {
    case LogLevel::DEBUG:
        qDebug() << qmsg;
        break;
    case LogLevel::INFO:
        qInfo() << qmsg;
        break;
    case LogLevel::WARNING:
        qWarning() << qmsg;
        break;
    case LogLevel::ERROR:
        qCritical() << qmsg;
        break;
    }
}
} // namespace TC

int main(int argc, char *argv[])
{
    // "player.controller.ReadThread=false\n"
    QLoggingCategory::setFilterRules(QStringLiteral("player.controller.AudioPlayThread=false\n"
                                                    "player.controller.AudioDecodeThread=false\n"));
    // QLoggingCategory::setFilterRules("*.debug=false\n"
    //                                  "*.info=false\n"
    //                                  "*.warning=false\n"
    //                                  "*.critical=false\n"
    //                                  "player.controller.*.debug=true\n"
    //                                  "player.controller.*.info=true\n");

    // 安装日志处理器
    //qInstallMessageHandler(myMessageHandler);

    // std::freopen(nullptr, "w", stdout);
    setvbuf(stdout, nullptr, _IONBF, 0);

#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(
        Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif

    QCoreApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);

    QApplication a(argc, argv);

    qRegisterMetaType<AVFrame *>("AVFrame*");
    qRegisterMetaType<QSharedPointer<VideoFrame>>("QSharedPointer<VideoFrame>");

    ThemeManager::instance().setThemeMode(ThemeManager::Light);

    // 注册Room 相关的类型 方便 序列化
    void initRoomType();
    initRoomType();

    // 初始化wgc

    avrecorder::video::InitWinRTCapture();

    /*
docker run -itd  --name zlmediakit --restart=always
-p 1935:1935 -p 8080:80 -p 8443:443
-p 8554:554 -p 10000:10000
-p 10000:10000/udp -p 8000:8000/udp
-p 9000:9000/udp
-v /data/zlmediakit/media/bin:/opt/media/bin
-v /data/zlmediakit/media/conf:/opt/media/conf
zlmediakit/zlmediakit:master
*/
    MainWindow w;
    w.show();

    // RecorderWidget testRecorder;
    // testRecorder.show();

    // AVPlayerWidget avPlayerWidget;
    // avPlayerWidget.show();

    int ret = a.exec();

    return ret;
}
