import os
import sys
import hashlib

from GChartWrapper import Line
from GChartWrapper import constants
from GChartWrapper import VerticalBarGroup

from config import cfg_dict


# Patch MARKER constant
constants.MARKERS += 'E'
sys.modules['GChartWrapper.GChart'].MARKERS += 'E'


COLORS = ["1569C7", "81D8D0", "B0BD2E", "5CB3FF", "0040FF", "81DAF5"]
constants.MARKERS += 'E'  # append E marker to available markers


def get_top_top_dir(path):
    top_top_dir = os.path.dirname(os.path.dirname(path))
    return path[len(top_top_dir) + 1:]


def render_vertical_bar(title, legend, bars_data, bars_dev_top,
                        bars_dev_bottom, file_name,
                        width=700, height=400,
                        scale_x=None, scale_y=None, label_x=None,
                        label_y=None, lines=()):
    """
    Renders vertical bar group chart

    :param legend - list of legend values.
        Example: ['bar1', 'bar2', 'bar3']
    :param dataset - list of values for each type (value, deviation)
        Example:
            [
                [(10,1), (11, 2), (10,1)], # bar1 values
                [(30,(29,33)),(35,(33,36)), (30,(29,33))], # bar2 values
                [(20,(19,21)),(20,(13, 24)), (20,(19,21))] # bar 3 values
            ]
    :param width - width of chart
    :param height - height of chart
    :param scale_x - x ace scale
    :param scale_y - y ace scale

    :returns url to chart

    dataset example:
    {
        'relese_1': {
            'randr': (1, 0.1),
            'randwr': (2, 0.2)
        }
        'release_2': {
            'randr': (3, 0.3),
            'randwr': (4, 0.4)
        }
    }
    """

    bar = VerticalBarGroup([], encoding='text')
    bar.title(title)

    dataset = bars_data + bars_dev_top + bars_dev_bottom + \
        [l[0] for l in lines]

    bar.dataset(dataset, series=len(bars_data))

    axes_type = ""

    if scale_x:
        bar.axes.label(0, *scale_x)
        axes_type += "x"
    if label_x:
        bar.axes.style(len(axes_type), '000000', '13')
        bar.axes.label(len(axes_type), label_x)
        axes_type += "x"

    max_value = (max([max(l) for l in dataset[:2]]))
    bar.axes.range(len(axes_type), 0, max_value)
    bar.axes.style(len(axes_type), 'N*s*')
    axes_type += "y"

    if label_y:
        bar.axes.style(len(axes_type), '000000', '13')
        bar.axes.label(len(axes_type), None, label_y)
        axes_type += "y"

    bar.scale(*[0, max_value] * 3)

    bar.bar('r', '.1', '1')
    for i in range(1):
        bar.marker('E', '000000', '%s:%s' % ((len(bars_data) + i*2), i),
                   '', '1:10')
    bar.color(*COLORS)
    bar.size(width, height)

    scale = [0, max_value] * len(bars_dev_top + bars_dev_bottom + bars_data)
    if lines:
        line_n = 0
        for data, label, axe, leg in lines:
            bar.marker('D', COLORS[len(bars_data) + line_n],
                       (len(bars_data + bars_dev_top + bars_dev_bottom))
                       + line_n, 0, 3)
            # max_val_l = max(data)
            if axe:
                max_val_l = max(data)
                bar.axes.type(axes_type + axe)
                bar.axes.range(len(axes_type), 0, max_val_l)
                bar.axes.style(len(axes_type), 'N*s*')
                bar.axes.label(len(axes_type) + 1, None, label)
                bar.axes.style(len(axes_type) + 1, '000000', '13')
                axes_type += axe
                line_n += 1
                scale += [0, max_val_l]
            else:
                scale += [0, max_value]
            legend.append(leg)
            # scale += [0, max_val_l]

    bar.legend(*legend)
    bar.scale(*scale)
    img_path = file_name + ".png"

    if not os.path.exists(img_path):
        bar.save(img_path)

    return get_top_top_dir(img_path)


def render_lines(title, legend, dataset, scale_x, width=700, height=400):
    line = Line([], encoding="text")
    line.title(title)
    line.dataset(dataset)

    line.axes('xy')
    max_value = (max([max(l) for l in dataset]))
    line.axes.range(1, 0, max_value)
    line.scale(0, max_value)
    line.axes.label(0, *scale_x)
    line.legend(*legend)
    line.color(*COLORS[:len(legend)])
    line.size(width, height)

    img_name = hashlib.md5(str(line)).hexdigest() + ".png"
    img_path = os.path.join(cfg_dict['charts_img_path'], img_name)
    if not os.path.exists(img_path):
        line.save(img_path)

    return get_top_top_dir(img_path)
