#include "presentcontainer.h"
#include "DocsManager.h"

#include <QMenu>

using namespace Components;
using namespace Core;
using namespace MakeTools;

PresentContainer::PresentContainer(TabPosition pos, HostListener *host)
    : host_inst(host) {
    setTabPosition(pos);
    setTabsClosable(true);
    setMovable(true);

    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, &QWidget::customContextMenuRequested, [this](const QPoint &p) {
        auto index = this->tabBar()->tabAt(p);
        setCurrentIndex(index);

        Route key_peaks;
        for (auto &v : items_store) {
            if (v->widget() == widget(index)) {
                key_peaks = items_store.key(v);
                break;
            }
        }

        QMenu m;

        auto trans = m.addMenu("转移标签页");
        trans->setEnabled(key_peaks.isValid());
        trans->addAction("到左侧视图", [this, key_peaks]() { emit presentTransTo(key_peaks, GroupType::LEFT); });
        trans->addAction("到右侧视图", [this, key_peaks]() { emit presentTransTo(key_peaks, GroupType::RIGHT); });
        trans->addAction("到底部视图", [this, key_peaks]() { emit presentTransTo(key_peaks, GroupType::BOTTOM); });
        trans->addAction("到中央视图", [this, key_peaks]() { emit presentTransTo(key_peaks, GroupType::CENTER); });

        m.exec(mapToGlobal(p));
    });

    connect(this, &QTabWidget::tabCloseRequested, this, &PresentContainer::accept_close_action);
}

QWidget *PresentContainer::hostWidget() const { return (QWidget *)this; }

void PresentContainer::append(PresentBase *ins, const Core::Route &key) {
    addTab(ins->widget(), ins->name());
    items_store[key] = ins;

    if (host_inst)
        host_inst->hasBeenAccepted(key);
}

bool PresentContainer::active(const Core::Route &unique) {
    if (!items_store.contains(unique))
        return false;

    auto inst = items_store[unique];
    inst->widget()->setVisible(true);

    auto index = indexOf(inst->widget());
    setCurrentIndex(index);
    setTabText(index, inst->name());
    return true;
}

PresentBase *PresentContainer::remove(const Core::Route &key) {
    if (!items_store.contains(key))
        return nullptr;

    auto inst = items_store[key];
    auto index = indexOf(inst->widget());
    removeTab(index);
    items_store.remove(key);

    if (host_inst)
        host_inst->hasBeenRemoved(key);
    return inst;
}

bool PresentContainer::instContains(MakeTools::PresentBase *ins) const {
    for (auto &insit : items_store) {
        if (insit == ins)
            return true;
    }
    return false;
}

void PresentContainer::setVisibleState(bool state) { hostWidget()->setVisible(state); }

bool PresentContainer::visibleState() const { return hostWidget()->isVisible(); }

void PresentContainer::accept_close_action(int index) {
    auto disp = widget(index);
    for (auto &key : items_store.keys()) {
        auto inst = items_store[key];
        if (inst->widget() == disp) {
            items_store.remove(key);
            removeTab(index);

            if (host_inst)
                host_inst->hasBeenClosed(key);

            return;
        }
    }
}
