/*
 * This file is part of system-settings
 *
 * Copyright (C) 2025 UBports Foundation
 *
 * Contact: Alfred Neumayer <dev.beidl@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "printing-plugin.h"

#include <QDebug>
#include <QFile>
#include <QProcessEnvironment>
#include <LomiriSystemSettings/ItemBase>
#include <glib.h>

using namespace LomiriSystemSettings;

class PrintingItem: public ItemBase
{
    Q_OBJECT

public:
    explicit PrintingItem(const QVariantMap &staticData, QObject *parent = 0);
    void setVisibility(bool visible);
};


PrintingItem::PrintingItem(const QVariantMap &staticData, QObject *parent):
    ItemBase(staticData, parent)
{
    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    if (env.contains(QLatin1String("LSS_SHOW_ALL_UI"))) {
        QString showAllS = env.value("LSS_SHOW_ALL_UI", QString());

        if(!showAllS.isEmpty()) {
            setVisibility(true);
            return;
        }
    }

    // Display a setup screen on Ubuntu Touch, should CUPS not already be installed
    bool showUi = QFile::exists("/run/cups/cups.sock");
    const auto distro_id = std::string(g_get_os_info("ID"));
    const auto distro_variant = std::string(g_get_os_info("VARIANT_ID"));
    qDebug() << "Distribution:" << QString::fromStdString(distro_id)
                                << QString::fromStdString(distro_variant);
    if (distro_id == "ubuntu" && distro_variant == "touch") {
        showUi = true;
    }

    setVisibility(showUi);
}

void PrintingItem::setVisibility(bool visible)
{
    setVisible(visible);
}

ItemBase *PrintingPlugin::createItem(const QVariantMap &staticData,
                                 QObject *parent)
{
    return new PrintingItem(staticData, parent);
}

#include "printing-plugin.moc"
