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 GenerateSweetBerryBushStageModel(name, stage):
    f = open('assets/teyvatcraft/models/block/{}_stage{}.json'.format(name, stage), 'w', encoding='utf-8')
    f.write('''{{
  "parent": "minecraft:block/cross",
  "textures": {{
    "cross": "teyvatcraft:block/{}_stage{}"
  }}
}}'''.format(name, stage))
    f.close()
    
# 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/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 = []
propList = []

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

if len(zhList) != len(enList) or len(propList) != len(enList):
    print('3 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)):
    underlineName = enList[index].lower().replace(',', '').replace('& ', '').replace('é', 'e').replace('-', '_').replace("'", '').replace('"', '').replace(' ', '_')
    upperName = underlineName.upper()

    if propList[index] == '':
        # keep blank line in register & declare
        javaDeclareFile.write('\n')
        javaRegisterFile.write('\n')
        langZhFile.write('\n')
        langEnFile.write('\n')
        continue

    flootTables = open('data/teyvatcraft/loot_tables/blocks/' + underlineName + '.json', 'w', encoding='utf-8')
    fblockstates = open('assets/teyvatcraft/blockstates/' + underlineName + '.json', 'w', encoding='utf-8')
    if propList[index] == 'sweet_berry_bush':
        javaDeclareFile.write('public static final Block {} = new TeyvatSweetBerryBushLikePlantBlock(ItemsManager.{});\n'.format(upperName, upperName))

        flootTables.write('''{{
  "type": "minecraft:block",
  "pools": [
    {{
      "rolls": 1.0,
      "entries": [
        {{
          "type": "minecraft:item",
          "name": "teyvatcraft:{}"
        }}
      ],
      "conditions": [
        {{
          "condition": "minecraft:block_state_property",
          "block": "teyvatcraft:{}",
          "properties": {{
            "age": "3"
          }}
        }}
      ],
      "functions": [
        {{
          "function": "minecraft:set_count",
          "count": {{
            "min": 2.0,
            "max": 3.0,
            "type": "minecraft:uniform"
          }}
        }},
        {{
          "function": "minecraft:apply_bonus",
          "enchantment": "minecraft:fortune",
          "formula": "minecraft:uniform_bonus_count",
          "parameters": {{
            "bonusMultiplier": 1
          }}
        }}
      ]
    }},
    {{
      "rolls": 1.0,
      "entries": [
        {{
          "type": "minecraft:item",
          "name": "teyvatcraft:{}"
        }}
      ],
      "conditions": [
        {{
          "condition": "minecraft:block_state_property",
          "block": "teyvatcraft:{}",
          "properties": {{
            "age": "2"
          }}
        }}
      ],
      "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
          }}
        }}
      ]
    }}
  ],
  "functions": [
    {{
      "function": "minecraft:explosion_decay"
    }}
  ]
}}'''.format(*([underlineName, ]*4)))

        fblockstates.write('''{{
  "variants": {{
    "age=0": {{
      "model": "teyvatcraft:block/{}_stage0"
    }},
    "age=1": {{
      "model": "teyvatcraft:block/{}_stage1"
    }},
    "age=2": {{
      "model": "teyvatcraft:block/{}_stage2"
    }},
    "age=3": {{
      "model": "teyvatcraft:block/{}_stage3"
    }}
  }}
}}'''.format(*([underlineName, ]*4)))

        GenerateSweetBerryBushStageModel(underlineName, 0)
        GenerateSweetBerryBushStageModel(underlineName, 1)
        GenerateSweetBerryBushStageModel(underlineName, 2)
        GenerateSweetBerryBushStageModel(underlineName, 3)

        #shutil.copyfile('sweet_berry_bush_stage0.png', 'assets/teyvatcraft/textures/block/{}_stage0.png'.format(underlineName))
        #shutil.copyfile('sweet_berry_bush_stage1.png', 'assets/teyvatcraft/textures/block/{}_stage1.png'.format(underlineName))
        #shutil.copyfile('sweet_berry_bush_stage2.png', 'assets/teyvatcraft/textures/block/{}_stage2.png'.format(underlineName))
        #shutil.copyfile('sweet_berry_bush_stage3.png', 'assets/teyvatcraft/textures/block/{}_stage3.png'.format(underlineName))

    elif propList[index] == 'flower':
        fmodel = open('assets/teyvatcraft/models/block/{}.json'.format(underlineName), 'w', encoding='utf-8')
        javaDeclareFile.write('public static final Block {} = new TeyvatFlowerLikePlantBlock();\n'.format(upperName))

        flootTables.write('''{{
  "type": "minecraft:block",
  "pools": [
    {{
      "rolls": 1,
      "entries": [
        {{
          "type": "minecraft:item",
          "name": "teyvatcraft:{}"
        }}
      ],
      "conditions": [
        {{
          "condition": "minecraft:survives_explosion"
        }}
      ]
    }}
  ]
}}'''.format(underlineName))

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

        fmodel.write('''{{
  "parent": "minecraft:block/cross",
  "textures": {{
    "cross": "teyvatcraft:block/{}"
  }}
}}'''.format(underlineName))
        fmodel.close()

        #shutil.copyfile('flower.png', 'assets/teyvatcraft/textures/block/{}.png'.format(underlineName))

    flootTables.close()
    fblockstates.close()

    javaRegisterFile.write('registerTeyvatPlant({}, "{}");\n'.format(upperName, underlineName))

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


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