#include "adddesktop.h"
#include <QTextStream>
#include "ui_adddesktop.h"
#include <fstream>

AddDesktop::AddDesktop(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddDesktop)
{
    ui->setupUi(this);

    // Audio:音频应用程序,如播放器、编辑器等。
    // Video:视频应用程序,如播放器、编辑器等。
    // Development:开发工具,如IDE、编辑器等。
    // Education:教育类应用程序,如学习工具、教育游戏等。
    // Game:游戏应用程序。
    // Graphics:图形设计和编辑工具。
    // Office:办公软件,如文档编辑器、电子表格、演示文稿等。
    // Science:科学工具和应用程序,如数学、物理学、化学工具等。
    // Settings:系统设置工具。
    // System:系统工具,如文件管理器、终端模拟器等。
    // Utility:实用工具,如压缩软件、文本编辑器等。

    ui->cbCategories->addItem("Audio");
    ui->cbCategories->addItem("Video");
    ui->cbCategories->addItem("Development");
    ui->cbCategories->addItem("Education");
    ui->cbCategories->addItem("Game");
    ui->cbCategories->addItem("Graphics");
    ui->cbCategories->addItem("Office");
    ui->cbCategories->addItem("Science");
    ui->cbCategories->addItem("Settings");
    ui->cbCategories->addItem("Utility");
    ui->cbCategories->setCurrentIndex(ui->cbCategories->count() - 1);

    connect(ui->cbIsCustomIco, &QCheckBox::toggled, this, &AddDesktop::checkchange);
    connect(ui->btnSelectBinary, &QPushButton::clicked, this, [=](){
        MainWidget::SelectFile(this, ui->edBinary, "请选择Qt二进制文件", "所有文件 (*)");
    });
    connect(ui->btnSelectIco, &QPushButton::clicked, this, [=](){
        MainWidget::SelectFile(this, ui->edIco, "请选择ico图标文件", "图标文件(*.ico);;所有文件 (*)");
    });
    connect(ui->btnInstall, &QPushButton::clicked, this, &AddDesktop::install_to_desktop);
}

void AddDesktop::checkchange()
{
    if (ui->cbIsCustomIco->isChecked()) {
        ui->edIco->setEnabled(false);
        ui->btnSelectIco->setEnabled(false);
    }
    else {
        ui->edIco->setEnabled(true);
        ui->btnSelectIco->setEnabled(true);
    }
}

void AddDesktop::install_to_desktop()
{
    QFile file("://resource/desktop.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        return;
    }
    QTextStream stream(&file);
    QString content = stream.readAll();
    file.close();
    std::string ct = content.toStdString();
    std::string tool_name = fs::path(ui->edBinary->text().trimmed().toStdString()).filename().string();
    boost::replace_all(ct, "re_name", tool_name);
    boost::replace_all(ct, "re_describe", tool_name);
    boost::replace_all(ct, "re_path", ui->edBinary->text().trimmed().toStdString());
    boost::replace_all(ct, "re_icon", ui->edIco->text().trimmed().toStdString());
    boost::replace_all(ct, "re_category", ui->cbCategories->currentText().toStdString());

    fs::path tmp = fs::path(QDir::homePath().toStdString()).append(".config");
    if (!fs::exists(tmp)) {
        fs::create_directories(tmp);
    }
    tmp.append(tool_name + ".desktop");;
    std::ofstream out(tmp.string(), std::ios::out);
    if (!out.is_open()) {
        return;
    }
    out << ct;
    out.close();
    MainWidget::message(this, "完成");
    std::string cp_cmd("pkexec cp " + tmp.string() + " /usr/share/applications");
    system(cp_cmd.c_str());
}

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