/*
    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.fightsystem.Config;
import net.minecraft.server.level.WorldServer;
import net.minecraft.world.level.chunk.Chunk;
import net.minecraft.world.level.chunk.ChunkSection;
import net.minecraft.world.level.entity.LevelEntityGetter;
import org.bukkit.World;
import org.bukkit.entity.Entity;

import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class CraftbukkitWrapper18 implements CraftbukkitWrapper {

    private static final Reflection.Method getWorld = Reflection.getMethod(Reflection.getClass("org.bukkit.craftbukkit.CraftWorld"), "getHandle");
    private static final Reflection.Method getChunk = Reflection.getTypedMethod(net.minecraft.world.level.World.class, null, Chunk.class, int.class, int.class);
    private static final Reflection.Method getChunkSections = Reflection.getTypedMethod(Chunk.class, null, ChunkSection[].class);
    private ChunkSection[] getChunkSections(World world, int x, int z) {
        return (ChunkSection[]) getChunkSections.invoke(getChunk.invoke(getWorld.invoke(world), x, z));
    }

    @Override
    public void resetChunk(World world, World backup, int x, int z) {
        ChunkSection[] sections = getChunkSections(world, x, z);
        System.arraycopy(getChunkSections(backup, x, z), 0, sections, 0, sections.length);
    }

    private static final Reflection.Method getEntity = Reflection.getTypedMethod(Reflection.getClass("org.bukkit.craftbukkit.entity.CraftEntity"), "getHandle", net.minecraft.world.entity.Entity.class);
    protected net.minecraft.world.entity.Entity getEntity(Entity e) {
        return (net.minecraft.world.entity.Entity) getEntity.invoke(e);
    }

    @Override
    public float headRotation(Entity e) {
        return getEntity(e).ce();
    }

    private static final Reflection.Method getWorldEntities = Reflection.getTypedMethod(WorldServer.class, null, LevelEntityGetter.class);
    private static final Reflection.Method getIterable = Reflection.getTypedMethod(LevelEntityGetter.class, null, Iterable.class);
    @Override
    public Stream<?> entityIterator() {
        return StreamSupport.stream(((Iterable<?>) getIterable.invoke(getWorldEntities.invoke(getWorld.invoke(Config.world)))).spliterator(), false);
    }
}
