#include "gui\messagedialog.h"
#include "ui_messagedialog.h"
#include <QPixmap>
#include <QBitmap>
#include <QMouseEvent>
#include "legacyapp.h"
#include <QTextCodec>
using namespace LOTRO_DAT;

MessageDialog::MessageDialog(LegacyApp *app_, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MessageDialog), app(app_)
{
    ui->setupUi(this);
    setModal(true);
    setWindowTitle("Выполнение операции...");
    QPixmap background(":/assets/backgrounds/process_dialog.png");

    QPixmap current_bg = background.scaled(width(), height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    QBitmap current_mask = current_bg.mask();
    setMask(current_mask);

    QPalette palette;
    palette.setBrush(QPalette::Window, current_bg);
    setPalette(palette);

    loader.setFileName(":/assets/backgrounds/loading-gear.gif");
    ui->loader->setMovie(&loader);
    loader.setScaledSize(ui->loader->size());
    loader.start();

    uiUpdateTimer.setInterval(30);
    connect(&uiUpdateTimer, &QTimer::timeout, this, &MessageDialog::updateUI);
    uiUpdateTimer.start();
}

void MessageDialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        dragPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

void MessageDialog::updateUI()
{
    if (!qApp)
        return;

    std::string debug_msg = app->client_local_dat.GetStatusModule().GetDebugMessage();
    QTextCodec *codec = QTextCodec::codecForName("UTF8");
    QByteArray ba(debug_msg.c_str());
    QString dmsg = codec->toUnicode(ba);

    int percentage = app->client_local_dat.GetStatusModule().GetPercentage();

    DatStatus::DAT_STATUS status = app->client_local_dat.GetStatusModule().GetStatus();
    QString title = "";

    switch (status) {
    case DatStatus::E_INITIALISING:
        title = "Инициализация";
        break;
    case DatStatus::E_EXTRACTING:
        title = "Извлечение данных";
        break;
    case DatStatus::E_PATCHING:
        title = "Применение патча";
        break;
    case DatStatus::E_COMMITING:
        title = "Фиксация данных";
        break;
    case DatStatus::E_BACKUP_CREATING:
        title = "Создание резервной копии";
        break;
    case DatStatus::E_BACKUP_REMOVING:
        title = "Удаление резервной копии";
        break;
    case DatStatus::E_GATHERING_INFO:
        title = "Сбор информации ядра";
        break;
    case DatStatus::E_FREE:
        title = "Свободен";
        break;
    default:
        title = "Неизвестная операция";
        break;
    }

    if (status == DatStatus::E_FREE)
        hide();
    else
        show();

    ui->title->setText(title);
    ui->progressBar->setValue(percentage);
    ui->debug_info->setText(dmsg);
}

void MessageDialog::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton) {
        move(event->globalPos() - dragPosition);
        app->window.move(event->globalPos() - dragPosition +
                         QPoint((width() - app->window.width()) / 2, (height() - app->window.height()) / 2)
                         );
        event->accept();
    }
}

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