# -*- coding: utf-8 -*-
"""
Tag your matplotlib plot with an ID.

Functions:
    TagPlot_matplotlib(figure object, string) -> list
"""

import matplotlib
import matplotlib.pyplot as plt
import create_id
from plotoptions import PlotOptions


def tagplot_matplotlib(plotid_object):
    """
    Add IDs to figures with matplotlib.

    The ID is placed visual on the figure window and
    as Tag (Property of figure).
    TagPlot can tag multiple figures at once
    """
    # Check if plotid_object is a valid instance of PlotOptions
    if not isinstance(plotid_object, PlotOptions):
        raise TypeError('The given options container is not an instance'
                        'of PlotOptions.')
    # Check if figs is a valid figure or a list of valid figures
    if isinstance(plotid_object.figs, matplotlib.figure.Figure):
        pass
    elif isinstance(plotid_object.figs, list):
        for figure in plotid_object.figs:
            if isinstance(figure, matplotlib.figure.Figure):
                pass
    else:
        raise TypeError('Figure is not a valid matplotlib-figure.')

    fontsize = 'small'
    color = 'grey'
    all_ids_as_list = []

    # Loop to create and position the IDs
    for fig in plotid_object.figs:
        figure_id = create_id.create_id(plotid_object.id_method)
        figure_id = plotid_object.prefix + str(figure_id)
        all_ids_as_list.append(figure_id)

        plt.figure(fig.number)
        plt.figtext(x=plotid_object.position[0], y=plotid_object.position[1],
                    s=figure_id, ha='left', wrap=True,
                    rotation=plotid_object.rotation,
                    fontsize=fontsize, color=color)
        fig.tight_layout()
    return [plotid_object.figs, all_ids_as_list]