/*
 * SPDX-FileCopyrightText: 2026 KOKA99CAB
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

package net.koka99cab.portals.mixin;

import net.koka99cab.portals.world.ModDimensions;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.Mth;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.SpawnGroupData;
import net.minecraft.world.entity.animal.sheep.Sheep;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.level.ServerLevelAccessor;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(Sheep.class)
public abstract class SheepColorMixin {
	@Inject(method = "finalizeSpawn", at = @At("RETURN"))
	private void portals$randomizeSheepLandColor(ServerLevelAccessor level, DifficultyInstance difficulty, EntitySpawnReason spawnReason, SpawnGroupData groupData, CallbackInfoReturnable<SpawnGroupData> callbackInfo) {
		ServerLevel serverLevel = level.getLevel();

		if (serverLevel.dimension() != ModDimensions.SHEEP_LAND) {
			return;
		}

		if (spawnReason != EntitySpawnReason.NATURAL && spawnReason != EntitySpawnReason.CHUNK_GENERATION) {
			return;
		}

		Sheep sheep = (Sheep)(Object)this;
		long seed = sheep.getUUID().getMostSignificantBits() ^ sheep.getUUID().getLeastSignificantBits() ^ sheep.blockPosition().asLong();
		int colorIndex = Mth.positiveModulo(Mth.murmurHash3Mixer((int)(seed ^ seed >>> 32)), DyeColor.VALUES.size());
		sheep.setColor(DyeColor.VALUES.get(colorIndex));
	}
}
