/*
 * Copyright 2024 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 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 <https://www.gnu.org/licenses/>.
 */

#ifndef SERVICES_COREIMAGEEMBEDDINGSERVICE_H
#define SERVICES_COREIMAGEEMBEDDINGSERVICE_H

#include <gio/gio.h>

#include <memory>
#include <set>

#include "coreimageembeddingserviceglue.h"
#include "engineselector/engineselector.h"

class CoreImageEmbeddingService : public std::enable_shared_from_this<CoreImageEmbeddingService> {
public:
    CoreImageEmbeddingService(GDBusConnection &connection, EngineSelector &engineSelector);

    ~CoreImageEmbeddingService();

    void stop();

private:
    void exportSkeleton();
    void unexportSkeleton();
    int generateRandomSessionId() const;
    bool initEngine(const std::string &engineName, gint sessionId);

    bool parseTextEmbeddingInput(const std::string &jsonInput, std::string &text, int &sessionId) const;
    bool parseImageEmbeddingInput(const std::string &jsonInput, std::string &filePath, int &sessionId) const;
    bool parseBase64ImageEmbeddingInput(const std::string &jsonInput, std::string &base64ImageData,
                                        int &sessionId) const;

    static bool onHandleInitEngine(AiRuntimeCoreImageEmbeddingService *delegate, GDBusMethodInvocation *invocation,
                                   const gchar *inputJson, gpointer userData);

    static bool onHandleEmbeddingText(AiRuntimeCoreImageEmbeddingService *delegate, GDBusMethodInvocation *invocation,
                                      const gchar *inputJson, gpointer userData);

    static bool onHandleEmbeddingImageFile(AiRuntimeCoreImageEmbeddingService *delegate,
                                           GDBusMethodInvocation *invocation, const gchar *inputJson,
                                           gpointer userData);

    static bool onHandleEmbeddingBase64Image(AiRuntimeCoreImageEmbeddingService *delegate,
                                             GDBusMethodInvocation *invocation, const gchar *inputJson,
                                             gpointer userData);
    static bool onHandleGetModelInfo(AiRuntimeCoreImageEmbeddingService *delegate, GDBusMethodInvocation *invocation,
                                     int sessionId, gpointer userData);

private:
    std::shared_ptr<CoreImageEmbeddingService> getSharedObject() { return shared_from_this(); }

private:
    bool isExported_ = false;
    AiRuntimeCoreImageEmbeddingService *delegate_ = nullptr;
    GDBusConnection &connection_;
    EngineSelector &engineSelector_;

    static const std::string objectPath_;

    static std::set<int> sessionIdSet;
    typedef std::map<gint, std::unique_ptr<ai_engine::core_ai::embedding::AbstractEmbeddingEngine>>
        sessionIdAndImageEmbeddingEngineMap;
    sessionIdAndImageEmbeddingEngineMap imageEmbeddingEngineMap_;
    std::vector<gulong> handlerIds_;
};

#endif
