#include "appwidget.h"
#include <QPluginLoader>
#include <QLibrary>
#include <QJsonObject>
#include <QString>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>

#include <QDebug>

AppWidget::AppWidget(QWidget *parent) : QWidget(parent)
  , m_pluginInterface(nullptr)
{
    resize(800, 600);
    m_pluginInterface = new TabletDesktopBackend();
    if(m_pluginInterface)
    {
        connect(m_pluginInterface, &TabletDesktopBackend::filechanged, [](){
            qDebug() << "get signal from plugin ! ! !     filechanged";
        });
    }
    qDebug() << "Internet: " << m_pluginInterface->getAppIdListAccordingCategory(Categories::Network);
    qDebug() << "All_APP: " << m_pluginInterface->getAllAppIdList();

    itemInfo = m_pluginInterface->getAllItemData();

    initUI();

    // 测试修改任务栏应用数据的接口
    QMap<quint32, QList<quint32>> panelData = m_pluginInterface->getAllScrollSetsData();
    panelData[0].removeLast();;
    panelData[0]={36, 52, 10, 16};
    m_pluginInterface->setScrollSetData(panelData.firstKey(), panelData.value(panelData.firstKey()));
}

AppWidget::~AppWidget()
{
    if(m_pluginInterface) {
        m_pluginInterface->deleteLater();
        m_pluginInterface = nullptr;
    }
}

void AppWidget::initUI()
{
    QVBoxLayout *main_layout = new QVBoxLayout(this);
    main_layout->setContentsMargins(8, 20, 8, 20);

    QHBoxLayout *deleteLayout = new QHBoxLayout;
    QLineEdit *itemDeleteLineEdit = new QLineEdit();
    itemDeleteLineEdit->setMinimumWidth(300);
    itemDeleteLineEdit->setPlaceholderText("请输入您想删除的应用ID：");
    int left, top, right, bottom;
    itemDeleteLineEdit->getTextMargins(&left, &top, &right, &bottom);
    qDebug() << "TextMargins:" << left << top << right << bottom;

    QPushButton *itemDeleteButton = new QPushButton("删除");
    deleteLayout->addStretch();
    deleteLayout->addWidget(itemDeleteLineEdit);
    deleteLayout->addStretch();
    deleteLayout->addWidget(itemDeleteButton);
    deleteLayout->addStretch();

    QLineEdit *setsIdLineEdit = new QLineEdit();
    setsIdLineEdit->setPlaceholderText("setId");
    QLineEdit *setsNameLineEdit = new QLineEdit();
    setsNameLineEdit->setPlaceholderText("set 名字");
    QHBoxLayout *hlayout_1 = new QHBoxLayout();
    hlayout_1->addWidget(setsIdLineEdit);
    hlayout_1->addWidget(setsNameLineEdit);
    QPushButton *modify_set_button = new QPushButton("修改");
    QPushButton *clear_button = new QPushButton("清空");
    QPushButton *getName_set_button = new QPushButton("获取");
    QHBoxLayout *hlayout_2 = new QHBoxLayout();
    hlayout_2->addWidget(modify_set_button);
    hlayout_2->addWidget(clear_button);
    hlayout_2->addWidget(getName_set_button);
    connect(modify_set_button, &QPushButton::clicked, [=](){
        if(!setsNameLineEdit->text().isEmpty() && !setsIdLineEdit->text().isEmpty()) {
            if(m_pluginInterface->modifySetName(setsIdLineEdit->text().toInt(), setsNameLineEdit->text()))
            {
                qDebug() << "修改 setName 成功!";
            }
            else
            {
                qDebug() << "修改 setName 失败!";
            }
        }
        else
            qDebug() << "请输入 setId 和 setName 后再点击修改按钮";
    });

    connect(clear_button, &QPushButton::clicked, [=](){
        setsNameLineEdit->clear();
        setsIdLineEdit->clear();
    });

    connect(getName_set_button, &QPushButton::clicked, [=](){
        setsNameLineEdit->clear();
        setsNameLineEdit->setText(itemInfo[setsIdLineEdit->text().toUInt()]->getName());
    });

    main_layout->addLayout(deleteLayout);
    main_layout->addSpacing(20);
    main_layout->addLayout(hlayout_1);
    main_layout->addLayout(hlayout_2);
    main_layout->addStretch();

    connect(itemDeleteButton, &QPushButton::clicked, [&](){
        quint32 id = itemDeleteLineEdit->text().toUInt();
        qDebug() << "ID: " << id;
        deleteID(id);
    });
}

void AppWidget::getSignal(){
    qDebug() << "get signal from plugin ! ! !";
}

void AppWidget::deleteID(int id){
//    m_pluginInterface->removeLauncherItem(id);
}
