#!/usr/bin/env python3
#
# Copyright (c) 2024 Stormux
# Copyright (c) 2010-2012 The Orca Team
# Copyright (c) 2012 Igalia, S.L.
# Copyright (c) 2005-2010 Sun Microsystems Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
# Boston MA  02110-1301 USA.
#
# Forked from Orca screen reader.
# Cthulhu project: https://git.stormux.org/storm/cthulhu

import gi
from gi.repository import GObject

from cthulhu import resource_manager

class DynamicApiManager():
    def __init__(self, app):
        self.app = app
        self.resourceManager = self.app.getResourceManager()
        self.api = {'Cthulhu': self.app}
    def registerAPI(self, key, value, application = '', contextName = None, overwrite = False):
        if not overwrite:
            try:
                d = self.api[application][key]
                return
            except KeyError: 
                pass
        # add profile
        try:
            d = self.api[application]
        except KeyError: 
            self.api[application]= {}
        # add dynamic API
        self.api[application][key] = value
        resourceContext = self.resourceManager.getResourceContext(contextName)
        if resourceContext:
            resourceEntry = resource_manager.ResourceEntry('api', key, value, value, key)
            resourceContext.addAPI(application, key, resourceEntry)

    def unregisterAPI(self, key,  application = '', contextName = None):
        ok = False
        try:
            del self.api[application][key]
            ok = True
        except:
            print('API Key: "{}/{}" not found,'.format(application, key))
        resourceContext = self.resourceManager.getResourceContext(contextName)
        if resourceContext:
            resourceContext.removeAPI(application, key)
        return ok
    def getAPI(self, key, application = '', fallback = True):
        # get dynamic API
        api = None

        try:
            api = self.api[application][key]
            return api
        except:
            if not fallback:
                print('API Key: "{}/{}" not found,'.format(application, key))
                return None

        # we already tried this
        if application == '':
            return api

        try:
            api = self.api[application]['']
        except:
            print('API Key: "{}/{}" not found,'.format(application, key))

        return api
