package com.acethewildfire.acesbs.item;

import com.acethewildfire.acesbs.AcesBS;
import com.acethewildfire.acesbs.block.ModBlocks;
import com.acethewildfire.acesbs.component.ModDataComponentTypes;
import com.acethewildfire.acesbs.entity.ModEntities;
import com.acethewildfire.acesbs.item.custom.*;
import com.acethewildfire.acesbs.potion.ModPotions;
import com.acethewildfire.acesbs.sounds.ModSounds;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.component.ComponentType;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.*;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.item.*;
import net.minecraft.item.equipment.EquipmentType;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;

import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;

public class ModItems {
    public static final Item ORACLE_LEMON = registerItem("oracle_lemon", setting -> new OracleLemon(setting.component(DataComponentTypes.POTION_CONTENTS, new PotionContentsComponent(ModPotions.LEMON))));
    public static final Item WAND = registerItem("wand", setting -> new Wand(setting.maxDamage(32)));
    public static final Item RAW_ENTROPY = registerItem("raw_entropy", Item::new);
    public static final Item STABLE_ENTROPY = registerItem("stable_entropy", Item::new);
    public static final Item RAW_ENDTROPY = registerItem("raw_endtropy", Item::new);
    public static final Item STABLE_ENDTROPY = registerItem("stable_endtropy", Item::new);
    public static final Item INFERNAL_ASHES = registerItem("infernal_ashes", Item::new);
    public static final Item INFERNAL_BEEF = registerItem("infernal_beef",
            setting -> new Item(setting
                    .food(ModFoodComponent.INFERNAL_BEEF, ModFoodComponent.INFERNAL_FIRE_EFFECT)
                    .fireproof()
                    .component(DataComponentTypes.LORE, new LoreComponent(
                            List.of(
                                    Text.literal("Brimstone fed").formatted(Formatting.DARK_RED, Formatting.ITALIC),
                                    Text.literal("Non-holy").formatted(Formatting.DARK_RED, Formatting.ITALIC)
                            )
                    ))
            ));
    public static final Item BRICKED_UP = registerItem("bricked_up",
            setting -> new Item(setting
                    .food(ModFoodComponent.BRICKED_UP, ModFoodComponent.BRICKED_UP_EFFECT)
                    .component(DataComponentTypes.LORE, new LoreComponent(
                            List.of(
                                    Text.literal("Straight to God").formatted(Formatting.GOLD, Formatting.ITALIC)
                            )
                    ))
            ));

    public static final Item COMBUSTIBLE_LEMON = registerItem("combustible_lemon",
            setting -> new CombustibleLemon(setting.maxCount(16)){
                @Override
                public void appendTooltip(ItemStack stack, TooltipContext context, TooltipDisplayComponent displayComponent, Consumer<Text> textConsumer, TooltipType type) {
                    textConsumer.accept(Text.translatable("tooltip.acesbs.combustible_lemon1"));
                    textConsumer.accept(Text.translatable("tooltip.acesbs.combustible_lemon2"));
                    super.appendTooltip(stack, context, displayComponent, textConsumer, type);
                }
            });

    public static final Item LEMON = registerItem("lemon", setting -> new Item(setting.food(ModFoodComponent.LEMON, ModFoodComponent.LEMON_EFFECT)));
    public static final Item COOKED_LEMON = registerItem("cooked_lemon", setting -> new Item(setting.food(ModFoodComponent.COOKED_LEMON)));
    public static final Item GREEN_BRICKS = registerItem("green_bricks", setting -> new Item(setting) {
        @Override
        public void appendTooltip(ItemStack stack, TooltipContext context, TooltipDisplayComponent displayComponent, Consumer<Text> textConsumer, TooltipType type) {
            if(!MinecraftClient.getInstance().isShiftPressed()){
                textConsumer.accept(Text.translatable("tooltip.acesbs.generic.shift_up"));
            }
            else {
                textConsumer.accept(Text.translatable("tooltip.acesbs.green_bricks"));
            }
            super.appendTooltip(stack, context, displayComponent, textConsumer, type);
        }
    });

    public static final Item MOTE_OF_ORDER = registerItem("mote_of_order", setting -> new Item(setting) {
        public void appendTooltip(ItemStack stack, TooltipContext context, TooltipDisplayComponent displayComponent, Consumer<Text> textConsumer, TooltipType type) {
            textConsumer.accept(Text.translatable("tooltip.acesbs.mote_of_order"));
            super.appendTooltip(stack, context, displayComponent, textConsumer, type);
        }
    });

    public static final Item DUST_OF_ORDER = registerItem("dust_of_order", setting -> new Item(setting) {
        public void appendTooltip(ItemStack stack, TooltipContext context, TooltipDisplayComponent displayComponent, Consumer<Text> textConsumer, TooltipType type) {
            textConsumer.accept(Text.translatable("tooltip.acesbs.dust_of_order"));
            super.appendTooltip(stack, context, displayComponent, textConsumer, type);
        }
    });

