#!/usr/bin/env python3

# Graphs
# Plot and manipulate data
#
# https://apps.gnome.org/Graphs/
# https://gitlab.gnome.org/World/Graphs
#
# Copyright 2022 - 2026 The Graphs Developers
#
# 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 <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Main graphs module."""
if __name__ == "__main__":
    import gi

    gi.require_version("Adw", "1")
    gi.require_version("Gtk", "4.0")
    gi.require_version("Graphs", "1")

    import asyncio
    import os
    import signal
    import sys

    from gi.events import GLibEventLoopPolicy
    asyncio.set_event_loop_policy(GLibEventLoopPolicy())

    graph_path_dir = "/usr/share/graphs"
    if os.environ.get("GRAPHS_DEVEL_PATH"):
        graph_path_dir = os.environ.get("GRAPHS_DEVEL_PATH")
    sys.path.insert(1, graph_path_dir)
    signal.signal(signal.SIGINT, signal.SIG_DFL)

    def on_startup(_application):
        """Handle Application setup."""
        import logging
        logging.basicConfig(
            format="%(levelname)s: %(message)s",
            level=logging.DEBUG if "False" == "True" else logging.INFO,
        )
        logging.getLogger("matplotlib.font_manager").disabled = True
        logging.debug("Begin Application startup")

        import gettext
        import locale
        localedir = "/usr/share/locale"
        if os.environ.get("GRAPHS_OVERRIDE_LOCALEDIR"):
            localedir = os.environ.get("GRAPHS_OVERRIDE_LOCALEDIR")
        locale.bindtextdomain("graphs", localedir)
        locale.textdomain("graphs")
        gettext.bindtextdomain("graphs", localedir)
        gettext.textdomain("graphs")

        from matplotlib import font_manager
        _ = gettext.gettext
        for f in font_manager.findSystemFonts(fontpaths=None, fontext="ttf"):
            try:
                font_manager.fontManager.addfont(f)
            except RuntimeError:
                logging.warning(_("Could not load {font}").format(font=f))

        from graphs import scales
        scales.register_scales()

        from graphs.file_import import DataImporter
        from graphs.item import ItemFactory
        from graphs.python_helper import PythonHelper
        from graphs.styles import StyleManager
        PythonHelper()
        StyleManager()
        DataImporter()
        ItemFactory()

    from gi.repository import Graphs
    application = Graphs.Application.new()
    application.connect("startup", on_startup)
    sys.exit(application.run(sys.argv))
