/*
 *  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.content.entity.npc;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mojang.authlib.GameProfile;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.LayeredRegistryAccess;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Connection;
import net.minecraft.network.chat.ChatType;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.PlayerChatMessage;
import net.minecraft.network.protocol.Packet;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerAdvancements;
import net.minecraft.server.RegistryLayer;
import net.minecraft.server.ServerScoreboard;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.*;
import net.minecraft.stats.ServerStatsCounter;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.r3944realms.eroticdungeongame.EroticDungeon;

import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;

public class FakePlayerList extends PlayerList {
    private final List<NPCServerPlayer> npcs = new ArrayList<>();
    private final Map<UUID, NPCServerPlayer> npcsByUUID = Maps.newHashMap();
    private int sendAllNPCInfoIn;
    private final List<ServerPlayer> npcsView = java.util.Collections.unmodifiableList(npcs);
    @SuppressWarnings("DataFlowIssue")
    public FakePlayerList(MinecraftServer server, LayeredRegistryAccess<RegistryLayer> registries, List<NPCServerPlayer> npcs, Map<UUID, NPCServerPlayer> npcsByUUID) {
        super(server, registries, null, -1);
        this.npcs.addAll(npcs);
        this.npcsByUUID.putAll(npcsByUUID);
    }

    private static void noSupportOperate() {
        EroticDungeon.getLogger().error("Not supported in FakePlayerList");
    }

    @Override
    public void placeNewPlayer(@NotNull Connection netManager, @NotNull ServerPlayer player) {
        noSupportOperate();
    }

    @Override
    protected void updateEntireScoreboard(@NotNull ServerScoreboard scoreboard, @NotNull ServerPlayer player) {
        noSupportOperate();
    }

    @Override
    public void addWorldborderListener(@NotNull ServerLevel level) {
        noSupportOperate();
    }

    @Nullable
    @Override
    public CompoundTag load(@NotNull ServerPlayer player) {
        noSupportOperate();
        return null;
    }

    @Override
    protected void save(@NotNull ServerPlayer player) {
        noSupportOperate();
    }

    @Override
    public void remove(@NotNull ServerPlayer player) {
        noSupportOperate();
    }

    @Nullable
    @Override
    public Component canPlayerLogin(@NotNull SocketAddress socketAddress, @NotNull GameProfile gameProfile) {
        noSupportOperate();
        return null;
    }

    @Override
    public @NotNull ServerPlayer getPlayerForLogin(@NotNull GameProfile profile) {
        noSupportOperate();
        return super.getPlayerForLogin(profile);
    }

    @Override
    public @NotNull ServerPlayer respawn(@NotNull ServerPlayer player, boolean keepEverything) {
        noSupportOperate();
        return super.respawn(player, keepEverything);
    }

    @Override
    public void sendPlayerPermissionLevel(@NotNull ServerPlayer player) {
        noSupportOperate();
    }

    @Override
    public void tick() {
        noSupportOperate();
    }

    @Override
    public void broadcastAll(@NotNull Packet<?> packet) {
        noSupportOperate();
    }

    @Override
    public void broadcastAll(@NotNull Packet<?> packet, @NotNull ResourceKey<Level> dimension) {
        noSupportOperate();
    }

    @Override
    public void broadcastSystemToTeam(@NotNull Player player, @NotNull Component message) {
        noSupportOperate();
    }

    @Override
    public void broadcastSystemToAllExceptTeam(@NotNull Player player, @NotNull Component message) {
        noSupportOperate();
    }

    @Override
    public String @NotNull [] getPlayerNamesArray() {
        noSupportOperate();
        return super.getPlayerNamesArray();
    }

    @Override
    public @NotNull UserBanList getBans() {
        noSupportOperate();
        return super.getBans();
    }

    @Override
    public @NotNull IpBanList getIpBans() {
        noSupportOperate();
        return super.getIpBans();
    }

    @Override
    public void op(@NotNull GameProfile profile) {
        noSupportOperate();
    }

    @Override
    public void deop(@NotNull GameProfile profile) {
        noSupportOperate();
    }

    @Override
    public boolean isWhiteListed(@NotNull GameProfile profile) {
        noSupportOperate();
        return super.isWhiteListed(profile);
    }

    @Override
    public boolean isOp(@NotNull GameProfile profile) {
        noSupportOperate();
        return super.isOp(profile);
    }

    @Nullable
    @Override
    public ServerPlayer getPlayerByName(@NotNull String username) {
        return super.getPlayerByName(username);
    }

    @Override
    public void broadcast(@Nullable Player except, double x, double y, double z, double radius, @NotNull ResourceKey<Level> dimension, @NotNull Packet<?> packet) {
        noSupportOperate();
    }

    @Override
    public void saveAll() {
        noSupportOperate();
    }

    @Override
    public @NotNull UserWhiteList getWhiteList() {
        noSupportOperate();
        return super.getWhiteList();
    }

    @Override
    public String @NotNull [] getWhiteListNames() {
        noSupportOperate();
        return super.getWhiteListNames();
    }

    @Override
    public @NotNull ServerOpList getOps() {
        noSupportOperate();
        return super.getOps();
    }

    @Override
    public String @NotNull [] getOpNames() {
        noSupportOperate();
        return super.getOpNames();
    }

    @Override
    public void reloadWhiteList() {
        noSupportOperate();
    }

    @Override
    public void sendLevelInfo(@NotNull ServerPlayer player, @NotNull ServerLevel level) {
        noSupportOperate();
    }

    @Override
    public void sendAllPlayerInfo(@NotNull ServerPlayer player) {
        noSupportOperate();
    }

    @Override
    public int getPlayerCount() {
        return this.npcs.size();
    }

    @Override
    public int getMaxPlayers() {
        noSupportOperate();
        return super.getMaxPlayers();
    }

    @Override
    public boolean isUsingWhitelist() {
        noSupportOperate();
        return super.isUsingWhitelist();
    }

    @Override
    public void setUsingWhiteList(boolean whitelistEnabled) {
        noSupportOperate();
        super.setUsingWhiteList(whitelistEnabled);
    }

    @Override
    public @NotNull List<ServerPlayer> getPlayersWithAddress(@NotNull String address) {
        List<ServerPlayer> list = Lists.newArrayList();

        for(ServerPlayer serverplayer : this.npcs) {
            if (serverplayer.getIpAddress().equals(address)) {
                list.add(serverplayer);
            }
        }

        return list;
    }

    @Override
    public int getViewDistance() {
        noSupportOperate();
        return super.getViewDistance();
    }

    @Override
    public int getSimulationDistance() {
        noSupportOperate();
        return super.getSimulationDistance();
    }

    @Override
    public @NotNull MinecraftServer getServer() {
        return super.getServer();
    }

    @Nullable
    @Override
    public CompoundTag getSingleplayerData() {
        noSupportOperate();
        return null;
    }

    @Override
    public void setAllowCheatsForAllPlayers(boolean allowCheatsForAllPlayers) {
        noSupportOperate();
    }

    @Override
    public void removeAll() {
        noSupportOperate();
    }

    @Override
    public void broadcastSystemMessage(@NotNull Component message, boolean bypassHiddenChat) {
        noSupportOperate();
    }

    @Override
    public void broadcastSystemMessage(@NotNull Component serverMessage, @NotNull Function<ServerPlayer, Component> playerMessageFactory, boolean bypassHiddenChat) {
        noSupportOperate();
    }

    @Override
    public void broadcastChatMessage(@NotNull PlayerChatMessage message, @NotNull CommandSourceStack sender, ChatType.@NotNull Bound boundChatType) {
        noSupportOperate();
    }

    @Override
    public void broadcastChatMessage(@NotNull PlayerChatMessage message, @NotNull ServerPlayer sender, ChatType.@NotNull Bound boundChatType) {
        noSupportOperate();
    }

    @Override
    public @NotNull ServerStatsCounter getPlayerStats(@NotNull Player player) {
        noSupportOperate();
        return super.getPlayerStats(player);
    }

    @Override
    public @NotNull PlayerAdvancements getPlayerAdvancements(@NotNull ServerPlayer player) {
        noSupportOperate();
        return super.getPlayerAdvancements(player);
    }

    @Override
    public void setViewDistance(int viewDistance) {
        noSupportOperate();
    }

    @Override
    public void setSimulationDistance(int simulationDistance) {
        noSupportOperate();
    }

    @Override
    public @NotNull List<ServerPlayer> getPlayers() {
        return this.npcsView;
    }

    @Nullable
    @Override
    public ServerPlayer getPlayer(@NotNull UUID playerUUID) {
        return npcsByUUID.get(playerUUID);
    }

    @Override
    public boolean canBypassPlayerLimit(@NotNull GameProfile profile) {
        noSupportOperate();
        return super.canBypassPlayerLimit(profile);
    }

    @Override
    public void reloadResources() {
        noSupportOperate();
    }

    @Override
    public boolean isAllowCheatsForAllPlayers() {
        noSupportOperate();
        return super.isAllowCheatsForAllPlayers();
    }
}
