#include "dbStructureView.h"
#include "dbStructureNode.h"
#include "dbStructureModel.h"
#include "dbManager.h"
#include "customMenu.h"
#include <QMenu>
#include <QPoint>
#include <QMouseEvent>

DBStructureView::DBStructureView(DatabaseManager* dbManager, QWidget* parent)
    : QTreeView(parent), m_dbManager(dbManager)
{
    // connect(m_dbManager, &DatabaseManager::connectionStatusChanged,
    //         [this](const QString& connName, bool connected)
    //         {

    //         });

    m_curConnection = "";
    initView();
}

DBStructureView::~DBStructureView()
{

}

void DBStructureView::mouseDoubleClickEvent(QMouseEvent* event)
{
    //只对鼠标左键的双击事件进行响应
    if(event->button() != Qt::LeftButton)
    {
        //event->accept();
        return;
    }
    QTreeView::mouseDoubleClickEvent(event);
}

void DBStructureView::initView()
{
    setHeaderHidden(true);
    setContextMenuPolicy(Qt::CustomContextMenu);
    setSelectionMode(SingleSelection);

    connect(this, &QTreeView::doubleClicked, this, &DBStructureView::itemDoubleClick);
    connect(this, &QTreeView::customContextMenuRequested, this ,&DBStructureView::showContextMenu);
}

void DBStructureView::connectToDB(const QString& connName)
{
    DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
    if(model && m_dbManager)
    {
        bool bResutl = m_dbManager->connect(connName);
        if(bResutl)
            model->refreshStructure_Connection(connName);

        /*QModelIndex index = model->getConnNodeIndex(connName);
        if(index.isValid())
        {
            if(!isExpanded(index))
                expand(index);
        }*/
    }
}

void DBStructureView::disconnectToDB(const QString& connName)
{
    DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
    if(model && m_dbManager)
    {
        m_dbManager->disconnect(connName);
        model->refreshStructure_Connection(connName);
        QModelIndex index = model->getConnNodeIndex(connName);
        if(index.isValid())
            collapse(index);
    }
}

void DBStructureView::disconnectCurConnection()
{
    if(!m_curConnection.isEmpty())
    {
        disconnectToDB(m_curConnection);
        m_curConnection = "";
    }
}

const QString DBStructureView::curConnection()
{
    return m_curConnection;
}

void DBStructureView::itemDoubleClick(const QModelIndex& index)
{
    if(!m_curConnection.isEmpty()) //先断掉当前链接
    {
        disconnectToDB(m_curConnection);
        m_curConnection = "";
    }

    DBStructureNode* node = static_cast<DBStructureNode*>(index.internalPointer());
    if(node->type() == ConnectionNode && node->status() == Disconnect)
    {
        connectToDB(node->name());
        m_curConnection = node->name();
    }
}

void DBStructureView::showContextMenu(const QPoint& pos)
{
    QModelIndex index = indexAt(pos);
    if(!index.isValid())
        return;

    DBStructureNode* node = static_cast<DBStructureNode*>(index.internalPointer());
    if(node->type() == ConnectionNode)
    {
        bool isConnected = true;
        if(node->status() == Disconnect)
            isConnected = false;

        QString connName = node->name();
        CustomMenu menu;
        menu.addAction(QString::fromWCharArray(L"链接"), [this, &connName]{
                connectToDB(connName);
                //展开链接
                DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
                QModelIndex index = model->getConnNodeIndex(connName);
                if(model && index.isValid())
                    expand(index);
            })->setEnabled(!isConnected);
        menu.addAction(QString::fromWCharArray(L"断开链接"), [this, &connName]{disconnectToDB(connName);})->setEnabled(isConnected);
        menu.addAction(QString::fromWCharArray(L"刷新"), [this, &connName]{
            DBStructureModel* model = dynamic_cast<DBStructureModel*>(this->model());
            if(model)
                model->refreshStructure_Connection(connName);
        });
        menu.addSeparator();
        menu.addAction(QString::fromWCharArray(L"添加模型"), [this]{emit actionTrigger_addModel();});
        QPoint originPoint = this->mapToGlobal(QPoint(0,0));
        menu.exec(originPoint + pos);
    }
    else if(node->type() == TableNode)
    {
        CustomMenu menu;
        menu.addAction(QString::fromWCharArray(L"删除"), [this]{});
    }
}
