/*
 * 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_COREVISIONSERVICE_H
#define SERVICES_COREVISIONSERVICE_H

#include <gio/gio.h>
#include <kylin-ai/aiengine.h>

#include <map>
#include <memory>
#include <set>
#include <string>

#include "corevisionserviceglue.h"
#include "engine/aienginepluginmanager.h"
#include "engineselector/engineselector.h"
#include "textrecognition.h"

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

    void stop();

private:
    void exportSkeleton();
    void connectSignal();
    void unexportSkeleton();
    int generateRandomSessionId();
    void releaseRandomSessionId(int sessionId);
    bool isTextRecognitionSessionIdValid(int sessionId);
    DeployType parseDeployType(const std::string& typeStr);

    const std::unique_ptr<ai_engine::core_ai::vision::AbstractTextRecognitionEngine> selectTextRecognitionEngine();

    static void completeRecognizeTextFromImageFile(const TextRecognitionResult& result,
                                                   AiRuntimeCoreVisionService* service,
                                                   GDBusMethodInvocation* invocation);
    static void completeRecognizeTextFromImageData(const TextRecognitionResult& result,
                                                   AiRuntimeCoreVisionService* service,
                                                   GDBusMethodInvocation* invocation);

    static bool onHandleInit(AiRuntimeCoreVisionService* service, GDBusMethodInvocation* invocation,
                             const gchar* modelconfig, gpointer userData);

    static bool onHandleDestroyTextRecognition(AiRuntimeCoreVisionService* service, GDBusMethodInvocation* invocation,
                                               gint sessionId, gpointer userData);

    static bool onHandleTextRecognitionFromImageFile(AiRuntimeCoreVisionService* service,
                                                     GDBusMethodInvocation* invocation, int sessionId,
                                                     const gchar* imageFilePath, gpointer userData);

    static bool onHandleTextRecognitionFromImageData(AiRuntimeCoreVisionService* service,
                                                     GDBusMethodInvocation* invocation, int sessionId,
                                                     const gchar* imageData, unsigned int imageDataLength,
                                                     gpointer userData);

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

private:
    bool isExported_ = false;
    AiRuntimeCoreVisionService* delegate_ = nullptr;

    GDBusConnection& connection_;

    EngineSelector engineSelector_;

    std::shared_ptr<ai_engine::core_ai::vision::AbstractImageProcessEngine> visionEngine_{nullptr};

    std::set<int> sessionIdSet_;
    std::map<int, std::shared_ptr<TextRecognition>> textRecognitions_;
    std::vector<gulong> handlerIds_;
};

#endif