package net.halbear.supernova.vehicle;

import net.halbear.supernova.*;
import net.halbear.supernova.entity.Spaceship;
import net.halbear.supernova.registry.ModEntities;
import net.minecraft.client.Minecraft;
import net.minecraft.util.math.vector.Vector3f;
import net.minecraftforge.client.event.EntityViewRenderEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber
public class VehicleCameraSetup {
    private static Vector3f CamOffset = new Vector3f(0.0f, 0.0f, 0.0f);
    private static Vector3f CamTransformations = new Vector3f(0.0f, 0.0f, 0.0f);
    private static float CamFOV = 0.0f;
    private static float CamFOVExternal = 0.0f;
    private static String CurrentEntity = "";
    private static boolean SupernovaVehicle = false;
    public static Vector3f GetCameraOffsets(){
        return CamOffset;
    }

    @SubscribeEvent
    public static void PlayerTickEvent(TickEvent.PlayerTickEvent event) {

        Minecraft minecraftInstance = Minecraft.getInstance();
        if (minecraftInstance.player != null && minecraftInstance.player.isPassenger() && minecraftInstance.player.getRidingEntity() != null
                && CurrentEntity != minecraftInstance.player.getRidingEntity().getType().getRegistryName().toString()) {
            CurrentEntity = minecraftInstance.player.getRidingEntity().getType().getRegistryName().toString();
            SupernovaVehicle = minecraftInstance.player.getRidingEntity().getType().getRegistryName().getNamespace().equals(SuperNova.MOD_ID);
            if (CurrentEntity.equals(ModEntities.SPACESHIP.get().getRegistryName().toString())) {
                CamOffset = new Vector3f(0.0f, -0.75f, 6.5f);
                CamTransformations = new Vector3f(0.0f, 0.0f, 0.0f);
                CamFOV = 70.0f;
                CamFOVExternal = 60.0f;
            }
        }
        if (minecraftInstance.player != null && (!minecraftInstance.player.isPassenger() || !SupernovaVehicle)
                && (CamFOVExternal != (float) Minecraft.getInstance().gameSettings.fov
                || CamFOV != (float) Minecraft.getInstance().gameSettings.fov || CamOffset.getX() != 0 || CamOffset.getY() != 0
                || CamOffset.getZ() != 0 || CamTransformations.getX() != 0 || CamTransformations.getY() != 0
                || CamTransformations.getZ() != 0)) {
            CamOffset = new Vector3f(0.0f, 0.0f, 0.0f);// reset if the players not riding an entity and if any the values aren't 0
            CamTransformations = new Vector3f(0.0f, 0.0f, 0.0f);
            CamFOV = (float) Minecraft.getInstance().gameSettings.fov;
            CamFOVExternal = (float) Minecraft.getInstance().gameSettings.fov;
            if (!minecraftInstance.player.isPassenger())
                CurrentEntity = "";
        }
    }

    @SubscribeEvent
    public static void onCameraSetup(EntityViewRenderEvent.CameraSetup event) {
        if (SupernovaVehicle) {
            event.setRoll(event.getRoll() + CamTransformations.getX());
            event.setYaw(event.getYaw() + CamTransformations.getY());
            event.setPitch(event.getPitch() + CamTransformations.getZ());
        }
    }
}