    public static final Item INGOT_OF_ORDER = registerItem("ingot_of_order", setting -> new Item(setting) {
        public void appendTooltip(ItemStack stack, TooltipContext context, TooltipDisplayComponent displayComponent, Consumer<Text> textConsumer, TooltipType type) {
            textConsumer.accept(Text.translatable("tooltip.acesbs.ingot_of_order"));
            super.appendTooltip(stack, context, displayComponent, textConsumer, type);
        }
    });

    public static final Item COMMAND_CORE = registerItem("command_core", setting -> new CommandCore(setting.maxCount(1)));

    public static final Item KAUPEN_BOW = registerItem("kaupen_bow",
            setting -> new BowItem(setting.maxDamage(500)) {
                @Override
                public void appendTooltip(ItemStack stack, TooltipContext context, TooltipDisplayComponent displayComponent, Consumer<Text> textConsumer, TooltipType type) {

                    if(!MinecraftClient.getInstance().isShiftPressed()){
                        textConsumer.accept(Text.translatable("tooltip.acesbs.generic.shift_up"));
                    }
                    else {
                        textConsumer.accept(Text.translatable("tooltip.acesbs.kaupen_bow"));
                        textConsumer.accept(Text.translatable("tooltip.acesbs.kaupen_bow_2"));
                    }
                    super.appendTooltip(stack, context, displayComponent, textConsumer, type);
                }
            });

    public static final Item THOT_BEGONE = ModItems.registerItem("thot_begone",
            setting -> new Item(setting.sword(ToolMaterial.WOOD, 0, -1.4f).attributeModifiers(
                    AttributeModifiersComponent.builder()
                            .add(EntityAttributes.ATTACK_KNOCKBACK, new EntityAttributeModifier(Identifier.of(AcesBS.MOD_ID, "attack.attack_knockback"), 25, EntityAttributeModifier.Operation.ADD_VALUE), AttributeModifierSlot.MAINHAND)
                            .add(EntityAttributes.ATTACK_DAMAGE, new EntityAttributeModifier(Identifier.of(AcesBS.MOD_ID, "generic.attack_damage"), -0.999, EntityAttributeModifier.Operation.ADD_MULTIPLIED_TOTAL), AttributeModifierSlot.MAINHAND)
                            .build()
            )));

    public static final Item CRAB_RAVE_MUSIC_DISC = registerItem("crab_rave_music_disc",
            setting -> new Item(setting.jukeboxPlayable(ModSounds.CRAB_RAVE_KEY).maxCount(1)));



    public static final Item BERSERKERS_PAULDRON = registerItem("berserkers_pauldron",
            setting -> new ModArmorItem(setting
                    .armor(ModArmorMaterials.BERSERKERS_ARMOR_MATERIAL, EquipmentType.CHESTPLATE)
                    .maxDamage(EquipmentType.CHESTPLATE.getMaxDamage(15))
                    .attributeModifiers(AttributeModifiersComponent.builder()
                            .add(EntityAttributes.MAX_HEALTH,
                                    new EntityAttributeModifier(Identifier.of(AcesBS.MOD_ID, "generic.max_health"), 20, EntityAttributeModifier.Operation.ADD_VALUE), AttributeModifierSlot.CHEST)
                            .add(EntityAttributes.ARMOR,
                                    new EntityAttributeModifier(Identifier.of(AcesBS.MOD_ID, "generic.armor"), -20, EntityAttributeModifier.Operation.ADD_VALUE), AttributeModifierSlot.CHEST)
                            .add(EntityAttributes.KNOCKBACK_RESISTANCE,
                                    new EntityAttributeModifier(Identifier.of(AcesBS.MOD_ID, "generic.knockback_resistance"), 1.0f, EntityAttributeModifier.Operation.ADD_VALUE), AttributeModifierSlot.CHEST)
                            .build())){
                @Override
                public void appendTooltip(ItemStack stack, TooltipContext context, TooltipDisplayComponent displayComponent, Consumer<Text> textConsumer, TooltipType type) {
                    if(!MinecraftClient.getInstance().isShiftPressed()){
                        textConsumer.accept(Text.translatable("tooltip.acesbs.berserkers_pauldron_desc"));
                        textConsumer.accept(Text.translatable("tooltip.acesbs.generic.shift_more"));
                    }
                    else {
                        textConsumer.accept(Text.translatable("tooltip.acesbs.berserkers_pauldron_thanks"));
                        textConsumer.accept(Text.translatable("tooltip.acesbs.thanks_sov"));
                    }
                    super.appendTooltip(stack, context, displayComponent, textConsumer, type);
                }
            }
    );

