#include "mainwindow.h"

#include <QSplitter>
#include <QStackedWidget>
#include <QTextBrowser>
#include <QThread>

#include <util/asynchelper.h>
#include <util/jsonmapper.h>
#include <util/layoutbuilder.h>

#include "api/roomapi.h"
#include "api/userapi.h"
#include "appevent.h"
#include "capturer/video/wgc/App.h"
#include "network/websocketclient.h"
#include "qboxlayout.h"
#include "qlistwidget.h"
#include "qobject.h"
#include "ui/av_recorder.h"
#include "views/loginwindow.h"

#include "widgets/bubbletip.h"
#include "widgets/chatView/chat1/chatview.h"
#include "widgets/chatView/chatwindow.h"
#include "widgets/framelessbase.h"
#include "widgets/maskoverlay.h"
#include "widgets/userprofilewidget.h"
#include "widgets/meetingselectionwidget.h"
#include "widgets/joinmeetingdialog.h"
#include "widgets/createmeetingdialog.h"

#include <QDebug>
#include <QFutureWatcher>

#include <api/loginapi.h>

#include "util/qformatter.h"

#include <qtpromise/qpromise.h>
#include <qtpromise/qpromisefuture.h>
#include <qtpromise/qpromisehelpers.h>

#include "AvPlayer2/PlayWidget.h"
#include "MainPanel.h"

#include <QListWidget>

// MainWidget 类已移除，相关实现迁移到 MainPanel.cpp/h

MainWindow::MainWindow(QWidget *parent)
    : TMainWindow(parent)
{
    setWindowTitle("test");
    setCentralWidget(new QWidget(this));
    // 创建主布局
    layout = new QVBoxLayout(centralWidget());
    layout->setContentsMargins(0, 0, 0, 0);

    // 创建堆叠部件
    stackedWidget = new QStackedWidget(this);
    layout->addWidget(stackedWidget);

    // 创建登录页面
    loginWidget = new LoginWidget(this);
    stackedWidget->addWidget(loginWidget);

    // 创建会议选择页面
    meetingSelectionWidget = new MeetingSelectionWidget(this);
    stackedWidget->addWidget(meetingSelectionWidget);

    mainWidget = nullptr;

    connect(loginWidget, &LoginWidget::loginRequested, this, &MainWindow::onLoginSuccess);
    connect(meetingSelectionWidget, &MeetingSelectionWidget::joinMeetingRequested, this, &MainWindow::onJoinMeetingRequested);
    connect(meetingSelectionWidget, &MeetingSelectionWidget::createMeetingRequested, this, &MainWindow::onCreateMeetingRequested);
    connect(meetingSelectionWidget, &MeetingSelectionWidget::logoutRequested, this, &MainWindow::onMeetingSelectionLogout);
    
    resize(1280, 600);

    // 显示登录页面
    stackedWidget->setCurrentWidget(loginWidget);
}

MainWindow::~MainWindow() {}

void MainWindow::createMainWindow()
{
    if (mainWidget) {
        delete mainWidget;
        mainWidget = nullptr;
    }
    // 主窗口
    mainWidget = new MainPanel(this);
    stackedWidget->addWidget(mainWidget);

    // 退出处理
    connect(mainWidget, &MainPanel::logoutClicked, this, [this]() {
        AppEvent *appEvent = AppEvent::instance();
        appEvent->setJwtToken(QString()); // 清理token
        appEvent->setRefreshTime(0);      // 清理token刷新时间
        appEvent->setRoles({});           // 清理角色
        appEvent->setUserId({});          // 清理用户id

        stackedWidget->setCurrentWidget(loginWidget);
    });
}

