# -*- coding: utf-8 -*-
"""
"""

import sys
import os
import os.path as osp
import re
import subprocess
import shutil
import warnings

from partis.pyproj import (
  norm_dist_name,
  join_dist_filename,
  dist_targz )

from partis.utils import caller_module

try:
  from importlib.metadata import metadata

except ImportError:
  from importlib_metadata import metadata

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def basic_conf(
  package,
  copyright_year ):
  """Convenience method for basic Sphinx and theme configuration options.

  Within the documentation ``conf.py``, call this method and set the ``globals``
  from the returned dictionary of options.
  Any option may be overritten after calling.

  Parameters
  ----------
  package : str
    Name of importable package for which documentation is being generated
  copyright_year : str | int

  Returns
  -------
  dict
    Sphinx options to be set

  Note
  ----
  Any 'list' options set by this method must be appended, instead of assigned,
  using ``+=`` or ``.append(...)``.


  Example
  -------

  .. code-block:: python

    from partis.utils.sphinx import basic_conf

    globals().update( basic_conf(
      package = 'partis',
      copyright_year = '2022' ) )

    exclude_patterns += [
      'src/partis-nwl/src/nwl' ]

    extensions += [
      'sphinxcontrib.bibtex',
      'partis.utils.sphinx.ext' ]

    bibtex_bibfiles = [
      'doc/refs/workflow.bib',
      'doc/refs/peps.bib' ]

  """

  static_path = osp.join( osp.dirname(osp.abspath(__file__)), '_static' )

  meta = metadata( package )

  root_doc = 'index'

  project = meta['Name']
  project_normed = norm_dist_name( project )

  version = meta['Version']
  description = meta['Summary']

  author = (
    meta['Author']
    or meta['Author-email']
    or meta['Maintainer']
    or meta['Maintainer-email'] )

  if not isinstance(author, str):
    author = next(iter(author))

  project_filename = join_dist_filename( [project_normed, version] )
  copyright = f'{copyright_year}, {author}'

  svgconverter = list()

  try:
    import Cairosvg
    svgconverter = ['sphinxcontrib.cairosvgconverter']
  except:
    if shutil.which('inkscape'):
      svgconverter = ['sphinxcontrib.inkscapeconverter']
    else:
      warnings.warn('No svg conversion backend found: cairosvg, inkscape')


  options = dict(
    # General information about the project.
    project = project,
    version = version,
    release = version,
    copyright = copyright,
    description = description,

    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    # general sphinx-doc configuration
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    # If your documentation needs a minimal Sphinx version, state it here.
    #
    needs_sphinx = '3.1',

    # Add any Sphinx extension module names here, as strings. They can be
    # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
    # ones.
    extensions = [
      # 'builtin' extensions
      'sphinx.ext.autodoc',
      "sphinx.ext.intersphinx",
      'sphinx.ext.napoleon',
      'sphinx.ext.mathjax',
      'sphinx.ext.viewcode',
      # installed extensions
      'sphinx_copybutton',
      'sphinx_inline_tabs',
      'sphinx_paramlinks',
      'sphinxcontrib.bibtex',
      # custom extension
      'partis.utils.sphinx.ext',
      # svg image converter extension
      *svgconverter ],

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

    # The suffix(es) of source filenames.
    # You can specify multiple suffix as a list of string:
    #
    # source_suffix = ['.rst', '.md']
    source_suffix = '.rst',

    # The master toctree document.
    root_doc = root_doc,

    # The language for content autogenerated by Sphinx. Refer to documentation
    # for a list of supported languages.
    #
    # This is also used if you do content translation via gettext catalogs.
    # Usually you set "language" from the command line for these cases.
    language = None,

    # List of patterns, relative to source directory, that match files and
    # directories to ignore when looking for source files.
    # This patterns also effect to html_static_path and html_extra_path
    exclude_patterns = [
      '.git',
      '.nox',
      'tmp',
      'build',
      'dist',
      'examples',
      'venv*',
      'test',
      'Thumbs.db',
      '.DS_Store'],

    # The name of the Pygments (syntax highlighting) style to use.
    # pygments_style = 'sphinx'
    pygments_style = 'partis.utils.theme.pygments_light.PygmentsStyle',

    # If true, `todo` and `todoList` produce output, else they produce nothing.
    todo_include_todos = False,

    # If true, the current module name will be prepended to all description
    # unit titles (such as .. function::).
    add_module_names = False,

    # If true, figures, tables and code-blocks are automatically numbered if
    # they have a caption. At same time, the numref role is enabled. For now,
    # it works only with the HTML builder and LaTeX builder. Default is False.
    numfig = True,

    # If true, footnotes and citation include links back to text sections
    # that references them
    footenote_backlinks = False,

    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    # sphinxcontrib.bibtex
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    bibtex_bibfiles = [],

    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    # sphinx.ext.autodoc
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    # This value is a list of autodoc directive flags that should be automatically
    # applied to all autodoc directives. The supported flags are 'members',
    # 'undoc-members', 'private-members', 'special-members', 'inherited-members'
    # and 'show-inheritance'.
    autodoc_default_options = {
      'members': True,
      'special-members': False,
      # 'undoc-members': False,
      'exclude-members': None,
      'private-members': False,
      'special-members': False,
      # alphabetical, groupwise, or bysource
      'member-order': 'bysource',
      'show-inheritance' : True },


    autodoc_inherit_docstrings = True,

    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    # sphinx.ext.intersphinx
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    intersphinx_mapping = {
      "python": ("https://docs.python.org/3", None),
      "setuptools": ("https://setuptools.pypa.io/en/latest", None) },


    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    # sphinx.ext.napoleon
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



    napoleon_google_docstring = False,
    napoleon_numpy_docstring = True,
    napoleon_include_init_with_doc = True,
    napoleon_include_private_with_doc = False,
    napoleon_include_special_with_doc = False,
    napoleon_use_admonition_for_examples = False,
    napoleon_use_admonition_for_notes = False,
    napoleon_use_admonition_for_references = False,
    napoleon_use_ivar = False,
    napoleon_use_param = True,
    napoleon_use_rtype = True,
    napoleon_use_keyword = False,
    napoleon_attr_annotations = True,


    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    # matplotlib.sphinxext.plot_directive
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    plot_include_source = True,
    plot_html_show_source_link = False,
    plot_html_show_formats = False,

    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    # html
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    # The theme to use for HTML and HTML Help pages.  See the documentation for
    # a list of builtin themes.

    html_theme = 'furo',

    # 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_path ],

    html_css_files = [
      'tables.css' ],

    mathjax_path = "mathjax-3.2.0/es5/tex-chtml-full.js",

    html_logo = osp.join( static_path, 'app_icon.svg' ),

    html_title = f'{project} {version}',

    html_theme_options = {
      #-----------------------------------------------------------------------------
      # light theme
      'light_css_variables': {

        'color-foreground-primary' : '#31363B',
        'color-foreground-muted' : '#454545',
        'color-foreground-secondary' : '#292727',
        'color-foreground-border' : '#BAB9B8',

        'color-background-primary' : '#EFF0F1',
        'color-background-secondary' : '#dbe5ee',
        'color-background-border' : '#b1b5b9',

        'color-brand-primary' : '#4a7fac',
        'color-brand-content' : '#4a7fac',

        'color-highlighted-background' : '#3daee90',

        'color-guilabel-background' : '#30506b80',
        'color-guilabel-border' : '#1c466a80',

        'color-highlight-on-target' : '#e2d0b7',
        'color-problematic' : '#875e05'
      },
      #-----------------------------------------------------------------------------
      # dark theme
      'dark_css_variables': {

        'color-foreground-primary' : '#eff0f1',
        'color-foreground-muted' : '#736f6f',
        'color-foreground-secondary' : '#a7aaad',
        'color-foreground-border' : '#76797c',

        'color-background-primary' : '#31363b',
        'color-background-secondary' : '#3b4045',
        'color-background-border' : '#51575d',

        'color-brand-primary' : '#6ab5f4',
        'color-brand-content' : '#6ab5f4',

        'color-highlighted-background' : '#3daee90',

        'color-guilabel-background' : '#30506b80',
        'color-guilabel-border' : '#1c466a80',

        'color-highlight-on-target' : '#7c5418',
        'color-problematic' : '#e6c07b'
      }
    },

    pygments_light_style = 'partis.utils.theme.pygments_light.PygmentsStyle',
    pygments_dark_style = 'partis.utils.theme.pygments_dark.PygmentsStyle',

    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    # htmlhelp
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    # Output file base name for HTML help builder.
    htmlhelp_basename = f'{project_filename}_doc',

    html_copy_source = True,

    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    # latex
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    latex_elements = {
        'papersize': 'letterpaper',
        'fontpkg': '',
          # '\\usepackage[sfdefault]{roboto}' + '\n' +
          # '\\usepackage[ttdefault]{nimbusmononarrow}', # change sans serif font
        'fncychap': '\\usepackage[Sonny]{fncychap}',
        # 'maketitle': '\\maketitle',
        'pointsize': '11pt',
        'preamble': '\\renewcommand{\\textbar}{|}', # fix warnings with some fonts
        'releasename': '',
        'babel': '',
        'printindex': '',
        'fontenc': '',
        'inputenc': '',
        'classoptions': '',
        'utf8extra': '',
        'figure_align': 'htbp',
        'extraclassoptions': 'openany', # remove even/odd blank pages
        'sphinxsetup':
          'TitleColor={rgb}{0,0,0},' +
          'InnerLinkColor={rgb}{0.10,0.10,0.44},' +
          'OuterLinkColor={rgb}{0.10,0.10,0.44}'
    },


    # Grouping the document tree into LaTeX files. List of tuples
    # (source start file, target name, title,
    #  author, documentclass [howto, manual, or own class]).
    latex_documents = [
        (root_doc, f'{project_filename}.tex', f'{project} Documentation',
         author, 'manual', True),
    ],

    # -- Options for manual page output ---------------------------------------

    # One entry per manual page. List of tuples
    # (source start file, name, description, authors, manual section).
    man_pages = [
        (root_doc, project_normed, f'{project} Documentation',
         [author], 1)
    ],

    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    # texinfo
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    # Grouping the document tree into Texinfo files. List of tuples
    # (source start file, target name, title, author,
    #  dir menu entry, description, category)
    texinfo_documents = [
        (root_doc, project_filename, f'{project} Documentation',
         author, project_normed, description,
         'Miscellaneous'),
    ] )

  return options
