package com.acethewildfire.acesbs.item.custom;

import com.acethewildfire.acesbs.component.ModDataComponentTypes;
import com.acethewildfire.acesbs.effect.ModEffects;
import com.acethewildfire.acesbs.screen.custom.CommandCoreScreen;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;

import java.util.List;

public class CommandCore extends Item {
    public CommandCore(Settings settings) {
        super(settings);
    }

    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
        ItemStack itemStack = user.getStackInHand(hand);

        if (user.isInSneakingPose()){
            if (Boolean.TRUE.equals(itemStack.get(ModDataComponentTypes.COMMAND_ACTIVE))){
                itemStack.set(ModDataComponentTypes.COMMAND_ACTIVE, false);
                itemStack.set(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, false);
            } else {
                itemStack.set(ModDataComponentTypes.COMMAND_ACTIVE, true);
                itemStack.set(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, true);
            }
        } else {
            if (world.isClient) {
                // Open the selection screen client-side
                MinecraftClient.getInstance().setScreen(
                        new CommandCoreScreen(itemStack)
                );
            }
        }


        return TypedActionResult.success(user.getStackInHand(hand));
    }

    @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.command_core"));
        }

        if(stack.get(ModDataComponentTypes.COMMAND) != null) {
            tooltip.add(Text.literal("Command: " + stack.get(ModDataComponentTypes.COMMAND)));
        } else {
            tooltip.add(Text.literal("Command: None"));
        }

        super.appendTooltip(stack, context, tooltip, type);
    }

    @Override
    public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
        super.inventoryTick(stack, world, entity, slot, selected);

        String command = stack.get(ModDataComponentTypes.COMMAND);

        LivingEntity livingEntity = (LivingEntity) entity;

        if (Boolean.TRUE.equals(stack.get(ModDataComponentTypes.COMMAND_ACTIVE))){
            switch (command) {
                case "TEST" -> { entity.setOnFireFor(1); }
                case "Sight" -> { if (!world.isClient()) livingEntity.removeStatusEffect(StatusEffects.BLINDNESS); }
                case "Immunity" -> {
                    if (!world.isClient()) {
                        livingEntity.removeStatusEffect(StatusEffects.POISON);
                        livingEntity.removeStatusEffect(StatusEffects.WITHER);
                        livingEntity.removeStatusEffect(ModEffects.LEMON);
                    }
                }
                case "Immovable" -> { if (!world.isClient()) livingEntity.removeStatusEffect(StatusEffects.LEVITATION); }
                case null, default -> {}
            }
        }
    }
}