void MainWindow::onLoginSuccess(const QString &username, const QString &password)
{
    // 准备登录参数
    AuthApi::LoginParams loginParams;
    loginParams.username = username;
    loginParams.password = password;

    QFuture<HttpResponse> loginApiFuture = AuthApi::loginApi(loginParams);

    // 使用fromQFuture将QFuture转换为QPromise
    QtPromise::QPromise<HttpResponse> loginApiPromise = QtPromise::resolve(loginApiFuture);

    MaskOverlay::instance()->show(stackedWidget, 0, MaskOverlay::ActiveWindow);

    loginApiPromise
        .then([this](const HttpResponse &response) {
            qDebug() << response.code << response.data << response.message;
            if (response.code != 0) {
                BubbleTip::showTip(this, response.message, BubbleTip::Top, 3000);
                return;
            }
            QString token;
            if (response.data.isObject()) {
                QJsonObject dataObj = response.data.toObject();
                qDebug() << dataObj;
                token = dataObj["token"].toString();
                AppEvent::instance()->setJwtToken(token);
                authLogin(); // 获取登录信息
            }

            if (!token.isEmpty()) {
                qDebug() << "登录成功，获取到的令牌：" << token;
                // 存储令牌
            }
        })
        .finally([]() { MaskOverlay::instance()->hide(); });
}
void MainWindow::authLogin()
{
    // 创建两个异步操作的Promise
    QFuture<HttpResponse> userInfoFuture = getUserInfoApi();
    QFuture<HttpResponse> accessCodesFuture = AuthApi::getAccessCodesApi();

    // 使用fromQFuture将QFuture转换为QPromise
    QtPromise::QPromise<HttpResponse> userInfoPromise = QtPromise::resolve(userInfoFuture);
    QtPromise::QPromise<HttpResponse> accessCodesPromise = QtPromise::resolve(accessCodesFuture);

    // 使用QtPromise::all等待所有Promise完成
    QtPromise::all(QVector<QtPromise::QPromise<HttpResponse>>{userInfoPromise, accessCodesPromise})
        .then([this](const QVector<HttpResponse> &results) {
            // 处理用户信息

            // 用户信息: 200 QJsonValue(object, QJsonObject({"id":"e52e6b47-f4d3-43a6-b481-570ffcc1c9db","role":[],"username":"admin"})) "success"
            // 访问代码: 203 QJsonValue(null) "Error transferring http://127.0.0.1:8888/api/auth/codes - server replied: Not Found"
            const HttpResponse &userInfo = results[0];
            qDebug() << "用户信息:" << userInfo.code << userInfo.data << userInfo.message;
            UserInfo user = JsonMapper::fromJson<UserInfo>(userInfo.data);

            // 处理访问代码
            const HttpResponse &accessCodes = results[1];
            qDebug() << "访问代码:" << accessCodes.code << accessCodes.data << accessCodes.message;

            // 处理成功的逻辑
            if (userInfo.code == 0 && accessCodes.code == 0) {
                qDebug() << "所有请求成功完成";
                // 这里可以添加后续处理逻辑
            }
            qDebug() << "当前角色:" << user.roleName;
            qDebug() << "当前ID:" << user.id;
            AppEvent::instance()->setRoles(user.roleName);
            AppEvent::instance()->setUserId(user.id);
            AppEvent::instance()->setUserName(user.username);

            // 显示会议选择页面
            showMeetingSelection();
        })
        .fail([this](const std::exception &e) {
            // 处理错误
            AppEvent::instance()->setRoles({});
            AppEvent::instance()->setUserId({});
            qWarning() << "登录后处理失败:" << e.what();
            BubbleTip::showTip(this,
                               QString("请求失败: %1").arg(e.what()),
                               BubbleTip::Top,
                               3000,
                               BubbleTip::Type::Error);
        });
}

void MainWindow::showMeetingSelection()
{
    // 设置会议选择页面的用户信息
    AppEvent *appEvent = AppEvent::instance();
    meetingSelectionWidget->setUserRoles(appEvent->roles());
    meetingSelectionWidget->setUserInfo(appEvent->userName(), appEvent->userId());
    
    // 显示会议选择页面
    stackedWidget->setCurrentWidget(meetingSelectionWidget);
}

