/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 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.bausystem;

import de.steamwar.bausystem.tracer.AbstractTraceEntity;
import de.steamwar.bausystem.world.TPSUtils;
import net.minecraft.server.v1_12_R1.*;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.List;

public class CraftbukkitWrapper12 implements CraftbukkitWrapper.ICraftbukkitWrapper {

    private final List<Packet<?>> packets = new ArrayList<>();

    @Override
    public void initTPS() {
        TPSUtils.disableWarp();
    }

    @Override
    public void createTickCache(World world) {
        packets.clear();
        world.getEntities().stream().filter(entity -> !(entity instanceof Player)).forEach(entity -> {
            packets.add(new PacketPlayOutEntityVelocity(entity.getEntityId(), 0, 0, 0));
            packets.add(new PacketPlayOutEntityTeleport(((CraftEntity) entity).getHandle()));

            if (entity instanceof TNTPrimed) {
                net.minecraft.server.v1_12_R1.Entity serverEntity = ((CraftEntity) entity).getHandle();
                packets.add(new PacketPlayOutEntityMetadata(serverEntity.getId(), serverEntity.getDataWatcher(), true));
            }
        });
    }

    @Override
    public void sendTickPackets() {
        Bukkit.getOnlinePlayers().forEach(player -> {
            PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
            for (Packet<?> p : packets) {
                connection.sendPacket(p);
            }
        });
    }

    @Override
    public void openSignEditor(Player player, Location location) {
        PacketPlayOutOpenSignEditor packet = new PacketPlayOutOpenSignEditor(new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()));
        ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    }

    @Override
    public AbstractTraceEntity create(World world, Vector tntPosition, boolean tnt) {
        return new TraceEntity12(world, tntPosition, tnt);
    }
}
