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

import de.steamwar.Reflection;
import de.steamwar.core.BountifulWrapper;
import de.steamwar.core.ChatWrapper;
import de.steamwar.core.FlatteningWrapper;
import de.steamwar.core.ProtocolWrapper;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import lombok.Getter;
import net.minecraft.network.protocol.game.*;
import org.bukkit.Location;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;

public class REntity {

    private static final Object entityStatusWatcher = BountifulWrapper.impl.getDataWatcherObject(0, Byte.class);
    private static final Object sneakingDataWatcher = BountifulWrapper.impl.getDataWatcherObject(6, FlatteningWrapper.impl.getPose(FlatteningWrapper.EntityPose.NORMAL).getClass());
    private static final Object bowDrawnWatcher = BountifulWrapper.impl.getDataWatcherObject(6, FlatteningWrapper.impl.getPose(FlatteningWrapper.EntityPose.NORMAL).getClass());
    private static final Object nameWatcher = BountifulWrapper.impl.getDataWatcherObject(2, Optional.class); // Optional<IChatBaseComponent>
    private static final Object nameVisibleWatcher = BountifulWrapper.impl.getDataWatcherObject(3, Boolean.class);

    private static final Object noGravityDataWatcher = BountifulWrapper.impl.getDataWatcherObject(5,Boolean.class);

    private static int entityIdCounter = -1;
    private static final Random random = new Random();

    protected final REntityServer server;
    private final EntityType entityType;
    @Getter
    protected final int entityId;
    @Getter
    protected final UUID uuid;

    @Getter
    protected double x;
    @Getter
    protected double y;
    @Getter
    protected double z;
    protected byte yaw;
    protected byte pitch;
    protected byte headYaw;

    @Getter
    private boolean hidden;
    @Getter
    private boolean invisible;
    @Getter
    private FlatteningWrapper.EntityPose pose = FlatteningWrapper.EntityPose.NORMAL;
    @Getter
    private boolean bowDrawn;
    @Getter
    private boolean noGravity;
    private boolean isGlowing;
    private int fireTick;

    private final int objectData;
    @Getter
    private String displayName;
    protected final Map<Object, ItemStack> itemSlots;

    public REntity(REntityServer server, EntityType entityType, Location location) {
        this(server,entityType,location,0);
        server.addEntity(this);
    }

    protected REntity(REntityServer server, EntityType entityType, Location location,int objectData) {
        this(server, entityType, new UUID(random.nextLong() & -61441L | 16384L, random.nextLong() & 4611686018427387903L | -9223372036854775808L), location,objectData);
    }

    protected REntity(REntityServer server, EntityType entityType, UUID uuid, Location location,int objectData) {
        this.server = server;
        this.entityType = entityType;
        this.entityId = entityIdCounter--;
        this.uuid = uuid;

        this.x = location.getX();
        this.y = location.getY();
        this.z = location.getZ();
        this.headYaw = this.yaw = rotToByte(location.getYaw());
        this.pitch = rotToByte(location.getPitch());

        this.itemSlots = entityType == EntityType.PLAYER ? new HashMap<>() : null;

        this.noGravity = false;
        this.isGlowing = false;

        this.objectData = objectData;
    }

    public void hide(boolean hide) {
        if(hidden == hide)
            return;

        if(hide) {
            despawn(packet -> server.updateEntity(this, packet));
            hidden = true;
        } else {
            hidden = false;
            spawn(packet -> server.updateEntity(this, packet));
        }
    }

    public void move(Location location) {
        move(location.getX(), location.getY(), location.getZ(), location.getPitch(), location.getYaw(), rotToByte(location.getYaw()));
    }

