'''
Created on May 15, 2017

@author: Bart
'''
import matplotlib.pyplot as plt
import json
from plot import Color, LineStyle

class MatPlotLibWrapper:
        
    def visualize(self, plots):
        i = 1
        for plot in plots:
            plot = json.loads(plot)
            # create a new subplot
            plt.subplot(len(plots), 1, i)
            # do all datasets for this subplot
            for dataset in plot['datasets']:
                stylecode = ""
                if dataset['color'] != None or dataset['linestyle'] != None:
                    stylecode = MatPlotLibWrapper.__color_code(dataset['color'])+MatPlotLibWrapper.__linestyle_code(dataset['linestyle'])
                # plot data with code and style 
                plt.plot([p['x'] for p in dataset['datapoints']], 
                        [p['y'] for p in dataset['datapoints']], 
                        stylecode)
            # set title
            if plot['title'] != None:
                plt.title(plot['title'])
            # set x and y axis labels
            if plot['x']['unit']:
                plt.xlabel("%s (%s)" % (plot['x']['name'], plot['x']['unit']))
            else:
                plt.xlabel("%s" % plot['x']['name'])
            if plot['y']['unit']:
                plt.ylabel("%s (%s)" % (plot['y']['name'], plot['y']['unit']))
            else:
                plt.ylabel("%s" % plot['y']['name'])
            # make legend
            if plot['legend']:
                plt.legend([ds['legend'] for ds in plot['datasets']])
            # set axis ranges
            if plot['x']['lim_low'] != None and plot['x']['lim_high'] != None:
                plt.xlim([plot['x']['lim_low'], plot['x']['lim_high']])
            if plot['y']['lim_low'] != None and plot['y']['lim_high'] != None:
                plt.ylim([plot['y']['lim_low'], plot['y']['lim_high']])
            i += 1
        plt.show()
    
    @staticmethod
    def __color_code(color):
        if color == Color.BLUE:
            return "b"
        elif color == Color.GREEN:
            return "g"
        elif color == Color.RED:
            return "r"
        elif color == Color.CYAN:
            return "c"
        elif color == Color.MAGENTA:
            return "m"
        elif color == Color.YELLOW:
            return "y"
        elif color == Color.BLACK:
            return "k"
        elif color == Color.WHITE:
            return "w"
        else:
            return MatPlotLibWrapper.__color_code(Color.BLUE)
    
    @staticmethod
    def __linestyle_code(linestyle):
        if linestyle == LineStyle.SOLID:
            return "-"
        elif linestyle == LineStyle.DASHDOT:
            return "-."
        elif linestyle == LineStyle.DASHED:
            return "--"
        elif linestyle == LineStyle.DOTTED:
            return ":"
        elif linestyle == LineStyle.PIXEL:
            return ","
        elif linestyle == LineStyle.POINT:
            return "."
        elif linestyle == LineStyle.CIRCLE:
            return "o"
        elif linestyle == LineStyle.STAR:
            return "*"
        elif linestyle == LineStyle.PLUS:
            return "+"
        elif linestyle == LineStyle.X:
            return "x"
        else:
            return MatPlotLibWrapper.__linestyle_code(LineStyle.CIRCLE)
        