# IMPORT
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

# ====
# DIRS
# ====

migration_modifier_file = "trimmed_tool.json"
trimming_recipes_dir = "tooltrims/recipe/trimming/"

# =====
# LISTS
# =====

selected_to_run = {"generate_migration_modifier": False, "generate_recipes": True }

materials = ["amethyst", "copper", "diamond", "emerald", "gold", "iron", "lapis", "netherite", "quartz", "redstone"]
materials_without_resin = ["amethyst", "copper", "diamond", "emerald", "gold", "iron", "lapis", "netherite", "quartz", "redstone"]
material_names = ["amethyst_shard", "copper_ingot", "diamond", "emerald", "gold_ingot", "iron_ingot", "lapis_lazuli", "netherite_ingot", "quartz", "redstone"]
tools = ["netherite_sword","netherite_pickaxe","netherite_axe","netherite_shovel","netherite_hoe","diamond_sword","diamond_pickaxe","diamond_axe","diamond_shovel","diamond_hoe","iron_sword","iron_pickaxe","iron_axe","iron_shovel","iron_hoe","golden_sword","golden_pickaxe","golden_axe","golden_shovel","golden_hoe","stone_sword","stone_pickaxe","stone_axe","stone_shovel","stone_hoe","wooden_sword","wooden_pickaxe","wooden_axe","wooden_shovel","wooden_hoe","bow","crossbow","mace","trident"]
patterns = ["linear","tracks","charge","frost"]
template_items = [ "tadpole_spawn_egg", "silverfish_spawn_egg", "cod_spawn_egg", "snow_golem_spawn_egg" ]


# ===============================
# GEN LEGACY UPDATE ITEM MODIFIER
# ===============================

if selected_to_run["generate_migration_modifier"]:
  file_text = """[
  {
    "function": "minecraft:set_components",
    "components": {
      "!minecraft:custom_model_data": {},
      "!minecraft:custom_data": {}
    }
  },
  {
    "function": "minecraft:set_lore",
    "entity": "this",
    "lore": [],
    "mode": "replace_all"
  }"""

  count = 0
  for i in patterns:
      for j in materials_without_resin:
          count +=1
          if count < 10:
              count_string = "0" + str(count)
          else:
              count_string = str(count)
          file_text += f""",
  {{
    "function": "filtered",
    "item_filter": {{
      "items": "#tooltrims:migration/old_trimmable_tools",
      "components": {{ "minecraft:custom_model_data": {{ "floats": [ 3110{count_string}.0 ] }} }}
    }},
    "on_pass": {{
      "function": "set_components",
      "components": {{
        "minecraft:trim": {{
          "material": "minecraft:{j}",
          "pattern": "tooltrims:{i}"
        }}
      }}
    }}
  }}"""

  for i in patterns:
      count +=1
      if count < 10:
          count_string = "0" + str(count)
      else:
          count_string = str(count)
      file_text += f""",
  {{
    "function": "filtered",
    "item_filter": {{
      "items": "#tooltrims:migration/old_trimmable_tools",
      "components": {{ "minecraft:custom_model_data": {{ "floats": [ 3110{count_string}.0 ] }} }}
    }},
    "on_pass": {{
      "function": "set_components",
      "components": {{
        "minecraft:trim": {{
          "material": "minecraft:resin",
          "pattern": "tooltrims:{i}"
        }}
      }}
    }}
  }}"""

  file_text += "\n]"

  with open(migration_modifier_file, "+w") as file:
      file.write(file_text)



# ===========
# GEN RECIPES
# ===========

if selected_to_run["generate_recipes"]:
  for current_tool in tools:
      main_custom_model_data = 311000
      resin_custom_model_data = 311040
      custom_model_data = main_custom_model_data
  
      for current_pattern_number in range(len(patterns)):
          for current_material_number in range(len(materials)):
              
              if materials[current_material_number] == "resin":
                  resin_custom_model_data += 1
                  custom_model_data = resin_custom_model_data
              else:
                  if custom_model_data == 311040:
                      main_custom_model_data += 11
                  else:
                      main_custom_model_data += 1
                  custom_model_data = main_custom_model_data
  
              with open(f"{current_tool}_{patterns[current_pattern_number]}_{materials[current_material_number]}.json", "w+") as file:
                  file.write(
  f"""{{
    "type": "minecraft:smithing_transform",
    "template": {{
      "item": "minecraft:{template_items[current_pattern_number]}"
    }},
    "base": {{
      "item":  "minecraft:{current_tool}"
    }},
    "addition": {{
      "item": "minecraft:{material_names[current_material_number]}"
    }},
    "result": {{
      "id": "minecraft:{current_tool}",
      "count": 1,
      "components": {{
        "minecraft:trim": {{
          "pattern": "tooltrims:{patterns[current_pattern_number]}",
          "material": "minecraft:{materials[current_material_number]}"
        }},
        "minecraft:custom_model_data": {custom_model_data}
      }}
    }}
  }}""")