import json
import sys

src = sys.argv[1]

with open(src,'r') as f:
    data = json.load(f)

def fix_desc(desc):
    abrv = [('i.e.', 'I/E')]
    for x in abrv:
        desc = desc.replace(x[0], x[1])
    desc = desc.replace('. ', '.  \n')
    for x in abrv:
        desc = desc.replace(x[1], x[0])
    desc = desc.replace("NOTE", "\n*Note*")
    return desc

s = "# State Representations\n\nThe following are accepted by `env.get_state(...)`:\n\n"
for A in data:
    name = A['name']
    desc = A['desc']
    args = A['args']
    demo = A['demo']
    s += "\n---\n## `" + name + "`" + '\n'
    s += fix_desc(desc) + '\n\n'
    if 'rets' in A:
        s += A['rets'].replace('Returns', '**Returns**', count=1) + '\n\n'
    s += "**Parameters**\n\n"
    if not args:
        continue
    if isinstance(args, str):
        s += args + '\n'
    elif isinstance(args, dict):
        s += "A dictionary `{`  \n\n"
        for k,v in args.items():
            s += f"`'{k}'`: {v}\n\n"
        s += "`}`\n\n"
    else:
        nargs = len(args)
        for n in range(nargs):
            if isinstance(args[n], str):
                s += "1. " + args[n] + '\n'
            else:
                for i, arg in enumerate(args[n]):
                    s += f"{i+1}. " + arg + '\n'
            if n < (nargs - 2):
                s += "\n\nor\n\n"
    s += "\n**Examples**\n\n"
    if isinstance(demo, str):
        demo = [demo]
    for D in demo:
        print(D)
        s += "- `" + D + '`\n'

dst = src.replace('.json', '.md')
with open(dst, 'w') as f:
    f.write(s)
