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 net.minecraft.core.Direction
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.block.tech.EnergyCounterBlock
import ru.dbotthepony.mc.otm.block.entity.tech.EnergyCounterBlockEntity
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.client.render.*
import ru.dbotthepony.mc.otm.core.math.RGBAColor
import ru.dbotthepony.mc.otm.core.math.asAngle
import ru.dbotthepony.mc.otm.core.util.formatPower
import ru.dbotthepony.mc.otm.core.math.times
import kotlin.math.PI

class EnergyCounterRenderer(private val context: BlockEntityRendererProvider.Context) : BlockEntityRenderer<EnergyCounterBlockEntity> {
	override fun render(
		tile: EnergyCounterBlockEntity,
		p_112308_: Float,
		poseStack: PoseStack,
		p_112310_: MultiBufferSource,
		p_112311_: Int,
		p_112312_: Int
	) {
		val screenDir: Direction = tile.blockState.getValue(EnergyCounterBlock.IF_DIRECTION)
		val inputDir: Direction = tile.blockState.getValue(EnergyCounterBlock.INPUT_DIRECTION)

		poseStack.pushPose()
		poseStack.translate(0.5, 0.5, 0.5)
		poseStack.translate(screenDir.normal * 0.44)

		val font = minecraft.font

		poseStack.scale(0.01f, 0.01f, 0.01f)

		val sorse = DynamicBufferSource.WORLD

		if (screenDir === Direction.DOWN) {
			poseStack.rotateAroundPoint(poseStack.translation(), inputDir.asAngle().copy(pitch = PI, roll = PI / 2))
		} else if (screenDir === Direction.UP) {
			poseStack.rotateAroundPoint(poseStack.translation(), inputDir.asAngle().copy(pitch = 0.0, roll = PI / 2))
		} else {
			poseStack.rotateAroundPoint(poseStack.translation(), screenDir.asAngle().copy(pitch = PI))
		}

		var y = -16f

		val finalX = font.drawAligned(poseStack, sorse, "00000000", RenderGravity.CENTER_CENTER, -4f, y, RGBAColor.GRAY)
		font.drawAligned(poseStack, sorse, "00000000", RenderGravity.CENTER_CENTER, -4f, y + font.lineHeight, RGBAColor.GRAY)
		font.drawAligned(poseStack, sorse, "/t", RenderGravity.CENTER_LEFT, finalX.toFloat(), y, RGBAColor.GRAY)
		font.drawAligned(poseStack, sorse, "/s", RenderGravity.CENTER_LEFT, finalX.toFloat(), y + font.lineHeight, RGBAColor.GRAY)

		poseStack.pushPose()
		poseStack.translate(-0.1, -0.1, -0.1)
		font.drawAligned(poseStack, sorse, tile.lastTick.toString(0), RenderGravity.CENTER_RIGHT, finalX.toFloat(), y, RGBAColor.WHITE)
		font.drawAligned(poseStack, sorse, tile.sumHistory(20).toString(0), RenderGravity.CENTER_RIGHT, finalX.toFloat(), y + font.lineHeight, RGBAColor.WHITE)
		poseStack.popPose()

		y += font.lineHeight * 3

		font.drawAligned(poseStack, sorse, TOTAL, RenderGravity.CENTER_CENTER, 0f, y, RGBAColor.WHITE)
		font.drawAligned(poseStack, sorse, tile.passed.formatPower(), RenderGravity.CENTER_CENTER, 0f, y + font.lineHeight, RGBAColor.WHITE)

		poseStack.popPose()
	}

	override fun getViewDistance() = 32

	companion object {
		private val TOTAL = TranslatableComponent("otm.gui.total_raw")
	}
}
