package com.zivilon.cinder_loe.mixins;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

import lotr.common.inventory.LOTRContainerAnvil;
import lotr.common.LOTRMod;
import lotr.common.item.LOTRMaterial;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.item.ItemTool;

import com.zivilon.cinder_loe.CinderLoE;

import java.util.Objects;

@Mixin(LOTRContainerAnvil.class)
public class MixinLOTRContainerAnvil {

    /**
     * @author Shinare
     * @reason KeyLime
     */
    @Overwrite(remap = false)
    public boolean isRepairMaterial(ItemStack inputItem, ItemStack materialItem) {
        if (inputItem.getItem().getIsRepairable(inputItem, materialItem))
            return true;
        Item item = inputItem.getItem();
        if (item == Items.bow && LOTRMod.rohanBow.getIsRepairable(inputItem, materialItem))
            return true;
        if (item instanceof net.minecraft.item.ItemFishingRod && materialItem.getItem() == Items.string)
            return true;
        if (item instanceof net.minecraft.item.ItemShears && materialItem.getItem() == Items.iron_ingot)
            return true;
        if (item instanceof lotr.common.item.LOTRItemChisel && materialItem.getItem() == Items.iron_ingot)
            return true;
        if (item instanceof net.minecraft.item.ItemEnchantedBook && materialItem.getItem() == Items.paper)
            return true;
        Item.ToolMaterial material = null;
        if (item instanceof ItemTool) {
            material = Item.ToolMaterial.valueOf(((ItemTool)item).getToolMaterialName());
        } else if (item instanceof ItemSword) {
            material = Item.ToolMaterial.valueOf(((ItemSword)item).getToolMaterialName());
        }
        if (material == Item.ToolMaterial.WOOD || material == LOTRMaterial.MOREDAIN_WOOD.toToolMaterial())
            return LOTRMod.isOreNameEqual(materialItem, "plankWood");
        if (material == LOTRMaterial.MALLORN.toToolMaterial())
            return (materialItem.getItem() == Item.getItemFromBlock(LOTRMod.planks) && materialItem.getItemDamage() == 1);
        if (material == LOTRMaterial.MALLORN_MACE.toToolMaterial())
            return (materialItem.getItem() == Item.getItemFromBlock(LOTRMod.wood) && materialItem.getItemDamage() == 1);
        if (material == CinderLoE.MATERIAL_RED_DWARF.toToolMaterial())
            return (materialItem.getItem() == CinderLoE.redDwarfSteel);
        if (material == CinderLoE.MATERIAL_ASH.toToolMaterial())
            return (materialItem.getItem() == CinderLoE.ingotAsh);
        if (material == CinderLoE.MATERIAL_BONEMOLD.toToolMaterial())
            return (materialItem.getItem() == CinderLoE.bonemold);
        if (material == CinderLoE.EVENT.toToolMaterial())
            return LOTRMod.isOreNameEqual(materialItem, "ice");
        if (material == CinderLoE.MATERIAL_BREE.toToolMaterial())
            return (materialItem.getItem() == Items.iron_ingot);
        if (item instanceof ItemArmor armor) {
            ItemArmor.ArmorMaterial armorMaterial = armor.getArmorMaterial();
            if (armorMaterial == LOTRMaterial.BONE.toArmorMaterial())
                return LOTRMod.isOreNameEqual(materialItem, "bone");
            if (armorMaterial == CinderLoE.MATERIAL_RED_DWARF.toArmorMaterial())
                return (materialItem.getItem() == CinderLoE.redDwarfSteel);
            if (armorMaterial == CinderLoE.MATERIAL_LIMWAITH_WOOD.toArmorMaterial())
                return (materialItem.getItem() == Item.getItemFromBlock(LOTRMod.planks) && materialItem.getItemDamage() == 15);
            if (armorMaterial == CinderLoE.MATERIAL_BREE.toArmorMaterial())
                return (materialItem.getItem() == Items.iron_ingot);
            if (armorMaterial == CinderLoE.MATERIAL_BATTLENUN.toArmorMaterial())
                return (materialItem.getItem() == Items.iron_ingot);
            if (armorMaterial == CinderLoE.MATERIAL_BONEMOLD.toArmorMaterial())
                return (materialItem.getItem() == CinderLoE.bonemold);
            if (armorMaterial == CinderLoE.MATERIAL_RHUDUAR.toArmorMaterial())
                return (materialItem.getItem() == Items.iron_ingot);
            if (armorMaterial == CinderLoE.MATERIAL_JADE.toArmorMaterial())
                return (materialItem.getItem() == LOTRMod.emerald);
        }

        return false;
    }
}
