package com.seibel.distanthorizons.common.render.blaze;

#if MC_VER <= MC_1_21_10
public class BlazeDhMetaRenderer {}

#else

import com.mojang.blaze3d.textures.GpuTexture;
import com.seibel.distanthorizons.api.methods.events.abstractEvents.DhApiAfterColorDepthTextureCreatedEvent;
import com.seibel.distanthorizons.api.methods.events.sharedParameterObjects.DhApiTextureCreatedParam;
import com.seibel.distanthorizons.common.render.blaze.apply.BlazeDhApplyRenderer;
import com.seibel.distanthorizons.common.render.blaze.wrappers.texture.BlazeTextureWrapper;
import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector;
import com.seibel.distanthorizons.core.render.RenderParams;
import com.seibel.distanthorizons.coreapi.util.ColorUtil;
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftRenderWrapper;
import com.seibel.distanthorizons.core.wrapperInterfaces.render.renderPass.IDhMetaRenderer;
import com.seibel.distanthorizons.coreapi.DependencyInjection.ApiEventInjector;
import net.minecraft.client.Minecraft;

import java.awt.*;

public class BlazeDhMetaRenderer implements IDhMetaRenderer
{
	public static final BlazeDhMetaRenderer INSTANCE = new BlazeDhMetaRenderer();
	
	private static final IMinecraftRenderWrapper MC_RENDER = SingletonInjector.INSTANCE.get(IMinecraftRenderWrapper.class);
	
	
	private BlazeDhApplyRenderer applyRenderer;
	
	public final BlazeTextureWrapper dhDepthTextureWrapper = BlazeTextureWrapper.createDepth("DhDepthTexture");
	public final BlazeTextureWrapper dhColorTextureWrapper = BlazeTextureWrapper.createColor("DhColorTexture");
	
	
	
	//=============//
	// constructor //
	//=============//
	//region
	
	private BlazeDhMetaRenderer() 
	{
		this.applyRenderer = new BlazeDhApplyRenderer(
			"dh_apply_to_mc",
			null,
			"apply/blaze/vert", "apply/blaze/frag"
		);
	}
	
	//endregion
	
	
	
	//=================//
	// pre/post render //
	//=================//
	//region
	
	@Override
	public void runRenderPassSetup(RenderParams renderParams)
	{
		int oldWidth = this.dhDepthTextureWrapper.getWidth();
		int oldHeight = this.dhDepthTextureWrapper.getHeight();
		
		boolean texturesChanged = false;
		texturesChanged = this.dhDepthTextureWrapper.tryCreateOrResize() | texturesChanged;
		texturesChanged = this.dhColorTextureWrapper.tryCreateOrResize() | texturesChanged;
		
		if (texturesChanged)
		{
			int newTextureWidth = MC_RENDER.getTargetFramebufferViewportWidth();
			int newTextureHeight = MC_RENDER.getTargetFramebufferViewportHeight();
			
			DhApiTextureCreatedParam textureCreatedParam = new DhApiTextureCreatedParam(
				oldWidth, oldHeight,
				newTextureWidth, newTextureHeight
			);
			ApiEventInjector.INSTANCE.fireAllEvents(DhApiAfterColorDepthTextureCreatedEvent.class, textureCreatedParam);
		}
	}
	
	@Override
	public void runRenderPassCleanup(RenderParams renderParams) {}
	
	@Override
	public void applyToMcTexture(RenderParams renderParams)
	{
		GpuTexture mcColorTexture = Minecraft.getInstance().getMainRenderTarget().getColorTexture();
		this.applyRenderer.render(this.dhColorTextureWrapper.texture, this.dhDepthTextureWrapper.texture, mcColorTexture);
	}
	
	//endregion
	
	
	
	//================//
	// clear textures //
	//================//
	//region
	
	@Override
	public void clearDhDepthAndColorTextures(RenderParams renderParams) 
	{
		this.dhDepthTextureWrapper.clearDepth(1.0f);
		
		Color color = MC_RENDER.getSkyColor();
		this.dhColorTextureWrapper.clearColor(ColorUtil.toColorInt(color)); 
	}
	
	//endregion
	
	
}
#endif