package com.acethewildfire.acesbs.effect;

import com.acethewildfire.acesbs.AcesBS;
import com.acethewildfire.acesbs.sounds.ModSounds;
import net.minecraft.entity.Flutterer;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectCategory;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

public class BrickedUpEffect extends StatusEffect {
    protected BrickedUpEffect(StatusEffectCategory category, int color) {
        super(category, color);
    }

    @Override
    public Text getName() {
        return Text.translatable(this.getTranslationKey());
    }

    @Override
    public boolean applyUpdateEffect(ServerWorld world, LivingEntity entity, int amplifier) {

        var effect = entity.getStatusEffect(ModEffects.BRICKED_UP);
        if (effect == null) return false;

        int durationLeft = effect.getDuration();
        int idealLen = 160;

        if(durationLeft >= idealLen - 1){
            if (!world.isClient()){
                    world.playSound(
                            null,
                            entity.getBlockPos(),
                            ModSounds.BRICKED_UP,
                            SoundCategory.PLAYERS,
                            2.0f,
                            1.0f
                    );
            }
        }
        else if(durationLeft > idealLen - 120){
            Vec3d vel = entity.getVelocity();
            float p = entity.getEntityWorld().getBlockState(entity.getBlockPos()).getBlock().getSlipperiness();
            float f = entity.isOnGround() ? p * 0.91F : 0.91F;
            Vec3d vec3d6 = entity.getVelocity();
            double q = vec3d6.y;
            q += (0.05 * 10 - vec3d6.y) * 0.2;

            entity.setVelocity(vec3d6.x * (double)f, this instanceof Flutterer ? q * (double)f : q * 0.9800000190734863, vec3d6.z * (double)f);
//            entity.setVelocity(vel.x, vel.y + 0.08, vel.z);
            entity.velocityModified = true;
        }
//        else if (durationLeft < idealLen - 80 && durationLeft > idealLen - 120) {
//
//            // Freeze in place
//            entity.setVelocity(0, 0, 0);
//
////            float p = entity.getWorld().getBlockState(entity.getBlockPos()).getBlock().getSlipperiness();
////            float f = entity.isOnGround() ? p * 0.91F : 0.91F;
////            Vec3d vec3d6 = entity.applyMovementInput(entity.getMovement(), p);
////            double q = vec3d6.y;
////            q += (0.05 * 7 - vec3d6.y) * 0.2;
////
////            entity.setVelocity(vec3d6.x * (double)f, this instanceof Flutterer ? q * (double)f : q * 0.9800000190734863, vec3d6.z * (double)f);
//        }
//        else if (durationLeft < idealLen - 120 && durationLeft > idealLen - 122) {
//            world.playSound(entity, entity.getBlockPos(), ModSounds.FART, SoundCategory.PLAYERS, 5f, 1f);
//        }
        else {
            float p = entity.getEntityWorld().getBlockState(entity.getBlockPos()).getBlock().getSlipperiness();
            float f = entity.isOnGround() ? p * 0.91F : 0.91F;
            Vec3d vec3d6 = entity.getVelocity();
            double q = vec3d6.y;
            q += (0.05 * 250 - vec3d6.y) * 0.2;

            entity.setVelocity(vec3d6.x * (double)f, this instanceof Flutterer ? q * (double)f : q * 0.9800000190734863, vec3d6.z * (double)f);
//            entity.setVelocity(vel.x, vel.y + 2, vel.z);
            entity.velocityModified = true;
        }


        return super.applyUpdateEffect(world, entity, amplifier);

    }

    @Override
    public boolean canApplyUpdateEffect(int duration, int amplifier) {
        return true;
    }

}