    private static final double MAX_REL_MOVE = 8.0;
    public void move(double locX, double locY, double locZ, float pitch, float yaw, byte headYaw) {
        server.preEntityMove(this, locX, locZ);

        double fromX = this.x;
        double fromZ = this.z;
        double diffX = locX - x;
        double diffY = locY - y;
        double diffZ = locZ - z;
        boolean rotEq = this.yaw == yaw && this.pitch == pitch;

        this.x = locX;
        this.y = locY;
        this.z = locZ;
        this.yaw = rotToByte(yaw);
        this.pitch = rotToByte(pitch);

        if(Math.abs(diffX) < MAX_REL_MOVE && Math.abs(diffY) < MAX_REL_MOVE && Math.abs(diffZ) < MAX_REL_MOVE) {
            Object packet = getMoveLookPacket(diffX, diffY, diffZ,rotEq);

            if(packet != null)
                server.updateEntity(this, packet);
        } else {
            server.updateEntity(this, getTeleportPacket());
        }

        if(this.headYaw != headYaw) {
            this.headYaw = headYaw;
            server.updateEntity(this, getHeadRotationPacket());
        }

        server.postEntityMove(this, fromX, fromZ);
    }

    private static final Class<?> animationPacket = ClientboundAnimatePacket.class;
    private static final Reflection.Field<Integer> animationEntity = Reflection.getField(animationPacket, int.class, 5);
    private static final Reflection.Field<Integer> animationAnimation = Reflection.getField(animationPacket, int.class, 6);
    public void showAnimation(byte animation) {
        Object packet = Reflection.newInstance(animationPacket);
        animationEntity.set(packet, entityId);
        animationAnimation.set(packet, (int) animation);
        server.updateEntity(this, packet);
    }

    private static final Class<?> velocityPacket = ClientboundSetEntityMotionPacket.class;
    private static final Reflection.Field<Integer> velocityEntity = Reflection.getField(velocityPacket, int.class, 0);
    private static final Reflection.Field<Integer> velocityX = Reflection.getField(velocityPacket, int.class, 1);
    private static final Reflection.Field<Integer> velocityY = Reflection.getField(velocityPacket, int.class, 2);
    private static final Reflection.Field<Integer> velocityZ = Reflection.getField(velocityPacket, int.class, 3);
    public void setVelocity(double dX, double dY, double dZ) {
        Object packet = Reflection.newInstance(velocityPacket);
        velocityEntity.set(packet, entityId);
        velocityX.set(packet, calcVelocity(dX));
        velocityY.set(packet, calcVelocity(dY));
        velocityZ.set(packet, calcVelocity(dZ));
        server.updateEntity(this, packet);
    }

    private static final Class<?> statusPacket = ClientboundEntityEventPacket.class;
    private static final Reflection.Field<Integer> statusEntity = Reflection.getField(statusPacket, int.class, 0);
    private static final Reflection.Field<Byte> statusStatus = Reflection.getField(statusPacket, byte.class, 0);
    public void showDamage() {
        Object packet = Reflection.newInstance(statusPacket);
        statusEntity.set(packet, entityId);
        statusStatus.set(packet, (byte) 2);
        server.updateEntity(this, packet);
    }

    public void setPose(FlatteningWrapper.EntityPose pose) {
        this.pose = pose;
        server.updateEntity(this, getDataWatcherPacket(sneakingDataWatcher, FlatteningWrapper.impl.getPose(pose)));
    }

    public void setOnFire(boolean perma) {
        fireTick = perma ? -1 : 21;
        server.updateEntity(this, getDataWatcherPacket(entityStatusWatcher, getEntityStatus()));
    }

    public boolean isOnFire() {
        return fireTick == -1 || fireTick > 0;
    }

    public void setInvisible(boolean invisible) {
        this.invisible = invisible;
        server.updateEntity(this, getDataWatcherPacket(entityStatusWatcher, getEntityStatus()));
    }

    public void setBowDrawn(boolean drawn, boolean offHand) {
        bowDrawn = drawn;
        server.updateEntity(this, getDataWatcherPacket(bowDrawnWatcher, FlatteningWrapper.impl.getPose(FlatteningWrapper.EntityPose.SHOOTING)));
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
        server.updateEntity(this, getDataWatcherPacket(
                nameWatcher, FlatteningWrapper.impl.formatDisplayName(displayName),
                nameVisibleWatcher, displayName != null
        ));
    }

