/*
 * @Author: caoxiansheng caoxiansheng@kylinos.cn
 * @Date: 2025-06-19 17:49:11
 * @LastEditors: caoxiansheng caoxiansheng@kylinos.cn
 * @LastEditTime: 2025-06-23 09:50:49
 * @FilePath: /kaiming/dbus/gdbus/KMSystemServerAdaptor.h
 * @Description: 
 */
#pragma once

#include "dbus/kmsysteminterface.h"
#include <memory>
#include <string>
#include <map>
#include <atomic>

#include "app/info/KMInfoCommand.h"
#include "app/list/KMList.h"
#include "common/KMPackageManager.h"

typedef enum
{
    KM_INSTALL_OPERATION = 0, // 安装操作
    KM_UPDATE_OPERATION,      // 升级操作
    KM_UNINSTALL_OPERATION,   // 卸载操作
} KMOperationType;

class KMSystemServerAdaptor
{
public:
    KMSystemServerAdaptor();
    ~KMSystemServerAdaptor() = default;

    gboolean install(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name, const gchar *branch = "");
    gboolean installOfflinePackage(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name, const gchar *branch = "");
    gboolean update(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name, const gchar *branch = "");
    gboolean uninstall(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name, const gchar *branch = "");
    gboolean getLocalInfomation(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name);
    gboolean getAllLocalInfo(KaimingInterface *object, GDBusMethodInvocation *invocation);
    gboolean getRemoteInfomation(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name = "");
    gboolean getRemotePackageStatus(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name, const gchar *version, const gchar *branch);
    gboolean getAllRemoteInfo(KaimingInterface *object, GDBusMethodInvocation *invocation);
    gboolean getAPPsRemoteInfo(KaimingInterface *object, GDBusMethodInvocation *invocation);
    gboolean isInstalled(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name);
    gboolean isUpgradable(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name);
    gboolean getRemoteSummary(KaimingInterface *object, GDBusMethodInvocation *invocation);

    static gboolean handleInstall(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name, const gchar *branch, gpointer user_data);
    static gboolean handleInstallOfflinePackage(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name, const gchar *branch, gpointer user_data);
    static gboolean handleUpdate(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name, const gchar *branch, gpointer user_data);
    static gboolean handleUninstall(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name, const gchar *branch, gpointer user_data);
    static gboolean handleGetLocalInfo(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name, gpointer user_data);
    static gboolean handleGetAllLocalInfo(KaimingInterface *object, GDBusMethodInvocation *invocation, gpointer user_data);
    static gboolean handleGetAllRemoteInfo(KaimingInterface *object, GDBusMethodInvocation *invocation, gpointer user_data);
    static gboolean handleGetAPPsRemoteInfo(KaimingInterface *object, GDBusMethodInvocation *invocation, gpointer user_data);
    static gboolean handleGetRemoteInfo(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name, gpointer user_data);
    static gboolean handleGetRemotePackageStatus(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name, const gchar *version, const gchar *branch, gpointer user_data);
    static gboolean handleIsInstalled(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name, gpointer user_data);
    static gboolean handleIsUpgradable(KaimingInterface *object, GDBusMethodInvocation *invocation, const gchar *name, gpointer user_data);
    static gboolean handleGetRemoteSummary(KaimingInterface *object, GDBusMethodInvocation *invocation, gpointer user_data);

private:
    bool validateInputName(const gchar *name);
    bool checkOperationRunning(std::atomic<bool> &flag);

    std::unique_ptr<KMPackageManager> m_pkgManager; // 开明包管理
    std::unique_ptr<KMInfoCommand> m_kmLocalInfo;   // 获取本地信息
    std::unique_ptr<KMList> m_kmList;               // 获取列表信息

    KMPackageManager *getPackageManager();
    KMInfoCommand *getKMInfoCommand();
    KMList *getKMList();

    std::atomic<bool> m_installRunningFlag = FALSE;
    std::atomic<bool> m_updateRunningFlag = FALSE;
    std::atomic<bool> m_uninstallRunningFlag = FALSE;
};
