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

    Copyright (C) 2022  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.core.Core;
import de.steamwar.core.VersionDependent;
import io.netty.buffer.ByteBuf;
import org.bukkit.entity.Player;

import java.util.Set;
import java.util.function.BiFunction;

public interface ChunkHider {
    ChunkHider impl = VersionDependent.getVersionImpl(Core.getInstance());

    Class<?> mapChunkPacket();
    BiFunction<Player, Object, Object> chunkHiderGenerator(TechHider.LocationEvaluator locationEvaluator, int obfuscationTarget, Set<Integer> obfuscate, Set<String> hiddenBlockEntities);

    static int processPalette(int obfuscationTarget, Set<Integer> obfuscate, ByteBuf in, ByteBuf out) {
        int paletteLength = ProtocolUtils.readVarInt(in);
        ProtocolUtils.writeVarInt(out, paletteLength);

        int paletteTarget = 0;

        for(int i = 0; i < paletteLength; i++) {
            int entry = ProtocolUtils.readVarInt(in);
            if(obfuscate.contains(entry))
                entry = obfuscationTarget;

            if(entry == obfuscationTarget)
                paletteTarget = i;

            ProtocolUtils.writeVarInt(out, entry);
        }

        return paletteTarget;
    }

    interface PosEvaluator {
        TechHider.State test(int x, int y, int z);
    }
}
