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.gui.screen.Screen;
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.tooltip.TooltipType;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
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;

public class ModItems {
    public static final Item ORACLE_LEMON = registerItem("oracle_lemon", new OracleLemon(new Item.Settings().component(DataComponentTypes.POTION_CONTENTS, new PotionContentsComponent(ModPotions.LEMON))));
    public static final Item WAND = registerItem("wand", new Wand(new Item.Settings().maxDamage(32)));
    public static final Item RAW_ENTROPY = registerItem("raw_entropy", new Item(new Item.Settings()));
    public static final Item STABLE_ENTROPY = registerItem("stable_entropy", new Item(new Item.Settings()));
    public static final Item RAW_ENDTROPY = registerItem("raw_endtropy", new Item(new Item.Settings()));
    public static final Item STABLE_ENDTROPY = registerItem("stable_endtropy", new Item(new Item.Settings()));
    public static final Item INFERNAL_ASHES = registerItem("infernal_ashes", new Item(new Item.Settings()));
    public static final Item INFERNAL_BEEF = registerItem("infernal_beef",
            new Item(new Item.Settings()
                    .food(ModFoodComponent.INFERNAL_BEEF)
                    .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",
            new Item(new Item.Settings()
                    .food(ModFoodComponent.BRICKED_UP)
                    .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",
            new CombustibleLemon(new Item.Settings().maxCount(16)){
                @Override
                public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {
                    tooltip.add(Text.translatable("tooltip.acesbs.combustible_lemon1"));
                    tooltip.add(Text.translatable("tooltip.acesbs.combustible_lemon2"));
                    super.appendTooltip(stack, context, tooltip, type);
                }
            });

    public static final Item LEMON = registerItem("lemon", new Item(new Item.Settings().food(ModFoodComponent.LEMON)));
    public static final Item COOKED_LEMON = registerItem("cooked_lemon", new Item(new Item.Settings().food(ModFoodComponent.COOKED_LEMON)));
    public static final Item GREEN_BRICKS = registerItem("green_bricks", new Item(new Item.Settings()) {
        @Override
        public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {
            if(!Screen.hasShiftDown()){
                tooltip.add(Text.translatable("tooltip.acesbs.generic.shift_up"));
            }
            else {
                tooltip.add(Text.translatable("tooltip.acesbs.green_bricks"));
            }
            super.appendTooltip(stack, context, tooltip, type);
        }
    });

    public static final Item MOTE_OF_ORDER = registerItem("mote_of_order", new Item(new Item.Settings()) {
        public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {
            tooltip.add(Text.translatable("tooltip.acesbs.mote_of_order"));
            super.appendTooltip(stack, context, tooltip, type);
        }
    });

    public static final Item DUST_OF_ORDER = registerItem("dust_of_order", new Item(new Item.Settings()) {
        public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {
            tooltip.add(Text.translatable("tooltip.acesbs.dust_of_order"));
            super.appendTooltip(stack, context, tooltip, type);
        }
    });

    public static final Item INGOT_OF_ORDER = registerItem("ingot_of_order", new Item(new Item.Settings()) {
        public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {
            tooltip.add(Text.translatable("tooltip.acesbs.ingot_of_order"));
            super.appendTooltip(stack, context, tooltip, type);
        }
    });

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

    public static final Item KAUPEN_BOW = registerItem("kaupen_bow",
            new BowItem(new Item.Settings().maxDamage(500)) {
                @Override
                public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {

                    if(!Screen.hasShiftDown()){
                        tooltip.add(Text.translatable("tooltip.acesbs.generic.shift_up"));
                    }
                    else {
                        tooltip.add(Text.translatable("tooltip.acesbs.kaupen_bow"));
                        tooltip.add(Text.translatable("tooltip.acesbs.kaupen_bow_2"));
                    }
                    super.appendTooltip(stack, context, tooltip, type);
                }
            });

    public static final Item THOT_BEGONE = ModItems.registerItem("thot_begone",
            new SwordItem(ToolMaterials.WOOD, new Item.Settings()
                    .attributeModifiers(SwordItem.createAttributeModifiers(ToolMaterials.WOOD, 0, -1.4f))
                    .attributeModifiers(AttributeModifiersComponent.builder()
                            .add(EntityAttributes.GENERIC_ATTACK_KNOCKBACK,
                                    new EntityAttributeModifier(Identifier.of(AcesBS.MOD_ID, "generic.attack_knockback"), 25, EntityAttributeModifier.Operation.ADD_VALUE), AttributeModifierSlot.MAINHAND)
                            .add(EntityAttributes.GENERIC_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",
            new Item(new Item.Settings().jukeboxPlayable(ModSounds.CRAB_RAVE_KEY).maxCount(1)));

    public static final Item BERSERKERS_PAULDRON = registerItem("berserkers_pauldron",
            new ModArmorItem(ModArmorMaterials.BERSERKERS, ArmorItem.Type.CHESTPLATE, new Item.Settings()
                    .maxDamage(ArmorItem.Type.CHESTPLATE.getMaxDamage(15))
                    .attributeModifiers(AttributeModifiersComponent.builder()
                            .add(EntityAttributes.GENERIC_MAX_HEALTH,
                                    new EntityAttributeModifier(Identifier.of(AcesBS.MOD_ID, "generic.max_health"), 20, EntityAttributeModifier.Operation.ADD_VALUE), AttributeModifierSlot.CHEST)
                            .add(EntityAttributes.GENERIC_ARMOR,
                                    new EntityAttributeModifier(Identifier.of(AcesBS.MOD_ID, "generic.armor"), -20, EntityAttributeModifier.Operation.ADD_VALUE), AttributeModifierSlot.CHEST)
                            .add(EntityAttributes.GENERIC_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, List<Text> tooltip, TooltipType type) {
                    if(!Screen.hasShiftDown()){
                        tooltip.add(Text.translatable("tooltip.acesbs.berserkers_pauldron_desc"));
                        tooltip.add(Text.translatable("tooltip.acesbs.generic.shift_more"));
                    }
                    else {
                        tooltip.add(Text.translatable("tooltip.acesbs.berserkers_pauldron_thanks"));
                        tooltip.add(Text.translatable("tooltip.acesbs.thanks_sov"));
                    }
                    super.appendTooltip(stack, context, tooltip, type);
                }
            }
    );

    public static final Item SOVS_PLATE_CARRIER = ModItems.registerItem("sovs_plate_carrier",
            new SovsPlateCarrierArmorItem(ModArmorMaterials.PLATES, ArmorItem.Type.CHESTPLATE, new Item.Settings()
                    .maxDamage(6)){
                @Override
                public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {
                    if(!Screen.hasShiftDown()){
                        tooltip.add(Text.translatable("tooltip.acesbs.sovs_plate_carrier_desc"));
                        tooltip.add(Text.translatable("tooltip.acesbs.generic.shift_more"));
                    }
                    else {
                        tooltip.add(Text.translatable("tooltip.acesbs.sovs_plate_carrier_thanks"));
                        tooltip.add(Text.translatable("tooltip.acesbs.sovs_plate_carrier_desc2"));
                        tooltip.add(Text.translatable("tooltip.acesbs.sovs_plate_carrier_desc3"));
                    }
                    super.appendTooltip(stack, context, tooltip, type);
                }
            });

    public static final Item WILDFIRE_BOOTS = registerItem("wildfire_boots",
            new WildfireArmorItem(ModArmorMaterials.WILDFIRE_ARMOR_MATERIAL, ArmorItem.Type.BOOTS, new Item.Settings()
                    .maxDamage(ArmorItem.Type.BOOTS.getMaxDamage(15))){
                @Override
                public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {
                    if(!Screen.hasShiftDown()){
                        tooltip.add(Text.translatable("tooltip.acesbs.wildfire_boots_desc"));
                        tooltip.add(Text.translatable("tooltip.acesbs.generic.shift_more"));
                    }
                    else {
                        tooltip.add(Text.translatable("tooltip.acesbs.wildfire_boots_thanks"));
//                        tooltip.add(Text.translatable("tooltip.acesbs.thanks_sov"));
                    }
                    super.appendTooltip(stack, context, tooltip, type);
                }
            }
    );

    public static final Item TOBACCO_SEEDS = registerItem("tobacco_seeds", new AliasedBlockItem(ModBlocks.TOBACCO_CROP, new Item.Settings()));

    public static final Item TOBACCO = registerItem("tobacco", new Item(new Item.Settings()));

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

    public static final Item FREN_SPAWN_EGG = registerItem("fren_spawn_egg",
            new SpawnEggItem(ModEntities.FREN, 894731, 0, new Item.Settings()));
    public static final Item VULGAR_BONES_SPAWN_EGG = registerItem("vulgar_bones_spawn_egg",
            new SpawnEggItem(ModEntities.VULGAR_BONES, 13082270, 0, new Item.Settings()));



    public static Item registerItem(String name, Item item){
        return Registry.register(Registries.ITEM, Identifier.of(AcesBS.MOD_ID, name), item);
    }

    public static void registerModItems() {
        PrismaSteelItems.registerPrismaItems();
        AshenSteelItems.registerAshenItems();
        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().main) {
                    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);
//        });
    }
}
