package ru.dbotthepony.mc.otm.client.render.entity

import com.mojang.blaze3d.vertex.PoseStack
import com.mojang.math.Axis
import net.minecraft.client.renderer.MultiBufferSource
import net.minecraft.client.renderer.RenderType
import net.minecraft.client.renderer.entity.EntityRenderer
import net.minecraft.client.renderer.entity.EntityRendererProvider
import net.minecraft.client.renderer.texture.OverlayTexture
import net.minecraft.resources.ResourceLocation
import ru.dbotthepony.mc.otm.OverdriveThatMatters.loc
import ru.dbotthepony.mc.otm.entity.PlasmaProjectile

class PlasmaProjectileRenderer(context: EntityRendererProvider.Context) : EntityRenderer<PlasmaProjectile>(context) {
	override fun getTextureLocation(p_114482_: PlasmaProjectile): ResourceLocation = PLASMA_PROJECTILE_LOCATION

	override fun render(
		entity: PlasmaProjectile,
		entityYaw: Float,
		partialTick: Float,
		poseStack: PoseStack,
		buffer: MultiBufferSource,
		packedLight: Int
	) {
		val consumer = buffer.getBuffer(RENDER_TYPE)

		poseStack.pushPose()

		poseStack.mulPose(entityRenderDispatcher.cameraOrientation())
		poseStack.mulPose(Axis.YP.rotationDegrees(180f))
		poseStack.scale(.3f, .3f, .3f)

		val matrix = poseStack.last().pose()
		val normal = poseStack.last().normal()

		consumer.vertex(matrix, -.5f, -.5f, 0f).color(1f, 1f, 1f, 1f).uv(0f, 1f).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(packedLight).normal(normal, 0f, 1f, 0f).endVertex()
		consumer.vertex(matrix, .5f, -.5f, 0f).color(1f, 1f, 1f, 1f).uv(1f, 1f).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(packedLight).normal(normal, 0f, 1f, 0f).endVertex()
		consumer.vertex(matrix, .5f, .5f, 0f).color(1f, 1f, 1f, 1f).uv(1f, 0f).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(packedLight).normal(normal, 0f, 1f, 0f).endVertex()
		consumer.vertex(matrix, -.5f, .5f, 0f).color(1f, 1f, 1f, 1f).uv(0f, 0f).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(packedLight).normal(normal, 0f, 1f, 0f).endVertex()

		poseStack.popPose()

		super.render(entity, entityYaw, partialTick, poseStack, buffer, packedLight)
	}

	companion object {
		private val PLASMA_PROJECTILE_LOCATION: ResourceLocation = loc("textures/misc/plasma_ball.png")
		private val RENDER_TYPE = RenderType.itemEntityTranslucentCull(PLASMA_PROJECTILE_LOCATION)
	}
}
