package studio.halbear.hem_custom.vehicles;

import net.minecraft.client.Minecraft;
import net.minecraft.client.settings.PointOfView;
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 VehicleCameraSettings {
    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 HemVehicle = 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().getClass().getName()) {

            CurrentEntity = minecraftInstance.player.getRidingEntity().getClass().getName();
            HemVehicle = CurrentEntity.contains("studio.halbear.hem.entity");
            switch (CurrentEntity) {
                case "studio.halbear.hem.entity.HotAirBalloonEntity$CustomEntity" : //yeah i hardcoded ts
                    CamOffset = new Vector3f(0.0f, 1.0f, 4.0f);
                    CamTransformations = new Vector3f(0.0f, 0.0f, 0.0f);
                    CamFOV = 70.0f;
                    CamFOVExternal = 80.0f;
                    break;
                case "studio.halbear.hem.entity.TestPlaneEntity$CustomEntity" :
                    CamOffset = new Vector3f(0.0f, 0.0f, 4.5f);
                    CamTransformations = new Vector3f(0.0f, 0.0f, 0.0f);
                    CamFOV = 80.0f;
                    CamFOVExternal = 80.0f;
                    break;
                case "studio.halbear.hem.entity.AirboatEntity$CustomEntity" :
                    CamOffset = new Vector3f(0.0f, 4.0f, 16.0f);
                    CamTransformations = new Vector3f(0.0f, 0.0f, 0.0f);
                    CamFOV = 70.0f;
                    CamFOVExternal = 65.0f;
                    break;
            }
        }
        if (minecraftInstance.player != null && (!minecraftInstance.player.isPassenger() || !HemVehicle)
                && (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) { // sets the Yaw, Roll, and Pitch for the camera, ONLY if its a HEM vehcile as to avoid unnecessary ticks
        if (HemVehicle) {
            event.setRoll(event.getRoll() + CamTransformations.getX());
            event.setYaw(event.getYaw() + CamTransformations.getY());
            event.setPitch(event.getPitch() + CamTransformations.getZ());
        }
    }

    @SubscribeEvent
    public static void onCameraSetup(EntityViewRenderEvent.FOVModifier event) { // sets the FOV
        if (HemVehicle) {
            Minecraft minecraftInstance = Minecraft.getInstance();
            if (minecraftInstance.player != null && minecraftInstance.gameRenderer != null) {
                PointOfView Cam = minecraftInstance.gameSettings.getPointOfView();
                if (Cam == PointOfView.FIRST_PERSON) {
                    event.setFOV(CamFOV);
                } else {
                    event.setFOV(CamFOVExternal);
                }
            }
        } else if (event.getFOV() != Minecraft.getInstance().gameSettings.fov) {
            event.setFOV(Minecraft.getInstance().gameSettings.fov);
        }
    }
}
