# -*- coding: utf-8 -*-
#
from __future__ import print_function

import six

from . import color as mycol
from . import path as mypath

def get_legend_label_(line):
    '''Check if line is in legend
    '''

    label = line.get_label()
    try:
        ax = line.axes
        leg = ax.get_legend()
        return label in [l.get_label() for l in leg.get_lines()]
    except AttributeError:
        return None

def draw_line2d(data, obj):
    '''Returns the PGFPlots code for an Line2D environment.
    '''
    content = []
    addplot_options = []

    # If line is of length 0, do nothing.  Otherwise, an empty \addplot table
    # will be created, which will be interpreted as an external data source
    # in either the file '' or '.tex'.  Instead, render nothing.
    # pylint: disable=len-as-condition
    if len(obj.get_xdata()) == 0:
        return data, []

    # get the linewidth (in pt)
    line_width = _mpl_linewidth2pgfp_linewidth(data, obj.get_linewidth())

    if line_width:
        addplot_options.append(line_width)

    # get line color
    color = obj.get_color()
    data, line_xcolor, _ = mycol.mpl_color2xcolor(data, color)
    addplot_options.append(line_xcolor)

    alpha = obj.get_alpha()
    if alpha is not None:
        addplot_options.append('opacity=%r' % alpha)

    show_line, linestyle = _mpl_linestyle2pgfp_linestyle(obj.get_linestyle())
    if show_line and linestyle:
        addplot_options.append(linestyle)

    marker_face_color = obj.get_markerfacecolor()
    marker_edge_color = obj.get_markeredgecolor()
    data, marker, extra_mark_options = \
        _mpl_marker2pgfp_marker(data, obj.get_marker(), marker_face_color)
    if marker:
        addplot_options.append('mark=' + marker)

        mark_size = obj.get_markersize()
        if mark_size:
            # setting half size because pgfplots counts the radius/half-width
            pgf_size = int(0.5 * mark_size)
            # make sure we didn't round off to zero by accident
            if pgf_size == 0 and mark_size != 0:
                pgf_size = 1
            addplot_options.append('mark size=%d' % pgf_size)

        mark_every = obj.get_markevery()
        if mark_every:
            addplot_options.append('mark repeat=%d' % mark_every)

        mark_options = ['solid']
        if extra_mark_options:
            mark_options.append(extra_mark_options)
        if marker_face_color is None or \
           (isinstance(marker_face_color, six.string_types) and
               marker_face_color == 'none'):
            mark_options.append('fill opacity=0')
        else:
            data, face_xcolor, _ = mycol.mpl_color2xcolor(
                    data,
                    marker_face_color
                    )
            if face_xcolor != line_xcolor:
                mark_options.append('fill=' + face_xcolor)

        face_and_edge_have_equal_color = \
            marker_edge_color == marker_face_color
        # Sometimes, the colors are given as arrays. Collapse them into a
        # single boolean.
        try:
            face_and_edge_have_equal_color = \
                all(face_and_edge_have_equal_color)
        except TypeError:
            pass

        if not face_and_edge_have_equal_color:
            data, draw_xcolor, _ = mycol.mpl_color2xcolor(
                    data,
                    marker_edge_color
                    )