#include "statuswidget.h"
#include "ui_statuswidget.h"
#include "widgets/mainwindow.h"
#include "models/patchdownloader.h"
#include "models/lotrodatmanager.h"

#include <QDesktopServices>
#include <QUrl>
#include <QDebug>
#include <QMessageBox>

StatusWidget::StatusWidget(QSettings* settings, PatchDownloader* patch_downloader, LotroDatManager* lotro_dat_manager, QWidget *parent) :
    QWidget(parent), lotro_manager(lotro_dat_manager),
    ui(new Ui::StatusWidget)
{

    ui->setupUi(this);
    connect(ui->announcements_list, &NewsListWidget::newsUpdated, this, &StatusWidget::invokeUpdateFontSize);
    connect(ui->server_status_flag, &StatusFlagWidget::flagIdChanged, this, &StatusWidget::changeCentralWidget);
    ui->status_widget->hide();

    patch_updater = patch_downloader;
    connect(patch_updater, &PatchDownloader::downloadStarted, this, &StatusWidget::onPatchDownloaderStarted, Qt::QueuedConnection);
    connect(patch_updater, &PatchDownloader::progressChanged, this, &StatusWidget::onPatchDownloaderProgressChanged, Qt::QueuedConnection);
    connect(patch_updater, &PatchDownloader::downloadCompleted, this, &StatusWidget::onPatchDownloaderFinished, Qt::QueuedConnection);
    ui->bottom_widget->hide();
    ui->tooltip->hide();

    connect(ui->weekly_code, &WeeklyCodeWidget::mouseEntered, this, &StatusWidget::showTooltip);
    connect(ui->weekly_code, &WeeklyCodeWidget::mouseLeaved, this, &StatusWidget::hideTooltip);
    connect(ui->server_status_flag, &StatusFlagWidget::mouseEntered, this, &StatusWidget::showTooltip);
    connect(ui->server_status_flag, &StatusFlagWidget::mouseLeaved, this, &StatusWidget::hideTooltip);
}

StatusWidget::~StatusWidget()
{
    delete ui;
}

void StatusWidget::showTooltip(QString text)
{
    ui->tooltip->updateText(text);
    ui->tooltip->show();
}

void StatusWidget::hideTooltip()
{
    ui->tooltip->hide();
}

void StatusWidget::onPatchDownloaderStarted()
{
    qDebug() << "Status widget received DownloadStarted signal!";
    ui->bottom_widget->show();
    ui->process_label->setText("Загрузка обновлений патчей...");
    ui->progressBar->setValue(0);
}

void StatusWidget::onPatchDownloaderFinished()
{
    ui->process_label->setText("Загрузка обновлений патчей завершена...");
    ui->progressBar->setValue(100);
    ui->bottom_widget->hide();
}

void StatusWidget::onPatchDownloaderProgressChanged(quint64 bytesDownloaded, quint64 bytesTotal, QString download_speed_formatted, QString elapsed_time_formatted)
{
    ui->bottom_widget->show();
    ui->process_label->setText("Загрузка " + download_speed_formatted + ". Загружено "
                               + Downloader::getSizeFormatted(bytesDownloaded) + " из " + Downloader::getSizeFormatted(bytesTotal)
                               + " (" + QString::number(bytesDownloaded * 100 / bytesTotal) + "%) ");
                               //+ "\nОставшееся время: " + elapsed_time_formatted);

    ui->progressBar->setValue(bytesDownloaded * 100 / bytesTotal + 5);
}

void StatusWidget::changeCentralWidget()
{
    if (ui->announcements_widget->isHidden()) {
        ui->announcements_widget->show();
        ui->status_widget->hide();
    } else {
        ui->announcements_widget->hide();
        ui->status_widget->show();
    }
}

void StatusWidget::invokeUpdateFontSize()
{
    MainWindow* window = qobject_cast<MainWindow*>(parentWidget()->parentWidget()->parentWidget());
    window->changeFontSizeRecursive(100, this);
}

void StatusWidget::on_site_link_button_clicked()
{
    QDesktopServices::openUrl(QUrl("http://translate.lotros.ru/"));
}

void StatusWidget::on_forum_link_button_clicked()
{
    QDesktopServices::openUrl(QUrl("http://lotros.ru/"));
}

void StatusWidget::on_guides_link_button_clicked()
{
    QDesktopServices::openUrl(QUrl("http://translate.lotros.ru/guides"));
}

void StatusWidget::on_addons_link_button_clicked()
{
    QDesktopServices::openUrl(QUrl("http://translate.lotros.ru/mathoms"));
}

void StatusWidget::on_bugreport_link_button_clicked()
{
    QDesktopServices::openUrl(QUrl("http://translate.lotros.ru/bugs/add"));
}

void StatusWidget::on_donate_link_button_clicked()
{
    QDesktopServices::openUrl(QUrl("http://translate.lotros.ru/donate"));
}

void StatusWidget::on_game_button_clicked()
{
    MainWindow* window = qobject_cast<MainWindow*>(parentWidget()->parentWidget()->parentWidget());
    window->showChooseVersionDialog();
}
