package me.km.overrides;

import com.google.common.collect.Lists;
import com.mojang.authlib.GameProfile;
import io.netty.buffer.Unpooled;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import me.km.Server;
import me.km.utils.ReflectionUtils;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.ServerPlayNetHandler;
import net.minecraft.network.play.server.SChangeGameStatePacket;
import net.minecraft.network.play.server.SCustomPayloadPlayPacket;
import net.minecraft.network.play.server.SHeldItemChangePacket;
import net.minecraft.network.play.server.SJoinGamePacket;
import net.minecraft.network.play.server.SPlayEntityEffectPacket;
import net.minecraft.network.play.server.SPlayerAbilitiesPacket;
import net.minecraft.network.play.server.SPlayerListItemPacket;
import net.minecraft.network.play.server.SRespawnPacket;
import net.minecraft.network.play.server.SServerDifficultyPacket;
import net.minecraft.network.play.server.SSetExperiencePacket;
import net.minecraft.network.play.server.SSpawnPositionPacket;
import net.minecraft.network.play.server.STagsListPacket;
import net.minecraft.network.play.server.SUpdateRecipesPacket;
import net.minecraft.potion.EffectInstance;
import net.minecraft.server.dedicated.DedicatedPlayerList;
import net.minecraft.server.dedicated.DedicatedServer;
import net.minecraft.server.management.DemoPlayerInteractionManager;
import net.minecraft.server.management.PlayerInteractionManager;
import net.minecraft.server.management.PlayerList;
import net.minecraft.server.management.PlayerProfileCache;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.GameRules;
import net.minecraft.world.IWorld;
import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.dimension.DimensionType;
import net.minecraft.world.storage.WorldInfo;
import org.apache.logging.log4j.LogManager;

public final class ModPlayerList extends DedicatedPlayerList {
    private final DedicatedServer server;
    private final List<ServerPlayerEntity> players;
    private final Map<UUID, ServerPlayerEntity> uuidToPlayerMap;

    public ModPlayerList(DedicatedServer server) {
        super(server);
        this.server = server;
        this.players = getPlayers();
        this.uuidToPlayerMap = ReflectionUtils.getFieldValue(Map.class, PlayerList.class, this, "field_177454_f");
    }

