package gvclib.client.event;

import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import gvclib.item.ItemAttachment;
import gvclib.item.ItemGunBase;
import net.minecraft.client.AttackIndicatorStatus;
import net.minecraft.client.Options;
import net.minecraft.client.gui.Font;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.*;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.client.event.ComputeFovModifierEvent;
//import net.minecraftforge.client.event.FOVModifierEvent;
import net.minecraftforge.client.event.RenderGuiEvent;
import net.minecraftforge.client.event.RenderGuiOverlayEvent;
import net.minecraftforge.client.gui.overlay.NamedGuiOverlay;
import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.eventbus.api.EventListenerHelper;
import net.minecraftforge.eventbus.api.EventPriority;
import org.lwjgl.opengl.GL11;

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

import net.minecraft.client.Minecraft;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
//import net.minecraftforge.client.event.RenderGameOverlayEvent;
//import net.minecraftforge.client.gui.ForgeIngameGui;
//import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;

public class RenderGunZoom {

    public String ads;


    @OnlyIn(Dist.CLIENT)
    @SubscribeEvent
    public void renderfov(ComputeFovModifierEvent event)
    {
        Minecraft minecraft = Minecraft.getInstance();
        Player entityplayer = minecraft.player;
        ItemStack itemstack = ((Player) (entityplayer)).getMainHandItem();
        if(!itemstack.isEmpty() && itemstack.getItem() instanceof ItemGunBase){
            ItemGunBase gun = (ItemGunBase) itemstack.getItem();
            float zoom = 1.0F;
            if(gun.getAttachment(gun, itemstack, 101) != null){
                ItemAttachment sight = (ItemAttachment) gun.getAttachment(gun, itemstack, 101);
                zoom = sight.zoom;
            }else{
                zoom = gun.scopezoom;
            }
        //    if(gun.scopezoom > 2.0F)
            {
                if (entityplayer.isCrouching()) {
                    {
                        event.setNewFovModifier(event.getFovModifier() / zoom);
                    }
                }
            }
        }//item
    }

    @SubscribeEvent
    @OnlyIn(Dist.CLIENT)
    public static void itemToolTip(ItemTooltipEvent event) {
        if(!event.getItemStack().isEmpty() && event.getItemStack().getItem() instanceof ItemGunBase){
            ItemGunBase gun = (ItemGunBase) event.getItemStack().getItem();
            //    TranslatableComponent te = new TranslatableComponent("aaa", new Object[0]);
            //    event.getToolTip().add(te);
        }
    }


    @OnlyIn(Dist.CLIENT)
    @SubscribeEvent
    public void onRenderCrosshair(RenderGuiOverlayEvent.Pre event) {
        NamedGuiOverlay over = event.getOverlay();
        Minecraft minecraft = Minecraft.getInstance();
        Player entityplayer = minecraft.player;
        ItemStack itemstack = ((Player)(entityplayer)).getMainHandItem();
        if (!itemstack.isEmpty() && itemstack.getItem() instanceof ItemGunBase) {
            if(over.id() == VanillaGuiOverlay.CROSSHAIR.id()){
                event.setCanceled(true);
            }
        }
    }


    public boolean zoomtype;

    @OnlyIn(Dist.CLIENT)
    @SubscribeEvent
    public void onEvent(RenderGuiOverlayEvent.Pre event)
    {
        Minecraft minecraft = Minecraft.getInstance();
        Level world = minecraft.level;
        Window window = minecraft.getWindow();
        int i = window.getGuiScaledWidth();
        int j = window.getGuiScaledHeight();
        Player entityplayer = minecraft.player;
        ItemStack itemstack = ((Player)(entityplayer)).getMainHandItem();
        Font fontrenderer = minecraft.font;
        Options options = minecraft.options;

        GL11.glEnable(GL11.GL_BLEND);
        //      if(FMLCommonHandler.instance().getSide() == Side.CLIENT)
        {
            if (!itemstack.isEmpty() && itemstack.getItem() instanceof ItemGunBase) {
                if (entityplayer.isCrouching()) {
                    ItemGunBase gun = (ItemGunBase) itemstack.getItem();
                    boolean render;
                    String scope;
                    if(gun.getAttachment(gun, itemstack, 101) != null){
                        ItemAttachment sight = (ItemAttachment) gun.getAttachment(gun, itemstack, 101);
                        render = sight.zoomrender;
                        scope = sight.ads_tex;
                    }else{
                        render = gun.zoomrender;
                        scope = gun.adstexture;
                    }
                    if(!render) {
                        ads = "classicguns:textures/misc/scope.png";
                        this.renderPumpkinBlur(minecraft, window, scope);
                    }
                }
            }
        }
    }