void MainWindow::onJoinMeetingRequested()
{
    // 创建加入会议对话框
    JoinMeetingDialog *joinDialog = new JoinMeetingDialog(this);
    
    // 获取会议列表并设置到对话框
    RoomInfo roomInfo;
    QFuture<HttpResponse> getRoomListFuture = getRoomListApi(roomInfo);
    
    // 使用QtPromise处理异步获取会议列表
    QtPromise::QPromise<HttpResponse> roomListPromise = QtPromise::resolve(getRoomListFuture);
    
    roomListPromise
        .then([this, joinDialog](const HttpResponse &response) {
            qDebug() << "获取会议列表:" << response.data << response.rawData << response.message;
            
            if (response.code == 0) {
                RoomListData roomListData(response.data.toObject());
                QList<RoomInfo> roomInfos = roomListData.getRooms();
                
                QStringList meetingIds;
                QStringList meetingNames;
                
                // 提取会议ID和名称
                for (const RoomInfo &room : roomInfos) {
                    if (room.id.has_value() && room.name.has_value()) {
                        meetingIds.append(room.id.value());
                        meetingNames.append(room.name.value());
                    }
                }

                // 设置可用的会议列表
                joinDialog->setAvailableMeetings(meetingIds, meetingNames);
            } else {
                qWarning() << "获取会议列表失败:" << response.message;
                BubbleTip::showTip(this,
                                   QString("获取会议列表失败: %1").arg(response.message),
                                   BubbleTip::Top,
                                   3000,
                                   BubbleTip::Type::Error);
            }
            
            // 显示对话框
            if (joinDialog->exec() == QDialog::Accepted) {
                QString meetingId = joinDialog->getSelectedMeetingId();
                
                // 创建主界面部件（用于加入会议）
                createMainWindow();

                // 加入会议时，强制使用PlayWidget（观看者模式），无论用户角色如何
                QStringList joinRoles = {"role.viewer"}; // 强制设置为观看者角色
                mainWidget->setRole(joinRoles);
                
                // 显示主界面
                stackedWidget->setCurrentWidget(mainWidget);
                
                // 检查房间状态并验证会议ID是否存在
                checkRoom(meetingId);
            }
            
            joinDialog->deleteLater();
        })
        .fail([this, joinDialog](const std::exception &e) {
            qWarning() << "获取会议列表异常:" << e.what();
            BubbleTip::showTip(this,
                               QString("获取会议列表失败: %1").arg(e.what()),
                               BubbleTip::Top,
                               3000,
                               BubbleTip::Type::Error);
            
            // 即使获取失败也显示对话框，允许手动输入
            if (joinDialog->exec() == QDialog::Accepted) {
                QString meetingId = joinDialog->getSelectedMeetingId();
                
                // 创建主界面部件（用于加入会议）
                createMainWindow();

                // 加入会议时，强制使用PlayWidget（观看者模式），无论用户角色如何
                QStringList joinRoles = {"role.viewer"}; // 强制设置为观看者角色
                mainWidget->setRole(joinRoles);
                
                // 显示主界面
                stackedWidget->setCurrentWidget(mainWidget);
                
                // 检查房间状态并验证会议ID是否存在
                checkRoom(meetingId);
            }
            
            joinDialog->deleteLater();
        });
}

void MainWindow::onCreateMeetingRequested()
{
    // 获取会议列表并设置到对话框
    RoomInfo roomInfo;
    roomInfo.ownerId = AppEvent::instance()->userId();

    QFuture<HttpResponse> getRoomListFuture = getRoomListApi(roomInfo);

    // 使用QtPromise处理异步获取会议列表
    QtPromise::QPromise<HttpResponse> roomListPromise = QtPromise::resolve(getRoomListFuture);

    roomListPromise
        .then([this](const HttpResponse &response) {
            qDebug() << "获取会议列表:" << response.data << response.rawData << response.message;

            if (response.code == 0) {
                RoomListData roomListData(response.data.toObject());
                QList<RoomInfo> roomInfos = roomListData.getRooms();

                QStringList meetingIds;
                QStringList meetingNames;

                // 提取会议ID和名称
                for (const RoomInfo &room : std::as_const(roomInfos)) {
                    if (room.id.has_value() && room.name.has_value()) {
                        meetingIds.append(room.id.value());
                        meetingNames.append(room.name.value());
                    }
                }
                if (meetingIds.size() > 0) {
                    checkRoom(meetingIds[0]);
                }

            } else {
                qWarning() << "获取会议列表失败:" << response.message;
                BubbleTip::showTip(this,
                                   QString("获取会议列表失败: %1").arg(response.message),
                                   BubbleTip::Top,
                                   3000,
                                   BubbleTip::Type::Error);
            }

            // 创建主界面部件（用于加入会议）
            createMainWindow();

            // 加入会议时，强制使用PlayWidget（观看者模式），无论用户角色如何
            mainWidget->setRole({"role.admin"});

            // 显示主界面
            stackedWidget->setCurrentWidget(mainWidget);
        })
        .fail([this](const std::exception &e) {
            qWarning() << "获取会议列表异常:" << e.what();
            BubbleTip::showTip(this,
                               QString("获取会议列表失败: %1").arg(e.what()),
                               BubbleTip::Top,
                               3000,
                               BubbleTip::Type::Error);
        });
}

