#include "hubmainwidget.h"
#include "ui_hubmainwidget.h"
#include "QtNetwork/QNetworkReply"

HubMainWidget::HubMainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::HubMainWidget)
{
    ui->setupUi(this);
    connect(&sysTimer, &QTimer::timeout, this, [this]()
    {
        ui->dateTimeEdit->setTime(QTime::currentTime());
        ui->dateTimeEdit->setDate(QDate::currentDate());
    });
    sysTimer.start(1000);

    getModules();
}

bool HubMainWidget::getModules()
{
    dllInfo UnionCom;
    UnionCom.libName = "Терминал CAN, RS, Modbus";
    UnionCom.fileName = "UnionCom";
    UnionCom.initFuncName = "init";
    UnionCom.adr = "https://git.arktika.cyou/Tenocha/UnionComDLL";
    UnionCom.downloadAdr = "https://git.arktika.cyou/Tenocha/UnionComDLL/archive/Release.zip";
    connectLibrary(UnionCom);

    dllInfo M3KTE_TERM;
    M3KTE_TERM.libName = "МЗКТЕ Терминал";
    M3KTE_TERM.fileName = "M3KTE_TERM";
    M3KTE_TERM.initFuncName = "init";
    M3KTE_TERM.adr = "https://git.arktika.cyou/Tenocha/M3KTE_TERM";
    M3KTE_TERM.downloadAdr = "https://git.arktika.cyou/Tenocha/M3KTE_TERM/releases/download/Pre-release/M3KTETERM.rar";
    connectLibrary(M3KTE_TERM);

    dllInfo LineRingerLib;
    LineRingerLib.libName = "Поиск modbus устройств";
    LineRingerLib.fileName = "LineRingerLib32Bit";
    LineRingerLib.initFuncName = "init";
    LineRingerLib.adr = "https://git.arktika.cyou/Tenocha/LineRingerDLL";
    LineRingerLib.downloadAdr = "https://git.arktika.cyou/Tenocha/LineRingerDLL/archive/Release.zip";
    connectLibrary(LineRingerLib);

    return true;
}

bool HubMainWidget::connectLibrary(dllInfo modul)
{
    QLibrary myLib(modul.fileName);
    myLib.load();
    //QString check = myLib.errorString();
    unsigned newRow = ui->libraryTable->rowCount();
    ui->libraryTable->insertRow(newRow);
    ui->libraryTable->setItem(newRow, 0, new QTableWidgetItem(modul.libName));
    ui->libraryTable->setItem(newRow, 1, new QTableWidgetItem(modul.fileName));
    QTableWidgetItem *item = new QTableWidgetItem();
    item->setText("");
    QLabel *url = new QLabel();
    url->setText(tr("<a href=\"%1\">Source</a>").arg(modul.adr.toString()));
    url->setTextFormat(Qt::RichText);
    url->setTextInteractionFlags(Qt::TextBrowserInteraction);
    url->setOpenExternalLinks(true);
    ui->libraryTable->setCellWidget(newRow, 3, url);
    //ui->libraryTable->setItem(newRow, 2, new QTableWidgetItem(modul.adr));
    if(myLib.isLoaded())
    {
        typedef QWidget* (*DLL_Init_Function)(QWidget*);
        DLL_Init_Function DLL_Init = (DLL_Init_Function) myLib.resolve(modul.initFuncName.toUtf8());
        if(DLL_Init)
        {
            item->setCheckState(Qt::Checked);
            ui->libraryTable->setItem(newRow, 2, item);
            modul.widget = DLL_Init(nullptr);
            connectedModules.append(modul);
            ui->appBox->addItem(modul.libName);
            ui->libraryTable->resizeColumnsToContents();
            return true;
        }
    }
    else
    {
        item->setCheckState(Qt::Unchecked);
        ui->libraryTable->setItem(newRow, 2, item);
        connectedModules.append(modul);
        ui->appBox->addItem(modul.libName);
        ui->libraryTable->resizeColumnsToContents();
    }
    return false;
}

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


void HubMainWidget::on_openOrDownloadButton_clicked()
{
    if(ui->appBox->count()!=0)
    {
        if(connectedModules.at(ui->appBox->currentIndex()).widget != nullptr)
        {
            connectedModules.at(ui->appBox->currentIndex()).widget->show();
        }
        else
        {
            QDesktopServices::openUrl(connectedModules.at(ui->appBox->currentIndex()).adr);
        }
    }
}

void HubMainWidget::on_appBox_currentIndexChanged(int index)
{
    if(connectedModules.at(index).widget == nullptr)
    {
        ui->openOrDownloadButton->setText("Скачать");
    }
    else
    {
        ui->openOrDownloadButton->setText("Открыть");
    }
}