    @Override
    public void initializeConnectionToPlayer(NetworkManager netManager, ServerPlayerEntity p) {
        GameProfile profile = p.getGameProfile();
        PlayerProfileCache cache = this.server.getPlayerProfileCache();
        cache.addEntry(profile);
        CompoundNBT compoundnbt = this.readPlayerDataFromFile(p);

        ServerWorld sw = this.server.getWorld(p.dimension);
        if(sw == null) {
            p.dimension = DimensionType.OVERWORLD;
            sw = this.server.getWorld(p.dimension);
            p.setPosition(sw.getWorldInfo().getSpawnX(), sw.getWorldInfo().getSpawnY(), sw.getWorldInfo().getSpawnZ());
        }
        p.setWorld(sw);
        p.interactionManager.setWorld((ServerWorld) p.world);
        String s1 = "local";
        if(netManager.getRemoteAddress() != null) {
            s1 = netManager.getRemoteAddress().toString();
        }

        LogManager.getLogger().info("{}[{}] logged in with entity id {} at ({}, {}, {})", p.getName().getString(), s1, p.getEntityId(), p.getPosX(), p.getPosY(), p.getPosZ());
        WorldInfo wi = sw.getWorldInfo();
        setPlayerGameTypeBasedOnOther(p, null, sw);
        ServerPlayNetHandler spnh = new ServerPlayNetHandler(server, netManager, p);
        net.minecraftforge.fml.network.NetworkHooks.sendMCRegistryPackets(netManager, "PLAY_TO_CLIENT");
        net.minecraftforge.fml.network.NetworkHooks.sendDimensionDataPacket(netManager, p);
        GameRules gamerules = sw.getGameRules();
        spnh.sendPacket(new SJoinGamePacket(p.getEntityId(), p.interactionManager.getGameType(), WorldInfo.byHashing(wi.getSeed()), wi.isHardcore(), sw.dimension.getType(), this.getMaxPlayers(), wi.getGenerator(), getViewDistance(), gamerules.getBoolean(GameRules.REDUCED_DEBUG_INFO), !gamerules.getBoolean(GameRules.DO_IMMEDIATE_RESPAWN)));
        spnh.sendPacket(new SCustomPayloadPlayPacket(SCustomPayloadPlayPacket.BRAND, (new PacketBuffer(Unpooled.buffer())).writeString(this.getServer().getServerModName())));
        spnh.sendPacket(new SServerDifficultyPacket(wi.getDifficulty(), wi.isDifficultyLocked()));
        spnh.sendPacket(new SPlayerAbilitiesPacket(p.abilities));
        spnh.sendPacket(new SHeldItemChangePacket(p.inventory.currentItem));
        spnh.sendPacket(new SUpdateRecipesPacket(server.getRecipeManager().getRecipes()));
        spnh.sendPacket(new STagsListPacket(server.getNetworkTagManager()));
        this.updatePermissionLevel(p);
        p.getStats().markAllDirty();
        p.getRecipeBook().init(p);
        this.sendScoreboard(sw.getScoreboard(), p);
        this.server.refreshStatusNextTick();

        spnh.setPlayerLocation(p.getPosX(), p.getPosY(), p.getPosZ(), p.rotationYaw, p.rotationPitch);
        this.addPlayer(p);
        this.uuidToPlayerMap.put(p.getUniqueID(), p);
        this.sendPacketToAllPlayers(new SPlayerListItemPacket(SPlayerListItemPacket.Action.ADD_PLAYER, p));

        for(int i = 0; i < this.players.size(); i++) {
            p.connection.sendPacket(new SPlayerListItemPacket(SPlayerListItemPacket.Action.ADD_PLAYER, players.get(i)));
        }

        sw.addNewPlayer(p);
        server.getCustomBossEvents().onPlayerLogin(p);
        sendWorldInfo(p, sw);
        if(!this.server.getResourcePackUrl().isEmpty()) {
            p.loadResourcePack(server.getResourcePackUrl(), server.getResourcePackHash());
        }

        for(EffectInstance effectinstance : p.getActivePotionEffects()) {
            spnh.sendPacket(new SPlayEntityEffectPacket(p.getEntityId(), effectinstance));
        }

        if(compoundnbt != null && compoundnbt.contains("RootVehicle", 10)) {
            CompoundNBT compoundnbt1 = compoundnbt.getCompound("RootVehicle");
            final ServerWorld worldf = sw;
            Entity entity1 = EntityType.func_220335_a(compoundnbt1.getCompound("Entity"), sw, (p_217885_1_) -> {
                return !worldf.summonEntity(p_217885_1_) ? null : p_217885_1_;
            });
            if(entity1 != null) {
                UUID uuid = compoundnbt1.getUniqueId("Attach");
                if(entity1.getUniqueID().equals(uuid)) {
                    p.startRiding(entity1, true);
                } else {
                    for(Entity entity : entity1.getRecursivePassengers()) {
                        if(entity.getUniqueID().equals(uuid)) {
                            p.startRiding(entity, true);
                            break;
                        }
                    }
                }

                if(!p.isPassenger()) {
                    LogManager.getLogger().warn("Couldn't reattach entity to player");
                    p.removeEntity(entity1);

                    for(Entity entity2 : entity1.getRecursivePassengers()) {
                        sw.removeEntity(entity2);
                    }
                }
            }
        }

        p.addSelfToInternalCraftingInventory();
        net.minecraftforge.fml.hooks.BasicEventHooks.firePlayerLoggedIn(p);
    }

    @Override
    public ServerPlayerEntity createPlayerForUser(GameProfile profile) {
        UUID uuid = PlayerEntity.getUUID(profile);
        List<ServerPlayerEntity> list = Lists.newArrayList();

        for(int i = 0; i < this.players.size(); i++) {
            ServerPlayerEntity p = this.players.get(i);
            if(p.getUniqueID().equals(uuid)) {
                list.add(p);
            }
        }

        ServerPlayerEntity p2 = this.uuidToPlayerMap.get(profile.getId());
        if(p2 != null && !list.contains(p2)) {
            list.add(p2);
        }

        for(ServerPlayerEntity p : list) {
            p.connection.disconnect(new TranslationTextComponent("multiplayer.disconnect.duplicate_login"));
        }

        PlayerInteractionManager playerinteractionmanager;
        if(server.isDemo()) {
            playerinteractionmanager = new DemoPlayerInteractionManager(server.getWorld(DimensionType.OVERWORLD));
        } else {
            playerinteractionmanager = new PlayerInteractionManager(server.getWorld(DimensionType.OVERWORLD));
        }

        return new ModEntityPlayerMP(this.server, this.server.getWorld(DimensionType.OVERWORLD), profile, playerinteractionmanager);
    }

