#! /usr/bin/python3
# -*- coding: utf-8 -*-
"""
========================================================
S01 - Mandelbrot explorer - Standard precision
========================================================

This is a simple "toy" template to start exploring the Mandelbrot set with
the GUI. Resolution limited to approx 1.e-13 due to double (float64) precision

Reference:
`fractalshades.models.Mandelbrot`
"""
import os

import fractalshades as fs
import fractalshades.models as fsm
import fractalshades.gui
import fractalshades.gui.guitemplates
import fractalshades.gui.guimodel


def plot(plot_dir):
    """
    """
    fractal = fsm.Mandelbrot(plot_dir)

    zooming = fs.gui.guitemplates.std_zooming(fractal)
    gui = fs.gui.guimodel.Fractal_GUI(zooming)
    gui.show()


if __name__ == "__main__":
    # Some magic to get the directory for plotting: with a name that matches
    # the file or a temporary dir if we are building the documentation
    plot_dir = None
    try:
        realpath = os.path.realpath(__file__)
        plot_dir = os.path.splitext(realpath)[0]
        # When this script is installed as a system binary (e.g.
        # /usr/bin/fractalshades) the derived path collides with the
        # executable itself and is not a usable output directory; fall back
        # to a temporary one.
        if not os.path.isdir(plot_dir) and os.path.exists(plot_dir):
            plot_dir = None
    except NameError:
        pass

    if plot_dir is None:
        import tempfile
        with tempfile.TemporaryDirectory() as plot_dir:
            fs.utils.exec_no_output(plot, plot_dir)
    else:
        plot(plot_dir)
