#
# Gramps Web API - A RESTful API for the Gramps genealogy program
#
# Copyright (C) 2020      David Straub
#
# 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 <https://www.gnu.org/licenses/>.
#

"""Constants for the web API."""

import shutil

import gramps.gen.lib as lib
from gramps.gen.plug import CATEGORY_DRAW, CATEGORY_GRAPHVIZ, CATEGORY_TEXT
from pkg_resources import resource_filename

from ._version import __version__ as VERSION

# files
TEST_CONFIG = resource_filename("gramps_webapi", "data/test.cfg")
TEST_AUTH_CONFIG = resource_filename("gramps_webapi", "data/test_auth.cfg")
TEST_EXAMPLE_GRAMPS_CONFIG = resource_filename(
    "gramps_webapi", "data/example_gramps.cfg"
)
TEST_EXAMPLE_GRAMPS_AUTH_CONFIG = resource_filename(
    "gramps_webapi", "data/example_gramps_auth.cfg"
)

# environment variables
ENV_CONFIG_FILE = "GRAMPS_API_CONFIG"

# API endpoints
API_PREFIX = "/api"

# Sex identifiers
SEX_MALE = "M"
SEX_FEMALE = "F"
SEX_UNKNOWN = "U"

# Primary Gramps objects
# This is used to identify the only ones that the keys and skipkeys
# filters should operate on
PRIMARY_GRAMPS_OBJECTS = {
    "Person": lib.Person,
    "Family": lib.Family,
    "Event": lib.Event,
    "Place": lib.Place,
    "Citation": lib.Citation,
    "Source": lib.Source,
    "Repository": lib.Repository,
    "Media": lib.Media,
    "Note": lib.Note,
    "Tag": lib.Tag,
}

# To map endpoints to Gramps objects
GRAMPS_NAMESPACES = {
    "people": "Person",
    "families": "Family",
    "events": "Event",
    "places": "Place",
    "citations": "Citation",
    "sources": "Source",
    "repositories": "Repository",
    "media": "Media",
    "notes": "Note",
}

# MIME types
MIME_PDF = "application/pdf"
MIME_JPEG = "image/jpeg"

# Some platforms may not find all of these
MIME_TYPES = {
    ".pdf": "application/pdf",
    ".gvpdf": "application/pdf",
    ".gspdf": "application/pdf",
    ".gv": "text/vnd.graphviz",
    ".dot": "text/vnd.graphviz",
    ".rtf": "application/rtf",
    ".ps": "application/postscript",
    ".svg": "image/svg+xml",
    ".svgz": "image/svg+xml",
    ".jpg": "image/jpeg",
    ".gif": "image/gif",
    ".png": "image/png",
    ".odt": "application/vnd.oasis.opendocument.text",
    ".tex": "application/x-tex",
    ".txt": "text/plain",
    ".html": "text/html",
}

# These determine the supported report categories and default formats
# depending on whether needed dependencies are available.
try:
    import gi

    REPORT_FILTERS = ["gv", "dot", "gvpdf"]
    REPORT_DEFAULTS = {
        CATEGORY_TEXT: "pdf",
        CATEGORY_DRAW: "pdf",
    }
    if shutil.which("dot") is not None:
        REPORT_FILTERS = []
        REPORT_DEFAULTS[CATEGORY_GRAPHVIZ] = "dot"
except ImportError:
    REPORT_FILTERS = ["pdf", "ps", "gspdf", "gvpdf"]
    REPORT_DEFAULTS = {
        CATEGORY_TEXT: "rtf",
    }
