import openpyxl
import sys
import statistics
import collections
import pprint
import xlsxwriter
import itertools
import matplotlib
import matplotlib.pyplot as plt
import numpy

def quality_convert(x):
    if x < 2:
        return 1500 * x - 500
    else:
        return 2500 * x - 2500

import matplotlib
import matplotlib.pyplot as plt
import numpy as np


# 0 = buf,
# 1 = front rtt,
# 2 = front rate,
# 3 = back rtt,
# 4 = back rate,
# 5 = type,
# 6 = Average Quality,
# 7 = dev AQ,
# 8 = Stall Rate,
# 9 = dev SR,
# 10 = r1,
# 11 = r1err,
# 12 = r6,
# 13 = r6err,
# 14 = r11err,
# 15 = r11err,

def draw():
    labels = ["Level 0", "Level 3", "Level 6", "Level 9"]

    http_8 = [0.03, 0.1, 0.04, 0.03]
    telescope_8 = [0.03, 0.04, 0.02, 0.03]

    http_2_5 = [0.07, 0.09, 0.07, 0.08]
    telescope_2_5 = [0.03, 0.03, 0.03, 0.03]

    http_25 = [0.05, 0.03, 0.05, 0.11]
    telescope_25 = [0.04, 0.03, 0.04, 0.03]

    x = np.arange(len(labels))  # the label locations
    width = 0.35  # the width of the bars

    telescope_stall = telescope_8

    abr_stall = http_8

    fig, ax = plt.subplots()
    rects1 = ax.bar(x - width/2, telescope_stall, width, label="Telescope", capsize=3, color="darkblue")
    rects2 = ax.bar(x + width/2, abr_stall, width, label="ABR", capsize=3, color="darkorange")

    ax.set_ylabel("Stall Rate", fontsize=16)
    ax.set_title("Stall Rate Comparison (2.5 Mbps)", fontsize=18)
    ax.set_xticks(x, fontsize=16)
    ax.set_xticklabels(labels, fontsize=16)
    ax.legend()

#    ax2 = fig.add_subplot(111, label="stallrate", frame_on=False)
#    rects3 = ax2.bar(x - width/2, telescope_stall, width, label="Stall Rate", color="darkred", capsize=3)
#    rects4 = ax2.bar(x + width/2, abr_stall, width, label="ABR Stall", color="darkred", capsize=3)
#    ax2.yaxis.tick_right()
#    ax2.set_ylabel("Stall Rate", fontsize=16)
#    ax2.yaxis.set_label_position("right")
##    ax2.set_xlim(-1.2, 3)
#    ax2.get_xaxis().set_visible(False)
#    ax2.set_ylim(0, 1.5)
##
##    ax2.scatter(x, telescope_stall, color="gray")
#    lines, labels = ax.get_legend_handles_labels()
#    lines2, labels2 = ax2.get_legend_handles_labels()
#
#    ax2.legend(lines + [lines2[0]], labels + [labels2[0]], loc="upper left")


    fig.tight_layout()

    plt.savefig("StallRate_2_5mbps.png")
    #plt.show()

draw()
