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

import com.mojang.blaze3d.vertex.PoseStack
import net.minecraft.client.renderer.MultiBufferSource
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider
import ru.dbotthepony.mc.otm.block.entity.decorative.HoloSignBlockEntity
import ru.dbotthepony.mc.otm.client.font
import ru.dbotthepony.mc.otm.client.render.DynamicBufferSource
import ru.dbotthepony.mc.otm.client.render.RenderGravity
import ru.dbotthepony.mc.otm.client.render.drawAligned
import ru.dbotthepony.mc.otm.core.math.RGBAColor
import ru.dbotthepony.mc.otm.core.math.rotateWithBlockFacing
import ru.dbotthepony.mc.otm.core.math.rotationThree

class HoloSignRenderer(private val context: BlockEntityRendererProvider.Context) : BlockEntityRenderer<HoloSignBlockEntity> {
	override fun render(
		tile: HoloSignBlockEntity,
		partialTick: Float,
		poseStack: PoseStack,
		badSorse: MultiBufferSource,
		p_112311_: Int,
		p_112312_: Int
	) {
		if (tile.redstoneControl.isBlockedByRedstone)
			return

		poseStack.pushPose()
		poseStack.rotateWithBlockFacing(tile.blockState.rotationThree)
		poseStack.translate(0.5f, 0.5f, 0.5f)
		poseStack.scale(0.01f, 0.01f, 0.01f)

		val sorse = DynamicBufferSource.WORLD

		val lines = tile.signText.split('\n')
		val totalHeight = lines.size * font.lineHeight + (lines.size - 1) * 2f
		var y = -totalHeight / 2f

		for (line in lines) {
			font.drawAligned(poseStack = poseStack, buffer = sorse, text = line, align = RenderGravity.TOP_CENTER, x = 0f, y = y, color = RGBAColor.YELLOW.toRGB())
			y += font.lineHeight + 2f
		}

		poseStack.popPose()
	}
}
