package studio.halbear.hem_custom.vehicles.Entity;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.IPacket;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkHooks;

public class Aeroplane extends Entity {
    private static final DataParameter<Float> ORIENTATION_ROLL = EntityDataManager.createKey(Aeroplane.class, DataSerializers.FLOAT);
    private static final DataParameter<Float> ORIENTATION_YAW = EntityDataManager.createKey(Aeroplane.class, DataSerializers.FLOAT);
    private static final DataParameter<Float> ORIENTATION_PITCH = EntityDataManager.createKey(Aeroplane.class, DataSerializers.FLOAT);

    private static final Vector3d[] VehicleSeatOffsets = new Vector3d[]{
            new Vector3d(0.0D, -1D, 0.0D),
    };


    public Aeroplane(EntityType<?> Type, World WorldIn) {
        super(Type, WorldIn);
    }

    @Override
    protected void registerData() {
        this.dataManager.register(ORIENTATION_ROLL, 0F);
        this.dataManager.register(ORIENTATION_PITCH, 0F);
        this.dataManager.register(ORIENTATION_YAW, 0F);
    }

    @Override
    protected void readAdditional(CompoundNBT compoundNBT) {

    }

    @Override
    protected void writeAdditional(CompoundNBT compoundNBT) {

    }

    @Override
    public IPacket<?> createSpawnPacket() {
        return NetworkHooks.getEntitySpawningPacket(this);
    }

    public void SetVehicleRoll(float value){
        this.dataManager.set(ORIENTATION_ROLL, value);
    }
    public void SetVehicleYaw(float value){
        this.dataManager.set(ORIENTATION_YAW, value);
    }
    public void SetVehiclePitch(float value){
        this.dataManager.set(ORIENTATION_PITCH, value);
    }
    public float GetVehicleRoll(){
        return this.dataManager.get(ORIENTATION_ROLL);
    }
    public float GetVehicleYaw(){
        return this.dataManager.get(ORIENTATION_YAW);
    }
    public float GetVehiclePitch(){
        return this.dataManager.get(ORIENTATION_PITCH);
    }
    public float[] GetVehicleRotations(){
        return new float[]{GetVehicleYaw(),GetVehiclePitch(),GetVehicleRoll()};
    }
    public void SetVehicleRotations(float Yaw, float Pitch, float Roll){
        SetVehicleRoll(Roll);
        SetVehicleYaw(Yaw);
        SetVehiclePitch(Pitch);
    }

    @Override
    public boolean canBeCollidedWith(){return true;}

    @Override
    protected boolean canFitPassenger(Entity passenger) {
        return this.getPassengers().size() < VehicleSeatOffsets.length;
    }
    @Override
    public ActionResultType processInitialInteract(PlayerEntity player, Hand hand) {
        if (this.getPassengers().size() < VehicleSeatOffsets.length && !player.isPassenger()) {
            if (!this.world.isRemote()) {
                return player.startRiding(this) ? ActionResultType.CONSUME : ActionResultType.PASS;
            }
            return ActionResultType.func_233537_a_(this.world.isRemote());
        }
        return ActionResultType.SUCCESS;
    }

    @Override
    public Entity getControllingPassenger() {
        return this.getPassengers().isEmpty() ? null : this.getPassengers().get(0);
    }

    @Override
    public void updatePassenger(Entity passenger) {
        if (this.isPassenger(passenger)) {
            int seatIndex = this.getPassengers().indexOf(passenger);

            if (seatIndex >= 0 && seatIndex < VehicleSeatOffsets.length) {
                Vector3d localOffset = VehicleSeatOffsets[seatIndex];
                Vector3d rotatedOffset = localOffset.rotateYaw((float) Math.toRadians(-this.rotationYaw));
                passenger.setPosition(
                        this.getPosX() + rotatedOffset.x,
                        this.getPosY() + rotatedOffset.y + passenger.getMountedYOffset(),
                        this.getPosZ() + rotatedOffset.z
                );
            } else {
                super.updatePassenger(passenger);
            }
        }
    }
}
