#include "wnd_main.h"
#include <QPushButton>
#include <iostream>
#include <render/render.h>
#include <thread>
#include <QCloseEvent>
#include <QFileDialog>
#include <mutex>
#include <QWidget>
#include <QLayout>
#include "states.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QSplitter>

namespace {
//region 渲染窗口
std::atomic<bool> b_RenderWndPrepared{false};
std::thread tr_render;
Render *g_render;
std::atomic<HWND> hwnd_RenderWnd{nullptr};
DWORD renderWindowRet = 0;

void RenderWindow() {
    g_render = new Render{};
    hwnd_RenderWnd.store(g_render->InitWindow(GetModuleHandle(nullptr), L"DirectX"),
                         std::memory_order_release);
    b_RenderWndPrepared.store(true, std::memory_order::release);
    if (hwnd_RenderWnd == nullptr) return;
    renderWindowRet = g_render->ExecWindow();
    hwnd_RenderWnd.store(nullptr, std::memory_order_release);
    b_RenderWndPrepared.store(false, std::memory_order_release);
    delete g_render;

}
//endregion


QString GetMainWindowStyleSheet() {
    return {"#MainCentralWidget{background-color:#223344;}"
        "#MainCentralLeftWidget{background-color:#333322;}"
        "#MainCentralRightWidget{background-color:#332233;}"
        ".WndResource{background-color:#222233;}"
        ".WndHierachy{background-color:#112233;}"
        ".WndInspector{background-color:#221144;}"
        ".WndDebug{background-color:#442211;}"
    };
}
}

//= public
WndMain::WndMain() {

    InitRenderWindow();
    InitDesign();
    this->setWindowState(Qt::WindowActive);
    this->show();
}
WndMain::~WndMain() {
    if (tr_render.joinable())tr_render.join();
    qDebug("Render Thread Returns:%d", renderWindowRet);
}

//= protected
void WndMain::closeEvent(QCloseEvent *event) {
    QMainWindow::closeEvent(event);
    PostMessageW(hwnd_RenderWnd.load(std::memory_order_acquire),WM_CLOSE, 0, 0);
    while (hwnd_RenderWnd.load(std::memory_order::acquire)) {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    if (tr_render.joinable())tr_render.join();
    event->accept();
}

//= private
void WndMain::InitRenderWindow() const {
    tr_render = std::thread(RenderWindow);
    while (!b_RenderWndPrepared.load(std::memory_order::acquire)) {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}

void WndMain::InitDesign() {
    const int w = 720;
    const int h = 820;
    this->resize(w, h);
    this->move(w, States::DesktopHeight() - h - 80);
    this->setStyleSheet(GetMainWindowStyleSheet());

    // menu bar
    QMenuBar *menubar = menuBar();
    QMenu *menu_file = menubar->addMenu("File");
    QAction *act_test = menu_file->addAction("test");

    // central widget
    m_central = new QWidget(this);
    m_central->setObjectName("MainCentralWidget");
    setCentralWidget(m_central);

    // central widget layout
    QHBoxLayout *lay = new QHBoxLayout();
    m_central->setLayout(lay);

    // main splitter
    QSplitter *sp = new QSplitter(m_central);
    sp->setOrientation(Qt::Orientation::Horizontal);
    lay->addWidget(sp);
    sp->setChildrenCollapsible(false);
    lay->setContentsMargins(0, 0, 0, 0);

    // left area
    QWidget *leftWgt = new QWidget(this);
    leftWgt->setObjectName("MainCentralLeftWidget");
    leftWgt->setLayout(new QVBoxLayout());
    QSplitter *spl = new QSplitter(leftWgt);
    spl->setChildrenCollapsible(false);
    spl->setOrientation(Qt::Vertical);
    m_WndHierachy = new WndHierachy(this);
    spl->addWidget(m_WndHierachy);
    m_WndResource = new WndResource(this);
    spl->addWidget(m_WndResource);
    leftWgt->layout()->addWidget(spl);

    spl->setSizes({500, 500});

    // right area
    QWidget *rightWgt = new QWidget(this);
    rightWgt->setObjectName("MainCentralRightWidget");
    rightWgt->setLayout(new QVBoxLayout());
    QSplitter *spr = new QSplitter(rightWgt);
    spr->setChildrenCollapsible(false);
    spr->setOrientation(Qt::Orientation::Vertical);
    m_WndInspector = new WndInspector(this);
    spr->addWidget(m_WndInspector);
    m_WndDebug = new WndDebug(this);
    spr->addWidget(m_WndDebug);
    rightWgt->layout()->addWidget(spr);

    spr->setSizes({500, 500});

    sp->addWidget(leftWgt);
    sp->addWidget(rightWgt);
    sp->setMinimumWidth(100);
    sp->setSizes({500, 500});
}














//
