
package studio.halbear.hem.gui.overlay;

import studio.halbear.hem.HemModVariables;

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.api.distmarker.Dist;

import net.minecraft.world.World;
import net.minecraft.util.ResourceLocation;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.client.Minecraft;

import java.util.stream.Stream;
import java.util.Map;
import java.util.HashMap;
import java.util.AbstractMap;
import net.minecraft.tags.EntityTypeTags;


import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.platform.GlStateManager;

@Mod.EventBusSubscriber
public class TemperatureGaugeOverlay {
	static ResourceLocation[] GaugeTextures = new ResourceLocation[32];
	static{
		for(int i = 0; i < 32; i++){
			GaugeTextures[i] = new ResourceLocation("hem:textures/screens/tempgauge"+i+ ".png");
		}
	}
	@OnlyIn(Dist.CLIENT)
	@SubscribeEvent(priority = EventPriority.HIGH)
	public static void eventHandler(RenderGameOverlayEvent.Post event) {
		if (event.getType() == RenderGameOverlayEvent.ElementType.HELMET) {
			int w = event.getWindow().getScaledWidth();
			int h = event.getWindow().getScaledHeight();
			int posX = w / 2;
			int posY = h / 2;
			World _world = null;
			double _x = 0;
			double _y = 0;
			double _z = 0;
			PlayerEntity entity = Minecraft.getInstance().player;
			if (entity != null) {
				_world = entity.world;
				_x = entity.getPosX();
				_y = entity.getPosY();
				_z = entity.getPosZ();
			}
			World world = _world;
			double x = _x;
			double y = _y;
			double z = _z;
			RenderSystem.disableDepthTest();
			RenderSystem.depthMask(false);
			RenderSystem.blendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA,
					GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
			RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
			RenderSystem.disableAlphaTest();
			if (entity.isPassenger() && EntityTypeTags.getCollection().getTagByID(new ResourceLocation("hem:hem_heat_vehicles"))
				.contains((entity.getRidingEntity()).getType()))
{
				Minecraft.getInstance().getTextureManager().bindTexture(new ResourceLocation("hem:textures/screens/pipe.png"));
				Minecraft.getInstance().ingameGUI.blit(event.getMatrixStack(), (int)(w/20) + (int)((h/6) *1.8),h- (int)(h/6.5), 0, 0, h/6, (h/6)*4, h/6, (h/6)*4);
				int Index = (int)Math.max(Math.floor((Math.floor(32 * ((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new HemModVariables.PlayerVariables())).Heat/160.0)) % 32.0)),0);
				Minecraft.getInstance().getTextureManager().bindTexture(GaugeTextures[Index]);
				Minecraft.getInstance().ingameGUI.blit(event.getMatrixStack(), (int)(w/20) + (int)((h/6) *1.8),h- (int)(h/3.5), 0, 0, h/6, h/6, h/6, h/6);
			}
			RenderSystem.depthMask(true);
			RenderSystem.enableDepthTest();
			RenderSystem.enableAlphaTest();
			RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
		}
	}
}