    public static final Item SOVS_PLATE_CARRIER = ModItems.registerItem("sovs_plate_carrier",
            setting -> new SovsPlateCarrierArmorItem(setting
                    .armor(ModArmorMaterials.PLATES_ARMOR_MATERIAL, EquipmentType.CHESTPLATE)
                    .maxDamage(6)){
                @Override
                public void appendTooltip(ItemStack stack, TooltipContext context, TooltipDisplayComponent displayComponent, Consumer<Text> textConsumer, TooltipType type) {
                    if(!MinecraftClient.getInstance().isShiftPressed()){
                        textConsumer.accept(Text.translatable("tooltip.acesbs.sovs_plate_carrier_desc"));
                        textConsumer.accept(Text.translatable("tooltip.acesbs.generic.shift_more"));
                    }
                    else {
                        textConsumer.accept(Text.translatable("tooltip.acesbs.sovs_plate_carrier_thanks"));
                        textConsumer.accept(Text.translatable("tooltip.acesbs.sovs_plate_carrier_desc2"));
                        textConsumer.accept(Text.translatable("tooltip.acesbs.sovs_plate_carrier_desc3"));
                    }
                    super.appendTooltip(stack, context, displayComponent, textConsumer, type);
                }
            });

    public static final Item WILDFIRE_BOOTS = registerItem("wildfire_boots",
            setting -> new WildfireArmorItem(setting
                    .armor(ModArmorMaterials.WILDFIRE_ARMOR_MATERIAL, EquipmentType.BOOTS)
                    .maxDamage(EquipmentType.BOOTS.getMaxDamage(15))){
                @Override
                public void appendTooltip(ItemStack stack, TooltipContext context, TooltipDisplayComponent displayComponent, Consumer<Text> textConsumer, TooltipType type) {
                    if(!MinecraftClient.getInstance().isShiftPressed()){
                        textConsumer.accept(Text.translatable("tooltip.acesbs.wildfire_boots_desc"));
                        textConsumer.accept(Text.translatable("tooltip.acesbs.generic.shift_more"));
                    }
                    else {
                        textConsumer.accept(Text.translatable("tooltip.acesbs.wildfire_boots_thanks"));
//                        textConsumer.accept(Text.translatable("tooltip.acesbs.thanks_sov"));
                    }
                    super.appendTooltip(stack, context, displayComponent, textConsumer, type);
                }
            }
    );

    public static final Item TOBACCO_SEEDS = registerItem("tobacco_seeds", setting -> new BlockItem(ModBlocks.TOBACCO_CROP, setting));

    public static final Item TOBACCO = registerItem("tobacco", setting -> new Item(setting));

    public static final Item CIGARETTE = registerItem("cigarette", setting -> new Cigarette(setting.maxCount(20)));
    public static final Item CIGARETTE_LEMON = registerItem("cigarette_lemon", setting -> new CigaretteLemon(setting.maxCount(20)));
    public static final Item CIGARETTE_FUNNY = registerItem("cigarette_funny", setting -> new CigaretteFunny(setting.maxCount(20)));
    public static final Item BUBBLE_PIPE = registerItem("bubble_pipe", setting -> new BubblePipe(setting.maxCount(1)));
    public static final Item AQUARIUM_GRAVEL = registerItem("aquarium_gravel", setting -> new AquariumGravel(setting.maxCount(1)));

    public static final Item FREN_SPAWN_EGG = registerItem("fren_spawn_egg",
            setting -> new SpawnEggItem(setting.spawnEgg(ModEntities.FREN)));
    public static final Item VULGAR_BONES_SPAWN_EGG = registerItem("vulgar_bones_spawn_egg",
            setting -> new SpawnEggItem(setting.spawnEgg(ModEntities.VULGAR_BONES)));



    public static Item registerItem(String name, Function<Item.Settings, Item> function) {
        return Registry.register(Registries.ITEM, Identifier.of(AcesBS.MOD_ID, name),
                function.apply(new Item.Settings().registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(AcesBS.MOD_ID, name)))));
    }

    public static void registerModItems() {
        PrismaSteelItems.registerPrismaItems();
        AshenSteelItems.registerAshenItems();
        FractalDiamondItems.registerFractalItems();
        HellfireDiamondItems.registerHellfireItems();

        AcesBS.LOGGER.info("Registering Mod Items for " + AcesBS.MOD_ID);

        AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {

            // To you who reads this now, weep, as I did when I created this monster whom I have become
            if (!world.isClient() && entity instanceof LivingEntity target) {
                for (ItemStack stack : player.getInventory().getMainStacks()) {
                    if (stack.isOf(ModItems.COMMAND_CORE)) {
                        if (Boolean.TRUE.equals(stack.get(ModDataComponentTypes.COMMAND_ACTIVE))){
                            if (Objects.equals(stack.get(ModDataComponentTypes.COMMAND), "Omnipotence")){
                                // 30 seconds = 600 ticks
                                target.addStatusEffect(
                                        new StatusEffectInstance(StatusEffects.GLOWING, 600, 0)
                                );
                            }
                        };
                    }
                }
            }

            return ActionResult.PASS;
        });

//        ItemGroupEvents.modifyEntriesEvent(ItemGroups.INGREDIENTS).register(fabricItemGroupEntries -> {
//            fabricItemGroupEntries.add(ORACLE_LEMON);
//        });
    }
}
