package com.drmangotea.tfmg.content.items.weapons.flamethrover;

import com.drmangotea.tfmg.TFMG;
import com.drmangotea.tfmg.TFMGClient;
import com.drmangotea.tfmg.base.spark.Spark;
import com.drmangotea.tfmg.registry.TFMGDataComponents;
import com.drmangotea.tfmg.registry.TFMGEntityTypes;
import com.simibubi.create.content.fluids.tank.FluidTankBlockEntity;
import com.simibubi.create.foundation.item.CustomArmPoseItem;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.UseAnim;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.HitResult;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
import org.jetbrains.annotations.Nullable;

public class FlamethrowerItem extends Item implements CustomArmPoseItem {


    public static final int FUEL_CAPACITY = 4000;


    public FlamethrowerItem(Properties pProperties) {
        super(pProperties);
    }


    public void onUseTick(Level level, LivingEntity entity, ItemStack stack, int time) {

        if(stack.get(TFMGDataComponents.AMOUNT)==null)
            stack.set(TFMGDataComponents.AMOUNT,0);

        Spark spark = TFMGEntityTypes.SPARK.create(level);
        spark.setPos(entity.getX(),entity.getY()+1.2f,entity.getZ());


        if(stack.get(TFMGDataComponents.AMOUNT)==0) {
            stack.set(TFMGDataComponents.FLAMETHROWER_FUEL,"");
            return;
        }


        level.playSound((Player)null, entity.getX(), entity.getY(), entity.getZ(), SoundEvents.FIRE_EXTINGUISH, SoundSource.NEUTRAL, 0.1F, 0.04F);

        FlamethrowerFuel fuel = Enum.valueOf(FlamethrowerFuel.class,stack.get(TFMGDataComponents.FLAMETHROWER_FUEL).toUpperCase());

        for(int i =0;i<fuel.amount;i++) {
            if(stack.get(TFMGDataComponents.AMOUNT)==0) {
                stack.set(TFMGDataComponents.FLAMETHROWER_FUEL,"");
                return;
            }
            stack.set(TFMGDataComponents.AMOUNT,stack.get(TFMGDataComponents.AMOUNT)-1);




            spark.shoot(entity.getLookAngle().x,entity.getLookAngle().y,entity.getLookAngle().z,fuel.speed,fuel.spread);

        level.addFreshEntity(spark);
        }

    }




    public int getUseDuration(ItemStack p_77626_1_) {
        return 696969;
    }

    @Override
    public boolean isBarVisible(ItemStack stack) {
        if(stack.get(TFMGDataComponents.AMOUNT)==null)
            return false;

        return stack.get(TFMGDataComponents.AMOUNT)!=0;
    }

    @Override
    public int getBarColor(ItemStack stack) {
        return stack.get(TFMGDataComponents.FLAMETHROWER_FUEL).isEmpty() ? 0xffffff :
        Enum.valueOf(FlamethrowerFuel.class,stack.get(TFMGDataComponents.FLAMETHROWER_FUEL).toUpperCase()).color;

    }

    @Override
    public int getBarWidth(ItemStack stack) {

        if(stack.get(TFMGDataComponents.AMOUNT)==null)
            stack.set(TFMGDataComponents.AMOUNT,0);

            return Math.round( 13* ((float)((float)stack.get(TFMGDataComponents.AMOUNT)/(float)FUEL_CAPACITY)));

    }

    public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
        player.startUsingItem(hand);



        if (level.isClientSide) {
            TFMGClient.FLAMETHROWER_RENDER_HANDLER.dontAnimateItem(hand);
        }

        return InteractionResultHolder.pass(player.getItemInHand(hand));
    }

    @Override
    public int getUseDuration(ItemStack stack, LivingEntity entity) {
        return 1000;
    }

    @Override
    public InteractionResult useOn(UseOnContext context) {

        Level level = context.getLevel();
        BlockPos pos = context.getClickedPos();
        ItemStack stack = context.getItemInHand();




        if(stack.get(TFMGDataComponents.FLAMETHROWER_FUEL)==null)
            stack.set(TFMGDataComponents.FLAMETHROWER_FUEL,"");
        if(stack.get(TFMGDataComponents.AMOUNT)==null)
            stack.set(TFMGDataComponents.AMOUNT,0);
        if(level.getBlockEntity(pos)!=null)
            if(level.getBlockEntity(pos) instanceof FluidTankBlockEntity fluidTankBe){

                FluidTankBlockEntity be = fluidTankBe.isController() ? fluidTankBe : fluidTankBe.getControllerBE();

            for(FlamethrowerFuel fuel : FlamethrowerFuel.values()) {

                String fluid = be.getFluid(0).getFluid().getFluidType().toString().replaceFirst("tfmg:","");


                if (fluid.equals(fuel.name().toLowerCase())) {

                    if(stack.get(TFMGDataComponents.FLAMETHROWER_FUEL).equals(fluid)||stack.get(TFMGDataComponents.AMOUNT)==0) {

                        int toDrain = Math.min(FUEL_CAPACITY - stack.get(TFMGDataComponents.AMOUNT), be.getFluid(0).getAmount());

                        stack.set(TFMGDataComponents.FLAMETHROWER_FUEL, fluid);
                        be.getTankInventory().drain(toDrain, IFluidHandler.FluidAction.EXECUTE);
                        stack.set(TFMGDataComponents.AMOUNT, stack.get(TFMGDataComponents.AMOUNT) + toDrain);
                        context.getPlayer().getCooldowns().addCooldown(stack.getItem(), 20);


                    }

                }

            }


            }



        return InteractionResult.PASS;
    }

    @Override
    @Nullable
    public HumanoidModel.ArmPose getArmPose(ItemStack stack, AbstractClientPlayer player, InteractionHand hand) {
        if (!player.swinging) {
            return HumanoidModel.ArmPose.CROSSBOW_HOLD;
        }
        return null;
    }


    @Override
    public UseAnim getUseAnimation(ItemStack pStack) {
        return UseAnim.NONE;
    }

    enum FlamethrowerFuel{

        GASOLINE(15,1,3,0xC4AA76),
        DIESEL(7,2,3,0xBA9177),
        KEROSENE(10,1.3f,4,0x7876D5),

        NAPHTHA(20,0.8f,1,0x5E1B0A),

        LPG(35,0.6f,15,0xE0BB48),

        NAPALM(20,1.8f,15,0xA3C649),

        MOLTEN_SLAG(15,0.3f,15,0xFF9621)


        ;

        public final float spread;
        public final float speed;
        public final int amount;

        public final int color;



        FlamethrowerFuel(float spread, float speed, int amount,int color){
            this.spread = spread;
            this.speed = speed;
            this.amount = amount;
            this.color = color;

        }

    }

}