    public void setItem(Object slot, ItemStack stack) {
        itemSlots.put(slot, stack);
        server.updateEntity(this, getEquipmentPacket(slot, stack));
    }

    public void die() {
        server.removeEntity(this);
    }

    public void setNoGravity(boolean noGravity) {
        this.noGravity = noGravity;
        server.updateEntity(this,getDataWatcherPacket(noGravityDataWatcher,noGravity));
    }

    public void setGlowing(boolean glowing) {
        this.isGlowing = glowing;
        server.updateEntity(this,getDataWatcherPacket(entityStatusWatcher,getEntityStatus()));
    }

    public boolean isGlowing() {
        return isGlowing;
    }

    private static final Function<REntity, Object> spawnPacketGenerator = entitySpawnPacketGenerator(ProtocolWrapper.spawnPacket, 2);

    private static final Reflection.Field<Integer> additionalData = Reflection.getField(ProtocolWrapper.spawnPacket, int.class, 4);

    private Object spawnPacketGenerator() {
        Object packet = spawnPacketGenerator.apply(this);
        additionalData.set(packet, objectData);
        return packet;
    }

    void list(Consumer<Object> packetSink) {
        // empty for regular entity
    }

    private static final Function<REntity, Object> livingSpawnPacketGenerator = REntity::spawnPacketGenerator;
    void spawn(Consumer<Object> packetSink) {
        if(entityType.isAlive()) {
            packetSink.accept(livingSpawnPacketGenerator.apply(this));
        } else {
            packetSink.accept(spawnPacketGenerator());
        }

        postSpawn(packetSink);
    }

    protected void postSpawn(Consumer<Object> packetSink) {
        if(headYaw != 0) {
            packetSink.accept(getHeadRotationPacket());
        }

        if(pose != FlatteningWrapper.EntityPose.NORMAL) {
            packetSink.accept(getDataWatcherPacket(sneakingDataWatcher, FlatteningWrapper.impl.getPose(pose)));
        }

        byte status = getEntityStatus();
        if(status != 0) {
            packetSink.accept(getDataWatcherPacket(entityStatusWatcher, getEntityStatus()));
        }

        if(displayName != null) {
            packetSink.accept(getDataWatcherPacket(nameWatcher, FlatteningWrapper.impl.formatDisplayName(displayName), nameVisibleWatcher, true));
        }

        if(noGravity) {
            packetSink.accept(getDataWatcherPacket(noGravityDataWatcher, true));
        }
    }

    void tick() {
        if(fireTick > 0) {
            fireTick--;
            if(fireTick == 0) {
                server.updateEntity(this, getDataWatcherPacket(entityStatusWatcher, getEntityStatus()));
            }
        }
    }

    private static final Class<?> destroyPacket = ClientboundRemoveEntitiesPacket.class;
    private static final Reflection.Field<?> destroyEntities = Reflection.getField(destroyPacket, IntList.class, 0);
    void despawn(Consumer<Object> packetSink){
        Object packet = Reflection.newInstance(destroyPacket);
        destroyEntities.set(packet, new IntArrayList(new int[]{entityId}));
        packetSink.accept(packet);
    }

    void delist(Consumer<Object> packetSink) {
        // empty for regular entity
    }

    double x() {
        return x;
    }

    double z() {
        return z;
    }

    private byte getEntityStatus() {
        byte status = 0;

        if(fireTick != 0)
            status |= 1;
        if(pose == FlatteningWrapper.EntityPose.SNEAKING)
            status |= 2;
        if(invisible)
            status |= 0x20;
        if(isGlowing)
            status |= 0x40;

        return status;
    }

    protected Object getDataWatcherPacket(Object... dataWatcherKeyValues) {
        return ChatWrapper.impl.getDataWatcherPacket(entityId, dataWatcherKeyValues);
    }

    public static final Class<?> teleportPacket = ClientboundTeleportEntityPacket.class;
    public static final Reflection.Field<Integer> teleportEntity = Reflection.getField(teleportPacket, int.class, 0);
    public static final BountifulWrapper.PositionSetter teleportPosition = BountifulWrapper.impl.getPositionSetter(teleportPacket, 0);
    private Object getTeleportPacket(){
        return PacketConstructor.impl.teleportPacket(entityId, x, y, z, pitch, yaw);
    }

