# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import shutil
import sys

sys.path.insert(0, os.path.abspath("../.."))  # path to the actual project root folder

# -- Project information -----------------------------------------------------

project = "Spotted dmi bot"
copyright = "2021, UNICT Devs"
author = "Tend, drendog, alepiaz, Helias, Herbrant, LightDestory, TaToTanWeb"

# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    "sphinx.ext.autodoc",
    "sphinx.ext.doctest",  # to run doctests
    "sphinx.ext.napoleon",  # to use NumPy and Google style docstrings
    "sphinx.ext.githubpages",  # generates the .nojekyll file
    "sphinx.ext.viewcode",  # add source code links to the documentation
    "sphinx_rtd_dark_mode",  # dark mode for ReadTheDocs
    "sphinx_autodoc_typehints",  # improves the type hinting
    "sphinx.ext.viewcode",  # add source code links to the documentation
    "sphinx.ext.coverage",  # add coverage links to the documentation
    "sphinx.ext.intersphinx",  # add external mapping to other documentation
    "m2r2",  # to convert markdown to rst
    "sphinxcontrib.mermaid",  # to render mermaid diagrams
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []

# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = "sphinx_rtd_theme"  # [optional, to use the far superior Read the Docs theme]

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
html_css_files = [
    "css/dark.css",
]

html_logo = "_static/img/spotted-logo.jpg"

# -- Extension configuration -------------------------------------------------

# -- Configuration of "sphinx_autodoc_typehints" -----------------------------
typehints_use_rtype = False
typehints_defaults = "comma"

# -- Run sphinx-apidoc -------------------------------------------------------
# This hack is necessary since RTD does not issue `sphinx-apidoc` before running
# `sphinx-build -b html . _build/html`. See Issue:
# https://github.com/readthedocs/readthedocs.org/issues/1139
# DON'T FORGET: Check the box "Install your project inside a virtualenv using
# setup.py install" in the RTD Advanced Settings.
# Additionally it helps us to avoid running apidoc manually

try:  # for Sphinx >= 1.7
    from sphinx.ext import apidoc
except ImportError:
    from sphinx import apidoc

output_dir = os.path.join(os.path.dirname(__file__), "api")
module_dir = os.path.join(os.path.dirname(__file__), "../../src/spotted")
try:
    shutil.rmtree(output_dir)
except FileNotFoundError:
    pass

try:
    import sphinx

    cmd_line = f"sphinx-apidoc --implicit-namespaces -t templates -f -o {output_dir} {module_dir}"

    args = cmd_line.split(" ")
    if tuple(sphinx.__version__.split(".")) >= ("1", "7"):
        # This is a rudimentary parse_version to avoid external dependencies
        args = args[1:]

    apidoc.main(args)
except Exception as e:
    print("Running `sphinx-apidoc` failed!\n{}".format(e))

# -- External mapping --------------------------------------------------------
python_version = ".".join(map(str, sys.version_info[0:2]))
intersphinx_mapping = {
    "sphinx": ("https://www.sphinx-doc.org/en/master", None),
    "python": ("https://docs.python.org/" + python_version, None),
    "matplotlib": ("https://matplotlib.org", None),
    "numpy": ("https://numpy.org/doc/stable", None),
    "sklearn": ("https://scikit-learn.org/stable", None),
    "pandas": ("https://pandas.pydata.org/pandas-docs/stable", None),
    "scipy": ("https://docs.scipy.org/doc/scipy/reference", None),
    "setuptools": ("https://setuptools.pypa.io/en/stable/", None),
    "pyscaffold": ("https://pyscaffold.org/en/stable", None),
    "telegram": ("https://docs.python-telegram-bot.org/en/stable/", None),
}

# -- Options for sphinxcontrib.mermaid ---------------------------------------
mermaid_init_js = """
mermaid.initialize({
    startOnLoad: true,
    theme: 'dark',
});
"""