    @Override
    public ServerPlayerEntity recreatePlayerEntity(ServerPlayerEntity pIn, DimensionType dim, boolean conqueredEnd) {
        Server.scriptEvents.onPlayerPreRespawn(pIn);
        ServerWorld world = server.getWorld(dim);
        if(world == null) {
            dim = pIn.getSpawnDimension();
        } else if(!world.getDimension().canRespawnHere()) {
            dim = world.getDimension().getRespawnDimension(pIn);
        }
        if(server.getWorld(dim) == null) {
            dim = DimensionType.OVERWORLD;
        }

        this.removePlayer(pIn);
        pIn.getServerWorld().removePlayer(pIn, true); // Forge: keep data until copyFrom called
        BlockPos blockpos = pIn.getBedLocation(dim);
        boolean flag = pIn.isSpawnForced(dim);
        pIn.dimension = dim;
        PlayerInteractionManager playerinteractionmanager;
        if(this.server.isDemo()) {
            playerinteractionmanager = new DemoPlayerInteractionManager(this.server.getWorld(pIn.dimension));
        } else {
            playerinteractionmanager = new PlayerInteractionManager(this.server.getWorld(pIn.dimension));
        }

        ServerPlayerEntity p = new ModEntityPlayerMP(this.server, this.server.getWorld(pIn.dimension), pIn.getGameProfile(), playerinteractionmanager, (ModEntityPlayerMP) pIn);
        p.connection = pIn.connection;
        p.copyFrom(pIn, conqueredEnd);
        pIn.remove(false); // Forge: clone event had a chance to see old data, now discard it
        p.dimension = dim;
        p.setEntityId(pIn.getEntityId());
        p.setPrimaryHand(pIn.getPrimaryHand());

        for(String s : pIn.getTags()) {
            p.addTag(s);
        }

        ServerWorld sw = this.server.getWorld(pIn.dimension);
        setPlayerGameTypeBasedOnOther(p, pIn, sw);
        if(blockpos != null) {
            Optional<Vec3d> optional = PlayerEntity.func_213822_a(server.getWorld(pIn.dimension), blockpos, flag);
            if(optional.isPresent()) {
                Vec3d vec3d = optional.get();
                p.setLocationAndAngles(vec3d.x, vec3d.y, vec3d.z, 0.0f, 0.0f);
                p.setSpawnPoint(blockpos, flag, false, dim);
            } else {
                p.connection.sendPacket(new SChangeGameStatePacket(0, 0.0f));
            }
        }

        while(!sw.func_226669_j_(p) && p.getPosY() < 256.0D) {
            p.setPosition(p.getPosX(), p.getPosY() + 1.0, p.getPosZ());
        }

        WorldInfo wi = p.world.getWorldInfo();
        net.minecraftforge.fml.network.NetworkHooks.sendDimensionDataPacket(p.connection.netManager, p);
        p.connection.sendPacket(new SRespawnPacket(p.dimension, WorldInfo.byHashing(wi.getSeed()), wi.getGenerator(), p.interactionManager.getGameType()));
        BlockPos pos = sw.getSpawnPoint();
        p.connection.setPlayerLocation(p.getPosX(), p.getPosY(), p.getPosZ(), p.rotationYaw, p.rotationPitch);
        p.connection.sendPacket(new SSpawnPositionPacket(pos));
        p.connection.sendPacket(new SServerDifficultyPacket(wi.getDifficulty(), wi.isDifficultyLocked()));
        p.connection.sendPacket(new SSetExperiencePacket(p.experience, p.experienceTotal, p.experienceLevel));
        sendWorldInfo(p, sw);
        updatePermissionLevel(p);
        sw.addRespawnedPlayer(p);
        this.addPlayer(p);
        this.uuidToPlayerMap.put(p.getUniqueID(), p);
        p.addSelfToInternalCraftingInventory();
        p.setHealth(p.getHealth());
        net.minecraftforge.fml.hooks.BasicEventHooks.firePlayerRespawnEvent(p, conqueredEnd);
        return p;
    }

    private void setPlayerGameTypeBasedOnOther(ServerPlayerEntity target, ServerPlayerEntity source, IWorld w) {
        ReflectionUtils.setPlayerGameTypeBasedOnOther(this, target, source, w);
    }

    @Override
    public void sendMessage(ITextComponent component) {
        // filter multiplayer leave text
        if(component instanceof TranslationTextComponent && ((TranslationTextComponent) component).getKey().equals("multiplayer.player.left")) {
            return;
        }
        super.sendMessage(component);
    }
}