    private static final Class<?> entityPacket = ClientboundMoveEntityPacket.class;
    private static final Reflection.Field<Integer> moveEntityId = Reflection.getField(entityPacket, int.class, 0);
    private static final BountifulWrapper.PositionSetter movePosition = BountifulWrapper.impl.getRelMoveSetter(entityPacket);
    private static final Class<?> lookPacket = ClientboundMoveEntityPacket.Rot.class;
    private static final Class<?> movePacket = ClientboundMoveEntityPacket.Pos.class;
    private static final Class<?> moveLookPacket = ClientboundMoveEntityPacket.PosRot.class;
    private Object getMoveLookPacket(double diffX, double diffY, double diffZ, boolean rotEq) {
        Class<?> clazz;
        if(diffX == 0 && diffY == 0 && diffZ == 0) {
            if(rotEq)
                return null;

            clazz = lookPacket;
        } else if (rotEq) {
            clazz = movePacket;
        } else {
            clazz = moveLookPacket;
        }

        Object packet = Reflection.newInstance(clazz);
        moveEntityId.set(packet, entityId);
        movePosition.set(packet, diffX, diffY, diffZ, pitch, yaw);
        return packet;
    }

    private static final Class<?> headRotationPacket = ClientboundRotateHeadPacket.class;
    private static final Reflection.Field<Integer> headRotationEntity = Reflection.getField(headRotationPacket, int.class, 0);
    private static final Reflection.Field<Byte> headRotationYaw = Reflection.getField(headRotationPacket, byte.class, 0);
    private Object getHeadRotationPacket(){
        Object packet = Reflection.newInstance(headRotationPacket);
        headRotationEntity.set(packet, entityId);
        headRotationYaw.set(packet, headYaw);
        return packet;
    }

    private static final Reflection.Field<Integer> equipmentEntity = Reflection.getField(ProtocolWrapper.equipmentPacket, int.class, 0);
    private static final Reflection.Field<List> equipmentSlots = Reflection.getField(ProtocolWrapper.equipmentPacket, List.class, 0);

    protected Object getEquipmentPacket(Object slot, ItemStack stack){
        Object packet = Reflection.newInstance(ProtocolWrapper.equipmentPacket);
        equipmentEntity.set(packet, entityId);
        equipmentSlots.set(packet, new ArrayList<>());
        ProtocolWrapper.impl.setEquipmentPacketStack(packet, slot, CraftItemStack.asNMSCopy(stack));
        return packet;
    }

    private static Function<REntity, Object> entitySpawnPacketGenerator(Class<?> spawnPacket, int posOffset) {
        BountifulWrapper.UUIDSetter uuid = BountifulWrapper.impl.getUUIDSetter(spawnPacket);
        Function<REntity, Object> packetGenerator = spawnPacketGenerator(spawnPacket, posOffset);

        return entity -> {
            Object packet = packetGenerator.apply(entity);
            uuid.set(packet, entity.uuid);
            FlatteningWrapper.impl.setSpawnPacketType(packet, entity.entityType);
            return packet;
        };
    }

    protected static Function<REntity, Object> spawnPacketGenerator(Class<?> spawnPacket, int posOffset) {
        Reflection.Field<Integer> entityId = Reflection.getField(spawnPacket, int.class, 0);
        BountifulWrapper.PositionSetter position = BountifulWrapper.impl.getPositionSetter(spawnPacket, posOffset);

        return entity -> {
            Object packet = Reflection.newInstance(spawnPacket);
            entityId.set(packet, entity.entityId);
            position.set(packet, entity.x, entity.y, entity.z, entity.pitch, entity.yaw);
            return packet;
        };
    }

    private byte rotToByte(float rot) {
        return (byte)((int)(rot * 256.0F / 360.0F));
    }

    private int calcVelocity(double value) {
        return (int)(Math.max(-3.9, Math.min(value, 3.9)) * 8000);
    }
}