void MainWindow::onMeetingSelectionLogout()
{
    // 清理用户信息
    AppEvent *appEvent = AppEvent::instance();
    appEvent->setJwtToken(QString()); // 清理token
    appEvent->setRefreshTime(0);      // 清理token刷新时间
    appEvent->setRoles({});           // 清理角色
    appEvent->setUserId({});          // 清理用户id
    appEvent->setUserName({});        // 清理用户名
    
    // 返回登录页面
    stackedWidget->setCurrentWidget(loginWidget);
}

void MainWindow::checkRoom(const QString &meetingId)
{
    // 创建两个异步操作的Promise
    RoomInfo roomInfo;
    roomInfo.id = "";
    QFuture<HttpResponse> getRoomListFuture = getRoomListApi(roomInfo);

    // 使用fromQFuture将QFuture转换为QPromise
    QtPromise::QPromise<HttpResponse> userInfoPromise = QtPromise::resolve(getRoomListFuture);

    // 使用QtPromise::all等待所有Promise完成
    QtPromise::all(QVector<QtPromise::QPromise<HttpResponse>>{userInfoPromise})
        .then([this, meetingId](const QVector<HttpResponse> &results) {
            const HttpResponse &getRoomList = results[0];
            qDebug() << "访问房间:" << getRoomList.data << getRoomList.rawData
                     << getRoomList.message;
            // JsonObject({"data":[{"createdAt":1752512296000,"createdId":null,"description":"","id":"019809df-75db-73db-adbb-2ca949a9329a","maxUsers":100,"name":"默认房间","ownerId":"0196e8ac-c737-7f93-ba6a-6562f2c8854d","status":1,"updatedAt":1752512296000}],"total":1})

            RoomListData roomListData(getRoomList.data.toObject());
            QList<RoomInfo> RoomInfos = roomListData.getRooms();
            qDebug() << RoomInfos.size();

            // 填充房间列表
            // for (int var = 0; var < RoomInfos.size(); ++var) {
            //     const RoomInfo room = RoomInfos.at(var);
            //     if (room.name) {
            //         QListWidgetItem *item = new QListWidgetItem(room.name.value_or(""),
            //                                                     mainWidget->roomListWidget());
            //         item->setData(Qt::UserRole + 100, room.id.value_or(""));
            //         mainWidget->roomListWidget()->addItem(item);
            //     }
            // }

            // 如果有待验证的会议ID，检查是否存在
            if (!meetingId.isEmpty()) {
                bool meetingFound = false;
                int foundIndex = -1;
                
                for (int i = 0; i < RoomInfos.size(); ++i) {
                    const RoomInfo room = RoomInfos.at(i);
                    if (room.id.value_or("") == meetingId) {
                        meetingFound = true;
                        foundIndex = i;
                        break;
                    }
                }
                
                if (meetingFound) {
                    // 会议ID存在，设置为当前房间
                    // mainWidget->roomListWidget()->setCurrentRow(foundIndex);
                    mainWidget->setPushRoomId(meetingId);
                    qDebug() << "成功加入会议:" << meetingId;

                    BubbleTip::showTip(this,
                                       QString("成功加入会议: %1").arg(meetingId),
                                       BubbleTip::Top,
                                       3000,
                                       BubbleTip::Type::Success);
                } else {
                    // 会议ID不存在，显示错误提示
                    qWarning() << "会议ID不存在:" << meetingId;

                    BubbleTip::showTip(this,
                                       QString("会议ID不存在: %1").arg(meetingId),
                                       BubbleTip::Top,
                                       5000,
                                       BubbleTip::Type::Error);

                    // 回到会议选择页面
                    showMeetingSelection();
                }
            } else if (RoomInfos.size() > 0) {
                // 没有待验证的会议ID，使用默认的第一个房间
                // mainWidget->roomListWidget()->setCurrentRow(0);
                mainWidget->setPushRoomId(RoomInfos[0].id.value_or(""));
                // mainWidget->chatView->initWebsocket(RoomInfos[0].id.value());

                // if (AvRecorder *avRecorder = qobject_cast<AvRecorder *>(mainWidget->playerWidget)) {
                //     SettingsPage::Param param;
                //     param.liveUrl = "rtmp://192.168.3.76:1935/stream/V1";
                //     param.liveName = RoomInfos[0].id.value_or("").toStdString();
                //     avRecorder->setSettings(param);
                // }
            }
        })
        .fail([this](const std::exception &e) {
            // 处理错误
            qWarning() << "[异常]获取房间失败:" << e.what();
            BubbleTip::showTip(this,
                               QString("请求失败: %1").arg(e.what()),
                               BubbleTip::Top,
                               3000,
                               BubbleTip::Type::Error);
        });
}
