/*
    This file is a part of the SteamWar software.

    Copyright (C) 2021  SteamWar.de-Serverteam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.fightsystem.utils;

import de.steamwar.Reflection;
import de.steamwar.core.Core;
import de.steamwar.fightsystem.Config;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import java.util.Map;

public class BlockIdWrapper14 implements BlockIdWrapper {

    private static final Class<?> chunkProviderServer = Reflection.getClass("net.minecraft.server.level.ServerChunkCache");
    private static final Reflection.Method getChunkProvider = Reflection.getTypedMethod(worldServer, null, chunkProviderServer);
    private static final Class<?> playerChunkMap = Reflection.getClass("net.minecraft.server.level.ChunkMap");
    private static final Reflection.Field<?> getPlayerChunkMap = Reflection.getField(chunkProviderServer, playerChunkMap,  0);
    private static final Reflection.Field<? extends Map> entityTrackers = Core.getVersion() > 15 ? Reflection.getField(playerChunkMap, Int2ObjectMap.class,  0) : Reflection.getField(playerChunkMap, org.bukkit.craftbukkit.libs.it.unimi.dsi.fastutil.ints.Int2ObjectMap.class, 0);
    private static final Class<?> block = Reflection.getClass("net.minecraft.world.level.block.Block");
    private static final Class<?> iBlockData = Reflection.getClass("net.minecraft.world.level.block.state.BlockState");
    private static final Class<?> blockPosition = Reflection.getClass("net.minecraft.core.BlockPos");

    private final Map trackers;
    public BlockIdWrapper14() {
        trackers = entityTrackers.get(getPlayerChunkMap.get(getChunkProvider.invoke(getWorldHandle.invoke(Config.world))));
    }

    private static final Reflection.Method getCombinedId = Reflection.getTypedMethod(block, null, int.class, iBlockData);
    private static final Reflection.Method getNMS = Reflection.getTypedMethod(Reflection.getClass("org.bukkit.craftbukkit.block.CraftBlock"), "getNMS", iBlockData);
    @Override
    public int blockToId(Block block) {
        return (int) getCombinedId.invoke(null, getNMS.invoke(block));
    }

    private static final Reflection.Method getByCombinedId = Reflection.getTypedMethod(block, null, iBlockData, int.class);
    private static final Reflection.Constructor newBlockPosition = Reflection.getConstructor(blockPosition, int.class, int.class, int.class);
    private static final Reflection.Method getTypeAndData = Reflection.getMethod(worldServer, null, blockPosition, iBlockData, int.class);
    private static final Reflection.Method removeTileEntity = Reflection.getMethod(worldServer, Core.getVersion() > 15 ? "m" : "removeTileEntity", blockPosition);
    private static final Reflection.Method flagDirty = Reflection.getMethod(chunkProviderServer, null, blockPosition);
    @Override
    public void setBlock(World world, int x, int y, int z, int blockState) {
        Object blockData = getByCombinedId.invoke(null, blockState);
        Object nworld = getWorldHandle.invoke(world);
        Object pos = newBlockPosition.invoke(x, y, z);

        removeTileEntity.invoke(nworld, pos);
        getTypeAndData.invoke(nworld, pos, blockData, 1042);
        flagDirty.invoke(getChunkProvider.invoke(nworld), pos);
    }

    private static final Class<?> entityTracker = Reflection.getClass("net.minecraft.server.level.ChunkMap$TrackedEntity");
    private static final Reflection.Method updatePlayer = Reflection.getMethod(entityTracker, Core.getVersion() > 15 ? "b" : "updatePlayer", entityPlayer);
    @Override
    public void trackEntity(Player player, Entity entity) {
        Object tracker = trackers.get(entity.getEntityId());
        if(tracker != null)
            updatePlayer.invoke(tracker, getPlayer.invoke(player));
    }

    private static final Reflection.Method clearPlayer = Reflection.getMethod(entityTracker, Core.getVersion() > 15 ? "a" : "clear", entityPlayer);
    @Override
    public void untrackEntity(Player player, Entity entity) {
        Object tracker = trackers.get(entity.getEntityId());
        if(tracker != null)
            clearPlayer.invoke(tracker, getPlayer.invoke(player));
    }

    private static final Reflection.Method getMaterialByBlock = Reflection.getTypedMethod(Reflection.getClass("org.bukkit.craftbukkit.util.CraftMagicNumbers"), "getMaterial", Material.class, block);
    private static final Reflection.Method getBlockByBlockData = Reflection.getTypedMethod(iBlockData, null, block);
    @Override
    public Material idToMaterial(int blockState) {
        return (Material)getMaterialByBlock.invoke(null, getBlockByBlockData.invoke(getByCombinedId.invoke(null, blockState)));
    }
}