    @OnlyIn(Dist.CLIENT)
    protected void renderPumpkinBlur(Minecraft minecraft, Window window, String adss)
    {
        RenderSystem.disableDepthTest();
        RenderSystem.depthMask(false);
        RenderSystem.defaultBlendFunc();
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
        RenderSystem.setShaderTexture(0, new ResourceLocation(adss));
        Tesselator tesselator = Tesselator.getInstance();
        BufferBuilder bufferbuilder = tesselator.getBuilder();
        bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX);
        bufferbuilder.vertex(0.0D, (double)window.getGuiScaledHeight(), -90.0D).uv(0.0F, 1.0F).endVertex();
        bufferbuilder.vertex((double)window.getGuiScaledWidth(), (double)window.getGuiScaledHeight(), -90.0D).uv(1.0F, 1.0F).endVertex();
        bufferbuilder.vertex((double)window.getGuiScaledWidth(), 0.0D, -90.0D).uv(1.0F, 0.0F).endVertex();
        bufferbuilder.vertex(0.0D, 0.0D, -90.0D).uv(0.0F, 0.0F).endVertex();
        tesselator.end();
        RenderSystem.depthMask(true);
        RenderSystem.enableDepthTest();
        RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
        /*
        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();
        minecraft.getTextureManager().bind(new ResourceLocation(adss));
        Tessellator tessellator = Tessellator.getInstance();
        BufferBuilder bufferbuilder = tessellator.getBuilder();
        bufferbuilder.begin(7, DefaultVertexFormats.POSITION_TEX);
        bufferbuilder.vertex(0.0D, (double)window.getGuiScaledHeight(), -90.0D).uv(0.0F, 1.0F).endVertex();
        bufferbuilder.vertex((double)window.getGuiScaledWidth(), (double)window.getGuiScaledHeight(), -90.0D).uv(1.0F, 1.0F).endVertex();
        bufferbuilder.vertex((double)window.getGuiScaledWidth(), 0.0D, -90.0D).uv(1.0F, 0.0F).endVertex();
        bufferbuilder.vertex(0.0D, 0.0D, -90.0D).uv(0.0F, 0.0F).endVertex();
        tessellator.end();
        RenderSystem.depthMask(true);
        RenderSystem.enableDepthTest();
        RenderSystem.enableAlphaTest();
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
		/*
		  GlStateManager.disableDepth();
	        GlStateManager.depthMask(false);
	        GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
	        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
	        GlStateManager.disableAlpha();
	        minecraft.getTextureManager().bindTexture(new ResourceLocation(adss));
	        Tessellator tessellator = Tessellator.getInstance();
	        BufferBuilder bufferbuilder = tessellator.getBuffer();
	        bufferbuilder.begin(7, DefaultVertexFormats.POSITION_TEX);
	        bufferbuilder.pos(0.0D, (double)scaledRes.getScaledHeight(), -90.0D).tex(0.0D, 1.0D).endVertex();
	        bufferbuilder.pos((double)scaledRes.getScaledWidth(), (double)scaledRes.getScaledHeight(), -90.0D).tex(1.0D, 1.0D).endVertex();
	        bufferbuilder.pos((double)scaledRes.getScaledWidth(), 0.0D, -90.0D).tex(1.0D, 0.0D).endVertex();
	        bufferbuilder.pos(0.0D, 0.0D, -90.0D).tex(0.0D, 0.0D).endVertex();
	        tessellator.draw();
	        GlStateManager.depthMask(true);
	        GlStateManager.enableDepth();
	        GlStateManager.enableAlpha();
	        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);*/
    }

}

