package com.r3944realms.leashedplayer.content.items.repcipe;

import com.r3944realms.leashedplayer.content.items.ModItemRegister;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.CraftingBookCategory;
import net.minecraft.world.item.crafting.CraftingInput;
import net.minecraft.world.item.crafting.CustomRecipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class TippedLeashRopeArrowRecipe {
    public static class TippedLeashRopeArrowARecipe extends CustomRecipe {
        public TippedLeashRopeArrowARecipe(CraftingBookCategory pCategory) {
            super(pCategory);
        }

        public boolean matches(CraftingInput pInput, @NotNull Level pLevel) {
            if (pInput.width() == 3 && pInput.height() == 3) {
                for (int i = 0; i < pInput.height(); i++) {
                    for (int j = 0; j < pInput.width(); j++) {
                        ItemStack itemstack = pInput.getItem(j, i);
                        if (itemstack.isEmpty()) {
                            return false;
                        }

                        if (j == 1 && i == 1) {
                            if (!itemstack.is(Items.LINGERING_POTION)) {
                                return false;
                            }
                        } else if (!itemstack.is(ModItemRegister.LEASH_ROPE_ARROW.get())) {
                            return false;
                        }
                    }
                }

                return true;
            } else {
                return false;
            }
        }

        public @NotNull ItemStack assemble(CraftingInput pInput, HolderLookup.@NotNull Provider pRegistries) {
            ItemStack itemstack = pInput.getItem(1, 1);
            if (!itemstack.is(Items.LINGERING_POTION)) {
                return ItemStack.EMPTY;
            } else {
                ItemStack itemstack1 = new ItemStack(ModItemRegister.TIPPED_LEASH_ROPE_ARROW.get(), 8);
                itemstack1.set(DataComponents.POTION_CONTENTS, itemstack.get(DataComponents.POTION_CONTENTS));
                return itemstack1;
            }
        }

        /**
         * Used to determine if this recipe can fit in a grid of the given width/height
         */
        @Override
        public boolean canCraftInDimensions(int pWidth, int pHeight) {
            return pWidth >= 3 && pHeight >= 3;
        }

        @Override
        public @NotNull RecipeSerializer<?> getSerializer() {
            return ModRecipeRegister.TIPPED_LEASH_ROPE_ARROW_A_RECIPE.get();
        }
        @Override
        public @NotNull ItemStack getResultItem(@Nullable HolderLookup.Provider pRegistries) {
            return new ItemStack(ModItemRegister.TIPPED_LEASH_ROPE_ARROW.get());
        }
        public @NotNull ItemStack getResultItem() {
            return getResultItem(null);
        }
    }
    public static class TippedLeashRopeArrowBRecipe extends CustomRecipe {

        public TippedLeashRopeArrowBRecipe(CraftingBookCategory pCategory) {
            super(pCategory);
        }

        @Override
        public boolean matches(CraftingInput pInput, @NotNull Level pLevel) {
            int tipped_arrow_count = 0, lead_count = 0;
            for (int i = 0; i < pInput.width(); i++) {
                for (int j = 0; j < pInput.height(); j++) {
                    if (pInput.getItem(i, j).is(Items.TIPPED_ARROW))
                        tipped_arrow_count++;
                    else if(pInput.getItem(i, j).is(Items.LEAD))
                        lead_count++;
                }
            }
            return tipped_arrow_count == 1 && lead_count == 1;
        }

        @Override
        public @NotNull ItemStack assemble(@NotNull CraftingInput pInput, HolderLookup.@NotNull Provider pRegistries) {
            ItemStack tipped_arrow = null;
            NODE:for (int i = 0; i < pInput.width(); i++) {
                for (int j = 0; j < pInput.height(); j++) {
                    if (pInput.getItem(i, j).is(Items.TIPPED_ARROW)) {
                        tipped_arrow = pInput.getItem(i, j);
                        break NODE;
                    }
                }
            }
            if(tipped_arrow != null) {
                ItemStack itemstack1 = new ItemStack(ModItemRegister.TIPPED_LEASH_ROPE_ARROW.get(), 1);
                itemstack1.set(DataComponents.POTION_CONTENTS, tipped_arrow.get(DataComponents.POTION_CONTENTS));
                return itemstack1;
            }
            return ItemStack.EMPTY;
        }

        @Override
        public boolean canCraftInDimensions(int pWidth, int pHeight) {
            return pWidth >= 2 && pHeight >= 2;
        }

        @Override
        public @NotNull RecipeSerializer<?> getSerializer() {
            return ModRecipeRegister.TIPPED_LEASH_ROPE_ARROW_B_RECIPE.get();
        }

        @Override
        public @NotNull ItemStack getResultItem(@Nullable HolderLookup.Provider pRegistries) {
            return new ItemStack(ModItemRegister.TIPPED_LEASH_ROPE_ARROW.get());
        }
        public @NotNull ItemStack getResultItem() {
            return getResultItem(null);
        }
    }
}
