#!/usr/bin/python3
#
# Cambalache Catalog Generator
#
# Copyright (C) 2024  Juan Pablo Ugarte
#
# 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; version 2 of the License.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Authors:
#   Juan Pablo Ugarte <juanpablougarte@gmail.com>
#

import gi
import os
import argparse
import json
import signal

gi.require_version("GIRepository", "3.0")
from gi.repository import Gio

repository = gi.Repository.get_default()
repository.prepend_search_path("/usr/lib/cmb_catalog_gen")
repository.prepend_library_path("/usr/lib/cmb_catalog_gen")

resource = Gio.Resource.load(os.path.join("/usr/share/cambalache", "cambalache.gresource"))
resource._register()

from cmb_catalog_gen import CmbCatalogDB

signal.signal(signal.SIGINT, signal.SIG_DFL)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Generate Cambalache catalogs from Gir library data")

    parser.add_argument("--gir", type=str, required=True, help="library Gir file")

    parser.add_argument("--output", type=str, required=True, help="Output xml filename")

    parser.add_argument("--target-gtk4", help="Target version gtk version 4.0 instead of 3.0", action="store_true")

    parser.add_argument(
        "--dependencies", metavar="T", type=str, nargs="+", help="Catalog dependencies lib-ver (gtk-4.0)", default=[]
    )

    parser.add_argument("--extra-data", type=str, help="Extra data for catalog", default=None)

    parser.add_argument("--types", metavar="T", type=str, nargs="+", help="Types to get extra metadata", default=None)

    parser.add_argument("--flag-types", metavar="T", type=str, nargs="+", help="Flag types to get extra metadata", default=None)

    parser.add_argument("--enum-types", metavar="T", type=str, nargs="+", help="Enum types to get extra metadata", default=None)

    parser.add_argument("--boxed-types", metavar="T", type=str, nargs="+", help="Boxed Types to include", default=[])

    parser.add_argument("--exclude-objects", help="Exclude objects in output", action="store_true")

    parser.add_argument("--show-property-overrides", help="Show properties pspec changes", action="store_true")

    parser.add_argument(
        "--skip-types",
        metavar="T",
        type=str,
        nargs="+",
        help="Types to avoid instantiating to get extra metadata",
        default=[],
    )

    parser.add_argument(
        "--external-catalogs",
        metavar="T",
        type=str,
        nargs="+",
        help="List of catalogs to get properties types",
        default=[],
    )

    args = parser.parse_args()

    db = CmbCatalogDB(dependencies=args.dependencies, external_catalogs=args.external_catalogs)

    tokens = os.path.basename(args.output).split("-")

    db.populate_from_gir(
        args.gir,
        libname=tokens[0],
        target_gtk4=args.target_gtk4,
        types=args.types,
        flag_types=args.flag_types,
        enum_types=args.enum_types,
        boxed_types=args.boxed_types,
        skip_types=args.skip_types,
        exclude_objects=args.exclude_objects,
    )

    # Load custom type data from json file
    if args.extra_data:
        db.populate_extra_data_from_xml(args.extra_data)

    if len(db.lib.ignored_pspecs):
        print(
            "Ignored pspecs: ",
            json.dumps(list(db.lib.ignored_pspecs), indent=2, sort_keys=True),
        )

    if len(db.lib.ignored_types):
        print(
            "Ignored types: ",
            json.dumps(list(db.lib.ignored_types), indent=2, sort_keys=True),
        )

    if len(db.lib.ignored_boxed_types):
        print(
            "Ignored boxed types: ",
            json.dumps(list(db.lib.ignored_boxed_types), indent=2, sort_keys=True),
        )

    ignored_named_icons = db.get_ignored_named_icons()
    if ignored_named_icons:
        print(
            'Possible icon name properties (You need to specify type="CmbIconName"): ',
            json.dumps(ignored_named_icons, indent=2, sort_keys=True),
        )

    translatable_properties = db.get_possibly_translatable_properties()
    if translatable_properties:
        print(
            'Possible translatable properties (You need to specify translatable="True"): ',
            json.dumps(translatable_properties, indent=2, sort_keys=True),
        )

    if args.show_property_overrides:
        overrides = db.get_property_overrides()
        if overrides:
            print(
                "Properties Overrides",
                json.dumps(overrides, indent=2, sort_keys=True),
            )

    db.dump(args.output)
