/*******************************************************************************
 **
 ** smolbote: yet another qute browser
 ** Copyright (C) 2017  Xian Nox
 **
 ** This program is free software: you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 ** the Free Software Foundation, either version 3 of the License, or
 ** (at your option) any later version.
 **
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 **
 ** You should have received a copy of the GNU General Public License
 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **
 ******************************************************************************/

#include "webviewtabbar.h"
#include "browser.h"
#include <QAction>

WebViewTabBar::WebViewTabBar(QWidget *parent) :
    QTabBar(parent)
{
    setElideMode(Qt::ElideRight);
    setTabsClosable(true);
    setMovable(true);
    connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(removeTab(int)));
    connect(this, SIGNAL(currentChanged(int)), this, SLOT(handleCurrentChanged(int)));
    connect(this, SIGNAL(tabMoved(int,int)), this, SLOT(updateVectorArrangement(int,int)));

    if(sSettings->contains("window.shortcuts.tabClose")) {
        QAction *tabCloseAction = new QAction(this);
        tabCloseAction->setShortcut(QKeySequence::fromString(sSettings->value("window.shortcuts.tabClose").toString()));
        connect(tabCloseAction, &QAction::triggered, [this]() {
            this->removeTab(currentIndex());
        });
        addAction(tabCloseAction);
    }
    if(sSettings->contains("window.shortcuts.tabLeft")) {
        QAction *tabLeftAction = new QAction(this);
        tabLeftAction->setShortcut(QKeySequence::fromString(sSettings->value("window.shortcuts.tabLeft").toString()));
        connect(tabLeftAction, &QAction::triggered, [this]() {
            this->setCurrentIndex(currentIndex()-1);
        });
        addAction(tabLeftAction);
    }
    if(sSettings->contains("window.shortcuts.tabRight")) {
        QAction *tabRightAction = new QAction(this);
        tabRightAction->setShortcut(QKeySequence::fromString(sSettings->value("window.shortcuts.tabRight").toString()));
        connect(tabRightAction, &QAction::triggered, [this]() {
            this->setCurrentIndex(currentIndex()+1);
        });
        addAction(tabRightAction);
    }

    m_signalMapper = new QSignalMapper(this);
    connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(webAction(int)));
}

WebViewTabBar::~WebViewTabBar()
{
    // cleanup
    qDeleteAll(m_views);
    m_views.clear();
}

QSignalMapper *WebViewTabBar::signalMapper()
{
    return m_signalMapper;
}

int WebViewTabBar::addTab(QWebEngineProfile *profile, const QUrl &url)
{
    WebView *view = new WebView(0);
    QWebEnginePage *page = new QWebEnginePage(profile);
    view->setPage(page);
    page->load(url);
    m_views.append(view);

    //connect(view, SIGNAL(titleChanged()), this, SLOT(updateTabText()));
    connect(view, &QWebEngineView::titleChanged, [this, view](const QString &title) {
        int index = m_views.indexOf(view);
        setTabText(index, title);
    });
    connect(view, &QWebEngineView::iconChanged, [this, view](const QIcon &icon) {
        int index = m_views.indexOf(view);
        setTabIcon(index, icon);
    });

    return QTabBar::addTab("New Tab");
}

void WebViewTabBar::setProfile(QWebEngineProfile *profile)
{
    for(auto view : qAsConst(m_views)) {
        QWebEnginePage *page = new QWebEnginePage(profile);
        page->load(view->url());
        view->setPage(page);
    }
}

WebView *WebViewTabBar::currentView()
{
    return m_views.at(currentIndex());
}

QSize WebViewTabBar::tabSizeHint(int index) const
{
    Q_UNUSED(index)
    return QSize(200, this->height());
}

void WebViewTabBar::handleCurrentChanged(int index)
{
    emit currentTabChanged(m_views.at(index));
}

void WebViewTabBar::removeTab(int index)
{
    // remove the tab data from the index
    m_views.at(index)->deleteLater();
    m_views.remove(index);

    // remove the tab from the QTabBar
    // this emits the currentTabChanged signal, so it should be done after the view is removed from the index
    QTabBar::removeTab(index);
}

void WebViewTabBar::updateTabText(WebView *view, const QString &text)
{
    int index = m_views.indexOf(view);
    setTabText(index, text);
}

void WebViewTabBar::updateVectorArrangement(int from, int to)
{
    m_views.move(from, to);
}

void WebViewTabBar::webAction(int action)
{
    currentView()->pageAction(static_cast<QWebEnginePage::WebAction>(action))->trigger();
}