#!/usr/local/cpython-3.4/bin/python3

'''Generate the gnuplot file'''

# pylint: disable=superfluous-parens
# superfluous-parens: Parentheses are good for clarity and portability

import sys

import python2x3

import common


def possible_title(plot_type, datastructure_description):
    '''If this is a line, output a title.  If it's an error bar, return notitle'''

    if plot_type == 'yerrorbars':
        title = python2x3.binary_to_string(datastructure_description).replace('_', r'\_')
        assert ' ' not in title
        return " title '%s'" % (title, )
    elif plot_type == 'linespoints':
        return ' notitle'
    else:
        raise AssertionError('plot_type not yerrorbars or linespoints')


def main():
    '''Main function'''
    print('set output "Datastructure comparison.pdf"')
    print('set key top left')
    print('set terminal pdf color enhanced')
    print('set xlabel "Number of operations"')
    print('set ylabel "Time in seconds"')
    print('#set logscale x')
    print('#set logscale y')
    sys.stdout.write('\n')
    print('# http://www.uni-hamburg.de/Wiss/FB/15/Sustainability/schneider/gnuplot/colors.htm')
    print('set style line 1 lt rgb "red" lw 1')
    print('set style line 2 lt rgb "orange" lw 1')
    print('set style line 3 lt rgb "green" lw 1')
    print('set style line 4 lt rgb "blue" lw 1')
    print('set style line 5 lt rgb "purple" lw 1')
    print('set style line 6 lt rgb "violet" lw 1')
    print('set style line 7 lt rgb "#8B0000" lw 1 # darkred')
    print('set style line 8 lt rgb "#FF8C00" lw 1 # darkorange')
    print('set style line 9 lt rgb "#00008B" lw 1 # darkblue')
    print('set style line 10 lt rgb "black" lw 1')
    sys.stdout.write('\n')
    for interpreter_path in common.INTERPRETERS:
        interpreter = python2x3.binary_to_string(common.get_interpreter(interpreter_path))
        for number_pattern_desc in (python2x3.binary_to_string(interp) for interp in common.NUMBER_PATTERN):
            print('set title "%s, %s.pdf"' % (interpreter, number_pattern_desc))
            print('plot \\')
            datastructure_list = sorted(common.DATASTRUCTURES)
            # We plot the same data twice - once for lines joining the datapoints, and again for the error bars
            for datastructure_line_style, datastructure_description in enumerate(datastructure_list):
                for plot_type in ['linespoints', 'yerrorbars']:
                    # We output a ", \" on each line, Including the last, but then have a blank line at the
                    # end to keep two plot commands from running together.
                    print("    '%s-%s-%s.dat' with %s ls %d%s, \\" % (
                        interpreter,
                        number_pattern_desc,
                        python2x3.binary_to_string(datastructure_description),
                        plot_type,
                        datastructure_line_style + 1,
                        possible_title(plot_type, datastructure_description),
                        ))
            sys.stdout.write('\n')

main()