import sys
import os
import shutil

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()

def GetUnderlineName(strl):
  return strl.lower().replace(',', '').replace('& ', '').replace('é', 'e').replace('-', '_').replace("'", '').replace('"', '').replace(' ', '_')
    
# folder creation
if not os.path.isdir('data/teyvatcraft/loot_tables/blocks'):
    os.makedirs('data/teyvatcraft/loot_tables/blocks')
if not os.path.isdir('assets/teyvatcraft/models/block'):
    os.makedirs('assets/teyvatcraft/models/block')
if not os.path.isdir('assets/teyvatcraft/models/item'):
    os.makedirs('assets/teyvatcraft/models/item')
if not os.path.isdir('assets/teyvatcraft/blockstates'):
    os.makedirs('assets/teyvatcraft/blockstates')
if not os.path.isdir('assets/teyvatcraft/textures/block'):
    os.makedirs('assets/teyvatcraft/textures/block')


# read file
enList = []
zhList = []

ReadListFromFile('zh.txt', zhList)
ReadListFromFile('en.txt', enList)

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

javaDeclareFile = open('declare.block.java', 'w', encoding='utf-8')
javaRegisterFile = open('register.block.java', 'w', encoding='utf-8')
langZhFile = open('zh_cn.block.json', 'w', encoding='utf-8')
langEnFile = open('en_us.block.json', 'w', encoding='utf-8')
for index in range(len(enList)):
    if enList[index] == '':
        # keep blank line in register & declare
        javaDeclareFile.write('\n')
        javaRegisterFile.write('\n')
        langZhFile.write('\n')
        langEnFile.write('\n')
        continue

    (droppedName, oreName) = enList[index].split('#')
    dropped_underlineName = GetUnderlineName(droppedName)
    ore_underlineName = GetUnderlineName(oreName)
    ore_upperName = ore_underlineName.upper()

    flootTables = open('data/teyvatcraft/loot_tables/blocks/' + ore_underlineName + '.json', 'w', encoding='utf-8')
    fblockstates = open('assets/teyvatcraft/blockstates/' + ore_underlineName + '.json', 'w', encoding='utf-8')
    fmodel = open('assets/teyvatcraft/models/block/{}.json'.format(ore_underlineName), 'w', encoding='utf-8')
    fmodelItem = open('assets/teyvatcraft/models/item/{}.json'.format(ore_underlineName), 'w', encoding='utf-8')
    
    flootTables.write('''{{
    "type": "minecraft:block",
    "pools": [
      {{
        "rolls": 1,
        "entries": [
          {{
            "type": "minecraft:alternatives",
            "children": [
              {{
                "type": "minecraft:item",
                "conditions": [
                  {{
                    "condition": "minecraft:match_tool",
                    "predicate": {{
                      "enchantments": [
                        {{
                          "enchantment": "minecraft:silk_touch",
                          "levels": {{
                            "min": 1
                          }}
                        }}
                      ]
                    }}
                  }}
                ],
                "name": "teyvatcraft:{}"
              }},
              {{
                "type": "minecraft:item",
                "functions": [
                  {{
                    "function": "minecraft:set_count",
                    "count": {{
                      "min": 1.0,
                      "max": 2.0,
                      "type": "minecraft:uniform"
                    }}
                  }},
                  {{
                    "function": "minecraft:apply_bonus",
                    "enchantment": "minecraft:fortune",
                    "formula": "minecraft:uniform_bonus_count",
                    "parameters": {{
                      "bonusMultiplier": 1
                    }}
                  }},
                  {{
                    "function": "minecraft:explosion_decay"
                  }}
                ],
                "name": "teyvatcraft:{}"
              }}
            ]
          }}
        ]
      }}
    ]
}}'''.format(ore_underlineName, dropped_underlineName))

    fblockstates.write('''{{
  "variants": {{
    "": {{
      "model": "teyvatcraft:block/{}"
    }}
  }}
}}'''.format(ore_underlineName))

    fmodel.write('''{{
    "parent": "minecraft:block/cube_all",
    "textures": {{
      "all": "teyvatcraft:block/{}"
    }}
}}'''.format(ore_underlineName))

    fmodelItem.write('''{{
  "parent": "teyvatcraft:block/{}"
}}'''.format(ore_underlineName))
    
    flootTables.close()
    fblockstates.close()
    fmodel.close()
    fmodelItem.close()

    javaDeclareFile.write('public static final Block {} = new OreBlock(getOreBlockSettings(1, 3.0f));\n'.format(ore_upperName))
    javaRegisterFile.write('Registry.register(Registry.BLOCK, new Identifier("teyvatcraft", "{}"), {});\n'.format(ore_underlineName, ore_upperName))

    langZhFile.write('"block.teyvatcraft.{}": "{}",\n'.format(ore_underlineName, zhList[index].split('#')[1]))
    langEnFile.write('"block.teyvatcraft.{}": "{}",\n'.format(ore_underlineName, enList[index].split('#')[1].replace('"', '')))

    shutil.copyfile('stone.png', 'assets/teyvatcraft/textures/block/{}.png'.format(ore_underlineName))


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