import sys
import os

def ReadListFromFile(filename, listInstance):
    f = open(filename, 'r', encoding='utf-8')
    while True:
        cache = f.readline()
        if cache == '':
            break;
        cache = cache.strip()
        if cache == '':
            listInstance.append('')
            continue
        if cache.startswith('//'):
            continue
        listInstance.append(cache)
    f.close()
    
# folder creation
if not os.path.isdir('models/item'):
    os.makedirs('models/item')
if not os.path.isdir('textures/item'):
    os.makedirs('textures/item')


# read file
enList = []
zhList = []
urlList = []

ReadListFromFile('zhcn.txt', zhList)
ReadListFromFile('enus.txt', enList)
ReadListFromFile('url.txt', urlList)

if len(zhList) != len(enList) or len(urlList) != len(enList):
    print('3 files item is not matched')
    sys.exit(0)

javaDeclareFile = open('declare.java', 'w', encoding='utf-8')
javaRegisterFile = open('register.java', 'w', encoding='utf-8')
langZhFile = open('zh_cn.json', 'w', encoding='utf-8')
langEnFile = open('en_us.json', 'w', encoding='utf-8')
shFile = open('downloadImage.sh', 'w', encoding='utf-8')
for index in range(len(enList)):
    underlineName = enList[index].lower().replace(' ', '_').replace('-', '').replace("'", '')
    upperName = underlineName.upper()

    if enList[index] != '':
        fmodels = open('models/item/' + underlineName + '.json', 'w', encoding='utf-8')
        fmodels.write('''{{
"parent": "minecraft:item/generated",
"textures": {{
    "layer0": "teyvatcraft:item/{}"
}}
}}'''.format(underlineName))
        fmodels.close()

        javaDeclareFile.write('public static final Item {} = new Item(new FabricItemSettings().group(ITEM_GROUP));\n'.format(upperName))
        javaRegisterFile.write('Registry.register(Registry.ITEM, new Identifier("teyvatcraft", "{}"), {});\n'.format(underlineName, upperName))

        langZhFile.write('"item.teyvatcraft.{}": "{}",\n'.format(underlineName, zhList[index]))
        langEnFile.write('"item.teyvatcraft.{}": "{}",\n'.format(underlineName, enList[index]))

        shFile.write('curl -o textures/item/{}.png {}\n'.format(underlineName, urlList[index]))

    else:
        # keep blank line in register & declare
        javaDeclareFile.write('\n')
        javaRegisterFile.write('\n')
        langZhFile.write('\n')
        langEnFile.write('\n')

javaDeclareFile.close()
javaRegisterFile.close()
langZhFile.close()
langEnFile.close()
shFile.close()
