#include "lotroprogresswidget.h"
#include "ui_lotroprogresswidget.h"

#include <QTextCodec>
#include "models/lotrodatmanager.h"

LotroProgressWidget::LotroProgressWidget(LotroDatManager* mgr, QSettings* settings, QWidget *parent) :
    QWidget(parent), lotro_manager(mgr), settings(settings), ui_update_timer(new QTimer(this)),
    ui(new Ui::LotroProgressWidget)
{
    ui->setupUi(this);
    ui_update_timer->setInterval(30);
    connect(ui_update_timer, &QTimer::timeout, this, &LotroProgressWidget::updateUI);
    ui_update_timer->start();
}

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

void LotroProgressWidget::updateUI()
{
    if (!qApp || isHidden())
        return;

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

    int percentage = lotro_manager->getStatusModule()->GetPercentage();

    LOTRO_DAT::DatStatus::DAT_STATUS status = lotro_manager->getStatusModule()->GetStatus();
    QString title = "";

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

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