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__))

logcolorscale = [
        [
          0,
          '#482878'
        ],
        [
          0.0001,
          '#482878'
        ],
        [
          0.001,
          '#31688e'
        ],
        [
          0.01,
          '#1f9e89'
        ],
        [
          0.1,
          '#6ece58'
        ],
        [
          1,
          '#fde725'
        ]
      ]

results = []
texts = []
devicesY = []
algorithmsX = [
  'AES128/CBC/NoPadding', 'AES192/CBC/NoPadding', 'AES256/CBC/NoPadding', 
  'AES128/CBC/PKCS7Padding', 'AES192/CBC/PKCS7Padding', 'AES256/CBC/PKCS7Padding', 
  'AES128/CTR/NoPadding', 'AES192/CTR/NoPadding', 'AES256/CTR/NoPadding',
  'AES128/ECB/NoPadding','AES192/ECB/NoPadding','AES256/ECB/NoPadding',
  'AES128/ECB/PKCS7Padding', 'AES192/ECB/PKCS7Padding','AES256/ECB/PKCS7Padding', 
  'AES128/GCM/NoPadding', 'AES192/GCM/NoPadding','AES256/GCM/NoPadding']
benchmarkNames = [
  'benchmarkAES128CbcNoPadding', 'benchmarkAES192CbcNoPadding', 'benchmarkAES256CbcNoPadding', 
  'benchmarkAES128CbcPKCS7Padding', 'benchmarkAES192CbcPKCS7Padding', 'benchmarkAES256CbcPKCS7Padding', 
  'benchmarkAES128CTRNoPadding', 'benchmarkAES192CTRNoPadding', 'benchmarkAES256CTRNoPadding',
  'benchmarkAES128ECBNoPadding','benchmarkAES192ECBNoPadding','benchmarkAES256ECBNoPadding',
  'benchmarkAES128ECBPKCS7Padding', 'benchmarkAES192ECBPKCS7Padding','benchmarkAES256ECBPKCS7Padding', 
  'benchmarkAES128GCMNoPadding', 'benchmarkAES192GCMNoPadding','benchmarkAES256GCMNoPadding']

benchmarkClassName = 'cz.vutbr.benchmark.SymmetricEncryptionBenchmark'

for device in os.listdir("{}/results".format(dir_path)):
    filePath = "{}/results/{}/result.json".format(dir_path, device)
    content = open(filePath, "r").read()
    devicesY.append(device)
    decoded = json.loads(content)
    benchmarkResults = decoded['benchmarks']
    result = []
    text = []
    for benchmarkName in benchmarkNames:
      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 = ff.create_annotated_heatmap(results, x=algorithmsX, y=devicesY, annotation_text=texts, colorscale='VIRIDIS', showscale = True)
fig.update_layout(
    xaxis_title="Algorithm",
    yaxis_title="Device",
    autosize=False,
    width=1280,
    height=720,
)
fig['layout']['xaxis']['side'] = 'bottom'
fig['data'][0]['colorbar'].tickformat = ",.1"
fig['data'][0]['colorbar'].ticksuffix = " ms"
fig.write_image("{}/pdf/symmetric_key_encryption.pdf".format(dir_path))