import plotly.figure_factory as ff
import plotly.graph_objects as go
import plotly
import json
import os
from decimal import Decimal

dir_path = os.path.dirname(os.path.realpath(__file__))

results = []
texts = []
devicesX = []
algorithmsX = [
  'AES128', 'AES192', 'AES256']
benchmarkNames = [
  'benchmarkAES128', 'benchmarkAES192', 'benchmarkAES256']

benchmarkClassName = 'cz.vutbr.benchmark.KeyGenerationBenchmark'

for benchmarkName in benchmarkNames:
  result = []
  text = []
  for device in os.listdir("{}/results".format(dir_path)):
      filePath = "{}/results/{}/result.json".format(dir_path, device)
      content = open(filePath, "r").read()
      devicesX.append(device)
      decoded = json.loads(content)
      benchmarkResults = decoded['benchmarks']
      for benchmark in benchmarkResults:
          if benchmark['className'] == benchmarkClassName and benchmark['name'] == benchmarkName:
              metric = benchmark['metrics']['timeNs']['median']
              result.append(metric/1000000)
              text.append('%.2f' % Decimal(metric/1000000))
              break
  results.append(result)
  texts.append(text)


fig = go.Figure()
fig.add_trace(go.Bar(
    x=devicesX,
    y=results[0],
    text=texts[0],
    textposition='auto',
    name=algorithmsX[0],
    marker_color='#482878'
))
fig.add_trace(go.Bar(
    x=devicesX,
    y=results[1],
    text=texts[1],
    textposition='auto',
    name=algorithmsX[1],
    marker_color='#31688e'
))
fig.add_trace(go.Bar(
    x=devicesX,
    y=results[2],
    text=texts[2],
    textposition='auto',
    name=algorithmsX[2],
    marker_color='#1f9e89'
))

fig.update_layout(
  barmode='group', 
  xaxis_title="Device",
  yaxis_title="Run time [ms]",
  autosize=False,
  width=1280,
  height=720)
fig.write_image("{}/pdf/symmetric_key_creation.pdf".format(dir_path))