from gettext import gettext as _
from typing import override

from gi.repository import Adw

from .config import APP_ID, PREFIX, VERSION
from .ui.window import Window


class Application(Adw.Application):
    """The main application."""

    @override
    def do_startup(self) -> None:
        Adw.Application.do_startup(self)

        self.add_action_entries(  # type: ignore[no-untyped-call]
            (
                ("about", lambda *_: self._present_about_dialog()),
                ("quit", lambda *_: self.quit()),
            )
        )
        self.set_accels_for_action("app.quit", ("<Control>q",))

    @override
    def do_activate(self) -> None:
        window = self.props.active_window or Window(application=self)
        window.present()

    def _present_about_dialog(self) -> None:
        dialog = Adw.AboutDialog.new_from_appdata(
            f"{PREFIX}/{APP_ID}.metainfo.xml", VERSION
        )
        # Translators: Replace "translator-credits" with your name/username,
        # and optionally a URL or an email in <me@example.org> format.
        dialog.props.translator_credits = _("translator-credits")
        dialog.props.developers = [
            "Jamie Gravendeel https://jamie.garden",
            "kramo https://kramo.page",
        ]
        dialog.props.artists = [
            "kramo https://kramo.page",
            "Jakub Steiner https://jimmac.eu",
            "Tobias Bernard https://tobiasbernard.com",
        ]
        dialog.props.copyright = "© 2024 Jamie Gravendeel"
        dialog.present(self.props.active_window)
