/*
 * 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_CORETEXTEMBEDDINGSERVICE_H
#define SERVICES_CORETEXTEMBEDDINGSERVICE_H

#include <gio/gio.h>

#include <memory>
#include <set>

#include "coretextembeddingserviceglue.h"
#include "engineselector/engineselector.h"

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

    void stop();

private:
    void exportSkeleton();
    void unexportSkeleton();
    int generateRandomSessionId();

    bool initEngine(const std::string &engineName, gint sessionId);

    bool parseTextEmbeddingInput(const std::string &jsonInput, std::string &text, int &sessionId) const;

    static bool onHandleInitEngine(AiRuntimeCoreTextEmbeddingService *delegate, GDBusMethodInvocation *invocation,
                                   const gchar *inputJson, gpointer userData);
    static bool onHandleEmbeddingText(AiRuntimeCoreTextEmbeddingService *delegate, GDBusMethodInvocation *invocation,
                                      const gchar *inputJson, gpointer userData);
    static bool onHandleGetModelInfo(AiRuntimeCoreTextEmbeddingService *delegate, GDBusMethodInvocation *invocation,
                                     int sessionId, gpointer userData);

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

private:
    bool isExported_ = false;
    AiRuntimeCoreTextEmbeddingService *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>>
        sessionIdAndTextEmbeddingEngineMap;
    sessionIdAndTextEmbeddingEngineMap textEmbeddingEngineMap_;
    std::vector<gulong> handlerIds_;
};

#endif
