/*
    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.techhider;

import de.steamwar.Reflection;
import com.google.common.collect.ImmutableList;
import org.bukkit.Material;

import java.util.HashSet;
import java.util.Set;

public class BlockIds14 implements BlockIds {

    private static final Class<?> blockStateList = Reflection.getClass("net.minecraft.world.level.block.state.StateDefinition");
    private static final Class<?> fluidTypeFlowing = Reflection.getClass("net.minecraft.world.level.material.FlowingFluid");
    private static final Class<?> fluid = Reflection.getClass("net.minecraft.world.level.material.FluidState");

    private static final Reflection.Method getBlockData = Reflection.getTypedMethod(TechHider.block, null, TechHider.iBlockData);
    @Override
    public int materialToId(Material material) {
        return getCombinedId(getBlockData.invoke(getBlock(material)));
    }

    private static final Reflection.Method getStates = Reflection.getTypedMethod(TechHider.block, null, blockStateList);
    private static final Reflection.Method getStateList = Reflection.getTypedMethod(blockStateList, null, ImmutableList.class);
    private static final Object water = Reflection.getTypedMethod(fluidTypeFlowing, null, fluid, boolean.class).invoke(Reflection.getField(Reflection.getClass("net.minecraft.world.level.material.Fluids"), fluidTypeFlowing, 1).get(null), false);
    private static final Iterable<?> registryBlockId = (Iterable<?>) Reflection.getField(TechHider.block, Reflection.getClass("net.minecraft.core.IdMapper"), 0).get(null);
    private static final Reflection.Method getFluid = Reflection.getTypedMethod(TechHider.iBlockData, null, fluid);
    @Override
    public Set<Integer> materialToAllIds(Material material) {
        Set<Integer> ids = new HashSet<>();
        for(Object data : (ImmutableList<?>) getStateList.invoke(getStates.invoke(getBlock(material)))) {
            ids.add(getCombinedId(data));
        }

        if(material == Material.WATER) {
            for(Object data : registryBlockId) {
                if(getFluid.invoke(data) == water) {
                    ids.add(getCombinedId(data));
                }
            }
        }

        return ids;
    }

    private static final Reflection.Method getBlock = Reflection.getTypedMethod(TechHider.craftMagicNumbers, "getBlock", TechHider.block, Material.class);
    private Object getBlock(Material material) {
        return getBlock.invoke(null, material);
    }

    private static final Reflection.Method getCombinedId = Reflection.getTypedMethod(TechHider.block, null, int.class, TechHider.iBlockData);
    private int getCombinedId(Object blockData) {
        return (int) getCombinedId.invoke(null, blockData);
    }
}
