package net.halbear.supernova.registry.util;
import net.halbear.supernova.SuperNova;

import net.halbear.supernova.registry.blocks.ModFluids;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.util.RegistryKey;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraftforge.client.event.EntityViewRenderEvent;
import net.minecraftforge.event.entity.EntityEvent;
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Objects;

@Mod.EventBusSubscriber(modid = SuperNova.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)

public class ServerEventHandler {

    //SPACE STUFF
    //if you can't tell what these do by the function names... bro.. cmon
    @SubscribeEvent
    public static void FogDistance(EntityViewRenderEvent.FogDensity FogDensity) {
        PlayerEntity player = Minecraft.getInstance().player;
        double eyeHeight = player.getPosYEye() - 1 / 9d;
        FluidState fluid = player.world.getFluidState(new BlockPos(player.getPosX(), eyeHeight, player.getPosZ()));
        if (fluid.getFluid() == ModFluids.CRUDE_OIL_FLUID.get() || fluid.getFluid() == ModFluids.CRUDE_OIL_FLOWING.get()) {
            FogDensity.setDensity(1000f);
        }
        if (fluid.getFluid() == ModFluids.REFINED_OIL_FLUID.get() || fluid.getFluid() == ModFluids.REFINED_OIL_FLOWING.get()) {
            FogDensity.setDensity(1000f);
        }
        if (fluid.getFluid() == ModFluids.KEROSENE_FLUID.get() || fluid.getFluid() == ModFluids.KEROSENE_FLOWING.get()) {
            FogDensity.setDensity(10f);
        }
    }

    @SubscribeEvent
    public static void OilFog(EntityViewRenderEvent.FogColors FogColor) {
        PlayerEntity player = Minecraft.getInstance().player;
        double eyeHeight = player.getPosYEye() - 1 / 9d;
        FluidState fluid = player.world.getFluidState(new BlockPos(player.getPosX(), eyeHeight, player.getPosZ()));
        if (fluid.getFluid() == ModFluids.CRUDE_OIL_FLUID.get() || fluid.getFluid() == ModFluids.CRUDE_OIL_FLOWING.get()) {
            FogColor.setBlue(0);
            FogColor.setGreen(0);
            FogColor.setRed(0);
        }
        if (fluid.getFluid() == ModFluids.REFINED_OIL_FLUID.get() || fluid.getFluid() == ModFluids.REFINED_OIL_FLOWING.get()) {
            FogColor.setBlue(35);
            FogColor.setGreen(0);
            FogColor.setRed(5);
        }
        if (fluid.getFluid() == ModFluids.KEROSENE_FLUID.get() || fluid.getFluid() == ModFluids.KEROSENE_FLOWING.get()) {
            FogColor.setBlue(230);
            FogColor.setGreen(190);
            FogColor.setRed(0);
        }
    }

    @SubscribeEvent
    public static void PlayerGravityCheck(PlayerEvent.PlayerChangedDimensionEvent event) {
        Entity player = event.getPlayer();
        if (event.getFrom() == RegistryKey.getOrCreateKey(Registry.WORLD_KEY, new ResourceLocation("supernova:space"))) {
            player.setNoGravity(false);
        }
        if (event.getTo() == RegistryKey.getOrCreateKey(Registry.WORLD_KEY, new ResourceLocation("supernova:space"))) {
            player.setNoGravity(true);
        }
    }

    @SubscribeEvent
    public static void ChunkGravityCheck(EntityEvent.EnteringChunk chunkEvent){
        Entity entity = chunkEvent.getEntity();
        World world = entity.world;
        Biome entityBiome = world.getBiome(entity.getPosition());
        if (Objects.equals(entityBiome.getRegistryName(), new ResourceLocation("supernova:space")) && !entity.hasNoGravity()){
            entity.setNoGravity(true);
        } else if (!Objects.equals(entityBiome.getRegistryName(), new ResourceLocation("supernova:space")) && entity.hasNoGravity()) {
            entity.setNoGravity(false);
        }
    }

    @SubscribeEvent
    public static void EntityGravityCheck(LivingSpawnEvent SpawnEvent){
        Entity entity = SpawnEvent.getEntity();
        World world = entity.world;
        Biome entityBiome = world.getBiome(entity.getPosition());
        if (Objects.equals(entityBiome.getRegistryName(), new ResourceLocation("supernova:space")) && !entity.hasNoGravity()){
            entity.setNoGravity(true);
        }
    }
}
