package asm.mixins;

import dev.crucifixbeta.CrucifixBeta;
import dev.crucifixbeta.api.events.impl.EntitySpawnEvent;
import dev.crucifixbeta.api.events.impl.EntitySpawnedEvent;
import dev.crucifixbeta.api.events.impl.RemoveEntityEvent;
import dev.crucifixbeta.api.events.impl.TickEntityEvent;
import dev.crucifixbeta.mod.modules.impl.render.Ambience;
import dev.crucifixbeta.mod.modules.impl.render.NoRender;
import java.awt.Color;
import java.util.function.Supplier;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.render.DimensionEffects;
import net.minecraft.client.render.DimensionEffects.Overworld;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
import net.minecraft.entity.Entity.RemovalReason;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.profiler.Profiler;
import net.minecraft.world.MutableWorldProperties;
import net.minecraft.world.World;
import net.minecraft.world.dimension.DimensionType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Environment(EnvType.CLIENT)
@Mixin({ClientWorld.class})
public abstract class MixinClientWorld extends World {
   @Unique
   private final DimensionEffects overworld = new Overworld();

   protected MixinClientWorld(
      MutableWorldProperties properties,
      RegistryKey<World> registryRef,
      DynamicRegistryManager registryManager,
      RegistryEntry<DimensionType> dimensionEntry,
      Supplier<Profiler> profiler,
      boolean isClient,
      boolean debugWorld,
      long biomeAccess,
      int maxChainedNeighborUpdates
   ) {
      super(properties, registryRef, registryManager, dimensionEntry, profiler, isClient, debugWorld, biomeAccess, maxChainedNeighborUpdates);
   }

   @Inject(
      method = {"tickEntity"},
      at = {@At("HEAD")},
      cancellable = true
   )
   public void onTickEntity(Entity entity, CallbackInfo ci) {
      TickEntityEvent var3 = TickEntityEvent.get(entity);
      CrucifixBeta.EVENT_BUS.post(var3);
      if (var3.isCancelled()) {
         ci.cancel();
      }
   }

   @Inject(
      method = {"addEntity"},
      at = {@At("HEAD")},
      cancellable = true
   )
   public void onAddEntity(Entity entity, CallbackInfo ci) {
      EntitySpawnEvent var3 = EntitySpawnEvent.get(entity);
      CrucifixBeta.EVENT_BUS.post(var3);
      if (var3.isCancelled()) {
         ci.cancel();
      }
   }

   @Inject(
      method = {"removeEntity"},
      at = {@At("HEAD")}
   )
   private void hookRemoveEntity(int entityId, RemovalReason removalReason, CallbackInfo ci) {
      Entity var4 = this.getEntityById(entityId);
      if (var4 != null) {
         RemoveEntityEvent var5 = RemoveEntityEvent.get(var4, removalReason);
         CrucifixBeta.EVENT_BUS.post(var5);
      }
   }

   @Inject(
      method = {"addEntity"},
      at = {@At("TAIL")}
   )
   public void onAddEntityTail(Entity entity, CallbackInfo ci) {
      EntitySpawnedEvent var3 = EntitySpawnedEvent.get(entity);
      CrucifixBeta.EVENT_BUS.post(var3);
   }

   @Inject(
      method = {"getSkyColor"},
      at = {@At("HEAD")},
      cancellable = true
   )
   private void onGetSkyColor(Vec3d cameraPos, float tickDelta, CallbackInfoReturnable<Vec3d> info) {
      if (Ambience.INSTANCE.isOn() && Ambience.INSTANCE.sky.booleanValue) {
         Color var4 = Ambience.INSTANCE.sky.getValue();
         info.setReturnValue(new Vec3d((double)var4.getRed() / 255.0, (double)var4.getGreen() / 255.0, (double)var4.getBlue() / 255.0));
      }
   }

   @Inject(
      method = {"getCloudsColor"},
      at = {@At("HEAD")},
      cancellable = true
   )
   private void hookGetCloudsColor(float tickDelta, CallbackInfoReturnable<Vec3d> cir) {
      if (Ambience.INSTANCE.isOn() && Ambience.INSTANCE.cloud.booleanValue) {
         Color var3 = Ambience.INSTANCE.cloud.getValue();
         cir.setReturnValue(new Vec3d((double)var3.getRed() / 255.0, (double)var3.getGreen() / 255.0, (double)var3.getBlue() / 255.0));
      }
   }

   @Inject(
      method = {"getDimensionEffects"},
      at = {@At("HEAD")},
      cancellable = true
   )
   private void onGetSkyProperties(CallbackInfoReturnable<DimensionEffects> info) {
      if (Ambience.INSTANCE.isOn() && Ambience.INSTANCE.forceOverworld.getValue()) {
         info.setReturnValue(this.overworld);
      }
   }

   @Override
   public float getRainGradient(float delta) {
      if (NoRender.INSTANCE.isOn() && NoRender.INSTANCE.weather.getValue()) {
         return 0.0F;
      } else {
         if (Ambience.INSTANCE != null) {
            float var2 = Ambience.INSTANCE.getWeatherGradient();
            if (var2 >= 0.0F) {
               return var2;
            }
         }

         return super.getRainGradient(delta);
      }
   }

   @Override
   public float getThunderGradient(float delta) {
      if (NoRender.INSTANCE.isOn() && NoRender.INSTANCE.weather.getValue()) {
         return 0.0F;
      } else {
         return Ambience.INSTANCE != null && Ambience.INSTANCE.shouldControlWeather() ? 0.0F : super.getThunderGradient(delta);
      }
   }
}
