import sys
import re
import json


LayoutVersion = 236


if(1):
    print("-------------------------")
    F = open('o323BGCTool_L'+str(LayoutVersion)+'.json', 'r') 
    js = F.read()
    F.close
    F = open('PySTorM32App_paramjson.py', 'w')
    F.write('#!/usr/bin/env python\n')
    F.write('#STorM32 parameters list, generated by PySTorM32_createParamJsons.py\n\n')
    F.write('paramLayoutVersion = '+str(LayoutVersion)+'\n\n')
    F.write("#this is the JSON of parameters\n")
    F.write("# it is different from paramItems, in that 'page' needs to be translated to 'nav'\n")
    F.write('paramJson = ')
    #choices may have numbers, convert them to strings
    j = ''
    L = js.split('\n')
    weAreInChoices = False
    for line in L:
#        print( line )
        if not weAreInChoices:
            if 'choices' in line:
#                print( "XXXXXXXXXXXXXXXXXXXX\n")
                weAreInChoices = True
        else:
            if ']' in line:
#                print( "YYYYYYYYYYYYYYYYYYYYYY\n")
                weAreInChoices = False
            else:
                if not '"' in line:             
#                    print( "!" )
                    ss = re.sub(r'([0-9]+)', r'"\1"', line)
#                    print(ss)
                    line = ss
        j += line + '\n'
    
    F.write(j)
    F.close()


if(1):
    
    from PySTorM32App_paramjson import paramJson, paramLayoutVersion

    #print(json.dumps(obj, indent=4))

    print("-------------------------")

    for param in paramJson:
        #print("*****")
        #print(param['name'])
        #print(param['name'].replace(' ',''))
        #print(param['name'].replace(' ','_').upper())
        #print(param['page'].upper())
        #print(param['type'].replace(' ','').replace('OPTTYPE_','').replace('+','').upper())
        name = param['name']
        type = param['type'].replace(' ','').replace('OPTTYPE_','').replace('+','').upper()
        NAME = param['name'].replace(' ','_').upper()
        PAGE = param['page'].upper()
        s = '  X( "'+name+'", '+type+', '+NAME+', '+PAGE+' )\\'
        #print(s)
        #print(param)


    print("-------------------------")


    # This generates the var P as needed in the espweb.js JS code
    '''
  'Name' :    {'name' : 'Board', 'type' : 'OPTTYPE_STR+OPTTYPE_READONLY', 'tab' : '', 'size' : 16 },

  'GyroLPF' : {'name' : 'Gyro LPF', 'type' : 'OPTYPE_LISTA', 'tab' : '',
               'min' : 0, 'max' : 6, 'step' : 1, 'pos' : 1,
               'choices' : ['off', '1.5 ms', '3.0 ms', '4.5 ms', '6.0 ms', '7.5 ms', '9 ms'] },
    '''
#if(1):
    F = open('PySTorM32WebApp_paramjson.js', 'w')
    F.write('//STorM32 parameters list, generated by PySTorM32_createParamJsons.py\n\n')
    F.write('var paramLayoutVersion = '+str(paramLayoutVersion)+';\n\n')
    F.write('var P = \n{\n')
    for param in paramJson:
        #print(param['name'])
        param['name'] = param['name'].split('(')[0].strip()
        NAME = param['name'].replace('#','').replace('-','').replace(' ','')
        s = '  \''+NAME+'\' : ' +json.dumps(param).replace('"','\'').replace(':',' :') + ','
        #print(s)
        F.write(s+'\n')
    F.write('}\n')
    F.close()

               
               
