import sys
import json


if(1):
    
    from PySTorM32App_paramjson import paramJson, paramLayoutVersion

    fs = 'paramJson = [\n'
    
    fs += '    {\n'
    for param in paramJson:
        keys = param.keys()
    
        #print('  '+param['name'])
        
        #first read in everything which possibly could be there
        # then decide what to print out
        
        namelist = param['name'].split('(')
        name = namelist[0].strip()
        if len(namelist) > 1:
          ext = '('+namelist[1].strip()
        else:  
          ext = ''
        if 'unit' in keys: unit = param['unit']
        else:              unit = ''
        
        page = param['page']

        type = param['type']
        size = param['size']
        if 'adr' in keys: adr = param['adr']
        else:             adr = -1
        default = param['default']
        
        if 'len' in keys: len_ = param['len']
        else:             len_ = 0
        if 'ppos' in keys: ppos = param['ppos']
        else:              ppos = 0
        
        if 'steps' in keys: steps = param['steps']
        else:               steps = 0
        if 'min' in keys: min = param['min']
        else:             min = 0
        if 'max' in keys: max = param['max']
        else:             max = 0
        
        if 'choices' in keys: choices = param['choices']
        else:                 choices = []
        
        foc = param['foc']
        hidden = param['hidden']
            
        print('  '+name+', '+ext+', '+type)
        
        #print that they all should have          
        fs += '        "name" : "'+name+'",\n'
        fs += '        "ext" : "'+ext+'",\n'
        fs += '        "page" : "'+page+'",\n'
        fs += '        "type" : "'+type+'",\n'
        fs += '        "size" : '+str(size)+',\n'
        fs += '        "adr" : '+str(adr)+',\n'
        if type == 'STR+READONLY':
            fs += '        "default" : "'+default+'",\n'
        else:
            if default == "": default = 0
            fs += '        "default" : '+str(default)+',\n'
        
        
        #print depending on type
        
        if type == 'STR+READONLY':
            pass
            
        elif type == 'LIST':
            if steps != 1: print('FUCK LIST STEPS')
            if min != 0: print('FUCK LIST MIN')
            if max > len(choices)-1: print('FUCK LIST MAX')
            if max != len(choices)-1: print('WARN LIST MAX')

            fs += '        "steps" : '+str(steps)+', '
            fs += '"min" : '+str(min)+', '
            fs += '"max" : '+str(max)+',\n'
            
            fs += '        "choices" : ['
            for c in choices:
                fs += '"'+c+'", '
            fs = fs[:-2]+'],\n'
            
        elif (type == 'UINT') or (type == 'INT'):
            fs += '        "steps" : '+str(steps)+', '
            fs += '"min" : '+str(min)+', '
            fs += '"max" : '+str(max)+',\n'
            
            fs += '        "len" : '+str(len_)+', '
            fs += '"ppos" : '+str(ppos)+', '
            fs += '"unit" : "'+unit+'",\n'
            
        elif type == 'SCRIPT':
            pass
            
        else:
            print('FUCK')
        

        
        
        #finsih with that they all should have          
        fs += '        "foc" : '+str(foc)+',\n'
        fs += '        "hidden" : '+str(hidden)+',\n'
        
        #print info which is for the perl gui only
        if 'column' in keys:  
            fs += '        "column" : '+str(param['column'])+',\n'
        if 'pos' in keys:  
            fs += '        "pos" : ['+str(param['pos'][0])+','+str(param['pos'][1])+'],\n'
        
        fs = fs[:-2]+'\n'
        fs += '    },{\n'
        
    
    fs = fs[:-3]+'\n'
    fs += ']\n'
        
    F = open('cvt-out.py', 'w')
    F.write(fs)
    F.close()

               
               
