#!/usr/bin/env python3


# Libervia: an XMPP client
# Copyright (C) 2009-2026 Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from libervia.backend.bridge.bridge_constructor import base_constructor


class DbusConstructor(base_constructor.Constructor):
    NAME = "dbus"
    CORE_TEMPLATE = "dbus_core_template.py"
    CORE_DEST = "dbus_bridge.py"
    CORE_FORMATS = {
        "methods_declarations": """\
        Method('{name}', arguments='{sig_in}', returns='{sig_out}'),""",
        "methods": """\
    def dbus_{name}(self, {args}):
        {debug}return self._callback("{name}", {args_no_default})\n""",
        "signals_declarations": """\
        Signal('{name}', '{sig_in}'),""",
        "signals": """\
    def {name}(self, {args}):
        self._obj.emitSignal("{name}", {args})\n""",
    }

    FRONTEND_TEMPLATE = "dbus_frontend_template.py"
    FRONTEND_DEST = CORE_DEST
    FRONTEND_FORMATS = {
        "methods": """\
    def {name}(self, {args}):
        {debug}return self.call("{name}", {args_result})\n""",
        "async_methods": """\
    async def {name}(self, {args}):
        {debug}return await self.call("{name}", {args_result})\n""",
        "twisted_methods": """\
    def {name}(self, {args}):
        {debug}return self.call("{name}", {args_result})\n""",
    }

    def core_completion_signal(self, completion, function, default, arg_doc, async_):
        completion["category"] = completion["category"].upper()
        completion["body"] = (
            "pass"
            if not self.args.debug
            else 'log.debug ("{}")'.format(completion["name"])
        )

    def core_completion_method(self, completion, function, default, arg_doc, async_):
        completion.update(
            {
                "debug": (
                    ""
                    if not self.args.debug
                    else f'log.debug ("{completion["name"]}")\n{8 * " "}'
                )
            }
        )

    def frontend_completion_method(self, completion, function, default, arg_doc, async_):
        completion.update(
            {
                "debug": (
                    ""
                    if not self.args.debug
                    else 'log.debug ("%s")\n%s' % (completion["name"], 8 * " ")
                ),
                "args_result": self.get_arguments(function["sig_in"], name=arg_doc),
            }
        )
