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 = [
  'SHA1withECDSA224', 'SHA1withECDSA256', 'SHA1withECDSA384', 'SHA1withECDSA521', 
  
  'SHA1withRSA512', 'SHA1withRSARSA768', 'SHA1withRSA1024', 
  'SHA1withRSA2048', 'SHA1withRSA3072', 'SHA1withRSA4096',
  
  'SHA1withRSA512/PSS', 'SHA1withRSARSA768/PSS', 'SHA1withRSA1024/PSS', 
  'SHA1withRSA2048/PSS', 'SHA1withRSA3072/PSS', 'SHA1withRSA4096/PSS',]
benchmarkNames = [
  'benchmarkSHA1WithECDSA224', 'benchmarkSHA1WithECDSA256', 'benchmarkSHA1WithECDSA384', 'benchmarkSHA1WithECDSA521', 
  
  'benchmarkSHA1WithRSA512', 'benchmarkSHA1WithRSA768', 'benchmarkSHA1WithRSA1024', 
  'benchmarkSHA1WithRSA2048', 'benchmarkSHA1WithRSA3072', 'benchmarkSHA1WithRSA4096',
  
  'benchmarkSHA1WithRSA512Pss', 'benchmarkSHA1WithRSA768Pss', 'benchmarkSHA1WithRSA1024Pss', 
  'benchmarkSHA1WithRSA2048Pss', 'benchmarkSHA1WithRSA3072Pss', 'benchmarkSHA1WithRSA4096Pss']

benchmarkClassName = 'cz.vutbr.benchmark.AsymmetricSignatureBenchmark'

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/asymmetric_key_signature_SHA1WithRSAandEC.pdf".format(dir_path))