package render

import (
	"math"
	"time"

	"edgaru089.ml/go/gl01/internal/asset"
	"edgaru089.ml/go/gl01/internal/igwrap"
	"edgaru089.ml/go/gl01/internal/io"
	"edgaru089.ml/go/gl01/internal/util"
	"edgaru089.ml/go/gl01/internal/util/itype"
	"github.com/inkyblackness/imgui-go/v4"
)

func (r *WorldRenderer) renderDebugInfo() {
	// Render information
	if igwrap.Begin("F3", nil, 0) {
		igwrap.TextBlank()

		igwrap.TextBackground("WorldRender: lastframe %.3fms", float64(io.Diagnostics.Times.Render.Nanoseconds())/float64(time.Millisecond))

		isize := asset.WorldTextureAtlas.ImageSize
		igwrap.TextBackground("Texture Atlas Size: (%dx%d)", isize[0], isize[1])
		imgui.SameLine()
		igwrap.TextBackground("[Hover]")
		if imgui.IsItemHoveredV(imgui.HoveredFlagsAllowWhenDisabled) {
			_, wheely := imgui.CurrentIO().MouseWheel()
			if math.Abs(float64(wheely)) > 1e-3 {
				atlasScale = util.Maxf(1, atlasScale+wheely)
			}
			imgui.BeginTooltip()
			igwrap.Image(r.texture.Handle(), isize.ToFloat32().Multiply(atlasScale), itype.Rectf{0, 0, 1, 1})
			imgui.EndTooltip()
		}

		imgui.End()
	}

	imgui.SetNextWindowPosV(imgui.Vec2{X: float32(r.lastDisplaySize[0]), Y: 0}, imgui.ConditionAlways, imgui.Vec2{X: 1, Y: 0})
	if igwrap.Begin("Renderer Textures/Outputs", nil, igwrap.WindowFlagsOverlay) {
		imgui.PushStyleVarVec2(imgui.StyleVarItemSpacing, imgui.Vec2{})

		imageSize := r.lastDisplaySize.ToFloat32().Multiply(0.25)
		imageSize[1] -= imgui.CurrentStyle().WindowPadding().Y / 2
		imageSize[0] = imageSize[1] / float32(r.lastDisplaySize[1]) * float32(r.lastDisplaySize[0])

		igwrap.Image(r.gbuffer.pos, imageSize, itype.Rectf{0, 0, 1, 1})
		igwrap.Image(r.gbuffer.norm, imageSize, itype.Rectf{0, 0, 1, 1})
		igwrap.Image(r.gbuffer.color, imageSize, itype.Rectf{0, 0, 1, 1})
		igwrap.Image(r.ssao.ambient, imageSize, itype.Rectf{0, 0, 1, 1})

		imgui.PopStyleVar()
		imgui.End()
	}

}
