# Path: aqmt/plot/common.py
# def add_plot(gpi):
#     """
#     Remove trailing comma from plot command because gnuplot complains about it
#     (We use the trailing comma to easier build plot commands)
#     """
#     return re.sub(r',(\s*\\?\s*\\?\s*)$', '\g<1>', gpi)
# 
# def add_scale(y_logarithmic, range_from=None, range_from_log=None,
#         range_to=None, range_to_log=None):
#     if y_logarithmic:
#         if range_from_log is None:
#             range_from_log = '1'
#         if range_to_log is None:
#             range_to_log = '*'
#         return """
#             set logscale y
#             set yrange [""" + range_from_log + """:""" + range_to_log + """]
#             """
#     else:
#         if range_from is None:
#             range_from = '0'
#         if range_to is None:
#             range_to = '1<*'
#         return """
#             set yrange [""" + range_from + """:""" + range_to + """]
#             """


from collections import OrderedDict
from .common import add_plot, add_scale
def drops_marks(y_logarithmic=False, show_total=True):
    def plot(testfolder, plotdef):
        label_y_pos = -0.06 * (1/plotdef.y_scale)
        title = "Packets per sample" if not show_total else \
                "Packets per sample \\n{/Times:Italic=10 Dotted lines are}\\n{/Times:Italic=10 sum dropped and sent}"
        gpi = """
            set format y "%g"
            set ylabel \"""" + title + """\"
            set xtics in mirror
            """ + add_scale(y_logarithmic, range_from_log='0.9', range_to='10<*') + """
            set label "Sample #:" at graph -0.01, graph """ + str(label_y_pos) + """ font 'Times-Roman,11pt' tc rgb 'black' right
            """

        # add hidden line to force autoscaling if using logarithimic plot without any points
        plot_gpi = " 1 lc rgb '#FFFF0000' notitle, \\\n"

        plot_gpi += "'" + testfolder + "/ta/drops_ecn'   using ($0+1):3 with linespoints pointtype 7 ps 0.2 lw 1.5 lc rgb 'red' title 'Drops (ECN)', \\\n"
        plot_gpi += "'" + testfolder + "/ta/marks_ecn'   using ($0+1):3 with linespoints ls 8 pointtype 7 ps 0.2 lw 1.5 title 'Marks (ECN)', \\\n"
        plot_gpi += "'" + testfolder + "/ta/drops_nonecn'   using ($0+1):3 with linespoints ls 3 pointtype 7 ps 0.2 lw 1.5 title 'Drops (Non-ECN)', \\\n"

        if show_total:
            gpi += """
                packets_ecn = "'< paste """ + testfolder + """/ta/drops_ecn """ + testfolder + """/ta/packets_ecn'"
                packets_nonecn = "'< paste """ + testfolder + """/ta/drops_nonecn """ + testfolder + """/ta/packets_nonecn'"
                """
            plot_gpi += "@packets_ecn     using ($0+1):($3+$4) with linespoints ls 8 dt 3 pointtype 7 ps 0.2 lw 1.5 title '', \\\n"
            plot_gpi += "@packets_nonecn  using ($0+1):($3+$4) with linespoints ls 3 dt 3 pointtype 7 ps 0.2 lw 1.5 title '', \\\n"

        gpi += """
            plot \\

# Path: aqmt/plot/common.py
# def add_plot(gpi):
#     """
#     Remove trailing comma from plot command because gnuplot complains about it
#     (We use the trailing comma to easier build plot commands)
#     """
#     return re.sub(r',(\s*\\?\s*\\?\s*)$', '\g<1>', gpi)
# 
# def add_scale(y_logarithmic, range_from=None, range_from_log=None,
#         range_to=None, range_to_log=None):
#     if y_logarithmic:
#         if range_from_log is None:
#             range_from_log = '1'
#         if range_to_log is None:
#             range_to_log = '*'
#         return """
#             set logscale y
#             set yrange [""" + range_from_log + """:""" + range_to_log + """]
#             """
#     else:
#         if range_from is None:
#             range_from = '0'
#         if range_to is None:
#             range_to = '1<*'
#         return """
#             set yrange [""" + range_from + """:""" + range_to + """]
#             """


# Path: aqmt/plot/flow_components.py
from collections import OrderedDict
from .common import add_plot, add_scale

def drops_marks(y_logarithmic=False, show_total=True):
    def plot(testfolder, plotdef):
        label_y_pos = -0.06 * (1/plotdef.y_scale)
        title = "Packets per sample" if not show_total else \
                "Packets per sample \\n{/Times:Italic=10 Dotted lines are}\\n{/Times:Italic=10 sum dropped and sent}"
        gpi = """
            set format y "%g"
            set ylabel \"""" + title + """\"
            set xtics in mirror
            """ + add_scale(y_logarithmic, range_from_log='0.9', range_to='10<*') + """
            set label "Sample #:" at graph -0.01, graph """ + str(label_y_pos) + """ font 'Times-Roman,11pt' tc rgb 'black' right
            """

        # add hidden line to force autoscaling if using logarithimic plot without any points
        plot_gpi = " 1 lc rgb '#FFFF0000' notitle, \\\n"

        plot_gpi += "'" + testfolder + "/ta/drops_ecn'   using ($0+1):3 with linespoints pointtype 7 ps 0.2 lw 1.5 lc rgb 'red' title 'Drops (ECN)', \\\n"
        plot_gpi += "'" + testfolder + "/ta/marks_ecn'   using ($0+1):3 with linespoints ls 8 pointtype 7 ps 0.2 lw 1.5 title 'Marks (ECN)', \\\n"
        plot_gpi += "'" + testfolder + "/ta/drops_nonecn'   using ($0+1):3 with linespoints ls 3 pointtype 7 ps 0.2 lw 1.5 title 'Drops (Non-ECN)', \\\n"

        if show_total:
            gpi += """
                packets_ecn = "'< paste """ + testfolder + """/ta/drops_ecn """ + testfolder + """/ta/packets_ecn'"
                packets_nonecn = "'< paste """ + testfolder + """/ta/drops_nonecn """ + testfolder + """/ta/packets_nonecn'"
                """
            plot_gpi += "@packets_ecn     using ($0+1):($3+$4) with linespoints ls 8 dt 3 pointtype 7 ps 0.2 lw 1.5 title '', \\\n"
            plot_gpi += "@packets_nonecn  using ($0+1):($3+$4) with linespoints ls 3 dt 3 pointtype 7 ps 0.2 lw 1.5 title '', \\\n"

        gpi += """
            plot \\

            """ + add_plot(plot_gpi) + """