/*
 *  Copyright 2025-2026 R3944Realms
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package top.r3944realms.eroticdungeongame.mixin.minecraft;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mojang.datafixers.DataFixer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.Services;
import net.minecraft.server.WorldStem;
import net.minecraft.server.level.progress.ChunkProgressListenerFactory;
import net.minecraft.server.packs.repository.PackRepository;
import net.minecraft.server.players.PlayerList;
import net.minecraft.world.level.storage.LevelStorageSource;
import org.jetbrains.annotations.NotNull;
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 top.r3944realms.eroticdungeongame.content.entity.npc.NPCPlayerList;
import top.r3944realms.eroticdungeongame.core.storage.NPCDataStorage;
import top.r3944realms.eroticdungeongame.util.IEDGLevelStorageAccess;
import top.r3944realms.eroticdungeongame.util.IEDGMinecraftServer;

import java.net.Proxy;

@Mixin(MinecraftServer.class)
public class MixinMinecraftServer implements IEDGMinecraftServer {

    @Unique
    private NPCDataStorage edg$npcDataStorage;
    @Unique
    private NPCPlayerList edg$npcPlayerList;
    @Inject(
            method = "<init>",
            at = @At("TAIL")
    )
    private void edg$init(Thread serverThread, LevelStorageSource.LevelStorageAccess storageSource, PackRepository packRepository, WorldStem worldStem, Proxy proxy, DataFixer fixerUpper, Services services, ChunkProgressListenerFactory progressListenerFactory, CallbackInfo ci) {
        if (storageSource instanceof IEDGLevelStorageAccess iedgLevelStorageAccess) {
            edg$npcDataStorage = iedgLevelStorageAccess.createNPCPlayerDataStorage();
        }
    }

    @SuppressWarnings("AddedMixinMembersNamePattern")
    @Override
    public NPCPlayerList getNPCPlayerList() {
        return edg$npcPlayerList;
    }

    @SuppressWarnings("AddedMixinMembersNamePattern")
    @Override
    public void setNPCPlayerList(NPCPlayerList playerList) {
        this.edg$npcPlayerList = playerList;
    }

    @SuppressWarnings("AddedMixinMembersNamePattern")
    @Override
    public NPCDataStorage getNPCDataStorage() {
        return edg$npcDataStorage;
    }

    @WrapOperation(
            method = "saveEverything",
            at = @At(
                    value = "INVOKE",
                    target = "Lnet/minecraft/server/players/PlayerList;saveAll()V"
            )
    )
    void edg$saveEverything$saveAll(PlayerList instance, @NotNull Operation<Void> original) {
        original.call(instance);
        edg$npcPlayerList.saveAll();
    }
    @WrapOperation(
            method = "stopServer",
            at = @At(
                    value = "INVOKE",
                    target = "Lnet/minecraft/server/players/PlayerList;saveAll()V"
            )
    )
    void edg$stopServer$saveAll(PlayerList instance, @NotNull Operation<Void> original) {
        original.call(instance);
        edg$npcPlayerList.saveAll();
        edg$npcPlayerList.removeAll();
    }

    @WrapOperation(
            method = "lambda$reloadResources$26",
            at = @At(
                    value = "INVOKE",
                    target = "Lnet/minecraft/server/players/PlayerList;reloadResources()V"
            )
    )
    void edg$reloadResources$saveAll(PlayerList instance, @NotNull Operation<Void> original) {
        original.call(instance);
        edg$npcPlayerList.saveAll();
        edg$npcPlayerList.removeAll();
    }
    @WrapOperation(
            method = "tickChildren",
            at = @At(
                    value = "INVOKE",
                    target = "Lnet/minecraft/server/players/PlayerList;tick()V"
            )
    )
    void edg$tickChildren$tick(PlayerList instance, @NotNull Operation<Void> original) {
        original.call(instance);
        edg$npcPlayerList.tick();
    }
}
