"""
Secondary views offered after a map: polar stereographic and the 3D globe.

Kept apart from render/__init__.py so that geomap can import it without a
circular import through the renderer registry.
"""

import os

from ..figure import VIDEO_SUFFIXES
from ..interactive import ask
from .polar import plot_polar_views
from .globe import GlobeOptions, plot_3D_globe


def globe_output_path(output_path, animated=False):
    """
    Where the globe writes its screenshot or movie.

    The main figure has already claimed `output_path`, so the globe takes
    '<stem>_globe<ext>' beside it, the same way the polar views take
    '<stem>_north<ext>'. A stem that is neither a still nor, for an animation, a
    movie becomes one: the globe is rendered by VTK rather than by matplotlib,
    so a PDF or SVG is not on offer.
    """
    if not output_path:
        return None
    stem, ext = os.path.splitext(output_path)
    allowed = VIDEO_SUFFIXES if animated else ('.png', '.jpg', '.jpeg')
    if ext.lower() not in allowed:
        ext = allowed[0]
    return f"{stem}_globe{ext}"


def show_extra_views(lon2d, lat2d, data2d, colormap, varname, units,
                     interactive, show_polar, show_3d, show_topo, output_path,
                     norm=None, globe_options=None, frames=None, frame_dim=None,
                     frame_axis=None, fps=12, composite=None, layers=(),
                     overlay_style='blend', interpolate=True):
    """
    Display the optional polar-stereographic and 3D globe views of a lat/lon map.
    The flags force the views on; otherwise the user is prompted when interactive.

    `norm` is the colour scale the main map used. Passing it on is what keeps
    these secondary views from re-deriving a linear scale of their own and
    contradicting the map the user has just seen. The frames travel with it for
    the same reason: an animated map whose polar views were frozen on step zero
    would be reporting two different times side by side.
    """
    if show_polar or ask("Display polar-stereo views?", interactive):
        plot_polar_views(lon2d, lat2d, data2d, colormap, varname, units,
                         topo_overlay=show_topo, output_path=output_path, norm=norm,
                         frames=frames, frame_dim=frame_dim, frame_axis=frame_axis,
                         fps=fps, interactive=interactive, layers=layers,
                         overlay_style=overlay_style, interpolate=interpolate)

    if show_3d or ask("Display 3D globe view?", interactive):
        options = globe_options or GlobeOptions()
        options.output_path = globe_output_path(output_path, animated=frames is not None)
        options.contours = show_topo
        # The surface globe colours one sphere, and two coloured spheres at
        # nearly one radius cannot be ordered: VTK sorts translucent actors by
        # centroid, which puts concentric shells all at the same distance. The
        # shell path does this properly when there is a vertical axis, so say
        # that rather than draw something that is right from one camera angle.
        if composite:
            names = ', '.join(f"'{n}'" for n in composite.names[1:])
            print(f"Overlay: the surface globe shows one variable; {names} need "
                  f"a vertical axis to be drawn as shells. Use --plot-kind globe "
                  f"on a field with altitude, or --overlay-style panels.")
        options.frames, options.frame_dim = frames, frame_dim
        options.frame_axis, options.fps = frame_axis, fps
        plot_3D_globe(lon2d, lat2d, data2d, colormap, varname, units,
                      norm=norm, options=options)
