#!/usr/bin/python3

import cgi, cgitb
#import re

form = cgi.FieldStorage()

AZ = form.getvalue('AZ')
A=form.getvalue('A')
Z=form.getvalue('Z')

import os, subprocess
os.chdir(r"files")

result = "";

if 'AZ' in form:
  #haha = re.split(r'(\d+)', AZ)
  #haha = list(filter(None, haha))
  #haha[1] = haha[1].capitalize()
  #kaka = ''.join(haha)
  result=subprocess.run(['../Cleopatra/IsotopeShort', AZ],  stdout=subprocess.PIPE).stdout.decode('utf-8')

if 'A' in form:
  result=subprocess.run(['../Cleopatra/IsotopeShort', A, Z],  stdout=subprocess.PIPE).stdout.decode('utf-8')



print( "Content-type:text/html\r\n\r\n")
#print("<html>")
#print("<body>")

if result != "":

  pA = result.find('A:')
  pZ = result.find('Z:')
  p1 = result.find('Mass:')
  p2 = result.find('Name:')
  p2a = result.find('|')
  pSn = result.find('Sn:')
  pSp = result.find('Sp:')
  pSa = result.find('Sa:')
  print(int(result[pA+2:pA+5])
     , ",",int(result[pZ+2:pZ+5])
     , ",",float(result[p1+5:p1+13+5])
     , ",",result[p2+5: p2a] 
     , ",", float(result[pSn+3:pSn+16])
     , ",", float(result[pSp+3:pSp+16])
     , ",", float(result[pSa+3:pSa+16])
    )
  
else:
  print("error");
#print("<br>" + result[p1:p1+13+5])
#p1 = result.find('Sn:')
#print("<br>" + result[p1:p1+16])
#p1 = result.find('Sp')
#print("<br>" + result[p1:p1+16])
#p1 = result.find('Sa')
#print("<br>" + result[p1:p1+16])
#print("<br>=============================")


#print("</body>")
#print("</html>")

#import plotly.express as px

#fig = px.scatter(x=range(10), y = range(10))
#fig.write_html("test.html")

