
package studio.halbear.hem.gui.overlay;

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 studio.halbear.hem.HemModVariables;
import net.minecraft.tags.EntityTypeTags;


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

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

@Mod.EventBusSubscriber
public class OdometerOverlay {
	static ResourceLocation[] OdometerNumbers = new ResourceLocation[10];
	static ResourceLocation[] OdometerNumbersDecimal = new ResourceLocation[10];
	static{
		for(int i = 0; i < 10; i++){
			OdometerNumbers[i] = new ResourceLocation("hem:textures/screens/odometernumber"+i+ ".png");
			OdometerNumbersDecimal[i] = new ResourceLocation("hem:textures/screens/odometernumberdecimal"+i+ ".png");
		}
	}
	@OnlyIn(Dist.CLIENT)
	@SubscribeEvent(priority = EventPriority.NORMAL)
	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_odometer_vehicles"))
				.contains((entity.getRidingEntity()).getType())) {
				String OdometerValue = "";
				if((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new HemModVariables.PlayerVariables())).VehicleOdometer < 9999.9){
					OdometerValue = String.valueOf(Math.round((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new HemModVariables.PlayerVariables())).VehicleOdometer * 10.0)/10.0);
				} else{
					OdometerValue = "9999.9";
				}
				OdometerValue = OdometerValue.replace(".", "");
				char[] OdemeterValues = OdometerValue.toCharArray();
				int[] indexes = new int[5];
				for(int i = 5; i > 0; i--){
					if(i <= OdemeterValues.length){
						indexes[5 - i] = Integer.parseInt("" + OdemeterValues[OdemeterValues.length - i]);
					} else{
						indexes[5 - i] = 0;
					}
				}
			
				Minecraft.getInstance().getTextureManager().bindTexture(new ResourceLocation("hem:textures/screens/odometer.png"));
				Minecraft.getInstance().ingameGUI.blit(event.getMatrixStack(), w - (h/8)*3, h/12, 0, 0, (h/6)*2, h/6, (h/6)*2, h/6);

				Minecraft.getInstance().getTextureManager().bindTexture(OdometerNumbers[indexes[0]]);
				Minecraft.getInstance().ingameGUI.blit(event.getMatrixStack(), w - (h/8)*3 + (h/6)*2*8/128, h/12 + (h/6)*19/64, 0, 0, (h/6)* 20/64, (h/6) * 37/64, (h/6)* 20/64, (h/6) * 37/64);

				Minecraft.getInstance().getTextureManager().bindTexture(OdometerNumbers[indexes[1]]);
				Minecraft.getInstance().ingameGUI.blit(event.getMatrixStack(), w - (h/8)*3 + (h/6)*2*30/128, h/12 + (h/6)*19/64, 0, 0, (h/6)* 20/64, (h/6) * 37/64, (h/6)* 20/64, (h/6) * 37/64);

				Minecraft.getInstance().getTextureManager().bindTexture(OdometerNumbers[indexes[2]]);
				Minecraft.getInstance().ingameGUI.blit(event.getMatrixStack(),  w - (h/8)*3 + (h/6)*2*52/128, h/12 + (h/6)*19/64, 0, 0, (h/6)* 20/64, (h/6) * 37/64, (h/6)* 20/64, (h/6) * 37/64);

				Minecraft.getInstance().getTextureManager().bindTexture(OdometerNumbers[indexes[3]]);
				Minecraft.getInstance().ingameGUI.blit(event.getMatrixStack(),  w - (h/8)*3 + (h/6)*2*74/128, h/12 + (h/6)*19/64, 0, 0, (h/6)* 20/64, (h/6) * 37/64, (h/6)* 20/64, (h/6) * 37/64);

				Minecraft.getInstance().getTextureManager().bindTexture(OdometerNumbersDecimal[indexes[4]]);
				Minecraft.getInstance().ingameGUI.blit(event.getMatrixStack(), w - (h/8)*3 + (h/6)*2*101/128,h/12 + (h/6)*19/64, 0, 0, (h/6)* 20/64, (h/6) * 37/64, (h/6)* 20/64, (h/6) * 37/64);

			}
			RenderSystem.depthMask(true);
			RenderSystem.enableDepthTest();
			RenderSystem.enableAlphaTest();
			RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
		}
	}
}
