package igwrap

import (
	"fmt"

	"edgaru089.ink/go/gl01/internal/util/itype"
	"github.com/inkyblackness/imgui-go/v4"
)

const (
	WindowFlagsOverlay imgui.WindowFlags = imgui.WindowFlagsAlwaysAutoResize | imgui.WindowFlagsNoBackground | imgui.WindowFlagsNoNavFocus | imgui.WindowFlagsNoNavInputs | imgui.WindowFlagsNoDecoration | imgui.WindowFlagsNoBringToFrontOnFocus | imgui.WindowFlagsNoSavedSettings | imgui.WindowFlagsNoFocusOnAppearing
)

// MenuItem wraps imgui.MenuItemV to create a
// easy-to-use and intuitive interface.
func MenuItem(name, shortcut string, selected *bool, enabled bool) {
	if imgui.MenuItemV(name, shortcut, *selected, enabled) {
		(*selected) = !(*selected)
	}
}

// Text wraps imgui.Text to create a
// shortcut for fmt.Sprintf.
func Text(format string, a ...interface{}) {
	imgui.Text(fmt.Sprintf(format, a...))
}

// TextBackground wraps imgui.Text to create a shortcut for fmt.Sprintf.
//
// It also fills the background of the text with half-transparant black, and takes care of the padding.
func TextBackground(format string, a ...interface{}) {
	pad := Vec2f(imgui.CurrentStyle().ItemSpacing())
	TextBackgroundV(PackedColor(itype.Vec4f{0, 0, 0, 0.5}), pad, format, a...)
}

// TextBackgroundV wraps imgui.Text to create a shortcut for fmt.Sprintf.
//
// It also fills the background of the text with the given color.
func TextBackgroundV(color imgui.PackedColor, padding itype.Vec2f, format string, a ...interface{}) {
	text := fmt.Sprintf(format, a...)
	orig := imgui.CursorScreenPos()
	size := imgui.CalcTextSize(text, false, 0)
	halfpad := padding.Multiply(0.5)
	imgui.BackgroundDrawList().AddRectFilledV(orig.Minus(Vec2(halfpad)), orig.Plus(size).Plus(Vec2(halfpad)), color, 0, 0)

	imgui.Text(text)
}

// TextBlank blanks a line with a nice-looking height (half the font height plus paddings).
func TextBlank() {
	fontSize := imgui.FontSize()
	imgui.Dummy(imgui.Vec2{X: fontSize / 2, Y: fontSize / 2})
}

// Begin wraps imgui.BeginV to create a
// easy-to-use and intuitive interface.
//
// You only need to call End() if the function
// returns true.
func Begin(id string, open *bool, flags imgui.WindowFlags) bool {
	// skip if the window is not open
	if open != nil && !(*open) {
		return false
	}

	ok := imgui.BeginV(id, open, flags)
	if !ok {
		imgui.End()
	}
	return ok
}

// Sliderf creates a slider with 1 float32 variable.
func Sliderf(label string, a *float32, min, max float32) bool {
	f := *a
	ok := imgui.SliderFloat(label, &f, min, max)
	(*a) = f
	return ok
}

// Slider2f creates a slider with 2 float32 variables.
func Slider2f(label string, a, b *float32, min, max float32) bool {
	f := [2]float32{*a, *b}
	ok := imgui.SliderFloat2(label, &f, min, max)
	(*a) = f[0]
	(*b) = f[1]
	return ok
}

// Slider3f creates a slider with 3 float32 variables.
func Slider3f(label string, a, b, c *float32, min, max float32) bool {
	f := [3]float32{*a, *b, *c}
	ok := imgui.SliderFloat3(label, &f, min, max)
	(*a) = f[0]
	(*b) = f[1]
	(*c) = f[2]
	return ok
}

// Slider4f creates a slider with 4 float32 variables.
func Slider4f(label string, a, b, c, d *float32, min, max float32) bool {
	f := [4]float32{*a, *b, *c, *d}
	ok := imgui.SliderFloat4(label, &f, min, max)
	(*a) = f[0]
	(*b) = f[1]
	(*c) = f[2]
	(*d) = f[3]
	return ok
}

// Sliderd creates a slider with 1 float64 variable.
func Sliderd(label string, a *float64, min, max float64) bool {
	f := float32(*a)
	ok := imgui.SliderFloat(label, &f, float32(min), float32(max))
	(*a) = float64(f)
	return ok
}

// Slider2d creates a slider with 2 float64 variables.
func Slider2d(label string, a, b *float64, min, max float64) bool {
	f := [2]float32{float32(*a), float32(*b)}
	ok := imgui.SliderFloat2(label, &f, float32(min), float32(max))
	(*a) = float64(f[0])
	(*b) = float64(f[1])
	return ok
}

// Slider3d creates a slider with 3 float64 variables.
func Slider3d(label string, a, b, c *float64, min, max float64) bool {
	f := [3]float32{float32(*a), float32(*b), float32(*c)}
	ok := imgui.SliderFloat3(label, &f, float32(min), float32(max))
	(*a) = float64(f[0])
	(*b) = float64(f[1])
	(*c) = float64(f[2])
	return ok
}

// Slider4d creates a slider with 4 float64 variables.
func Slider4d(label string, a, b, c, d *float64, min, max float64) bool {
	f := [4]float32{float32(*a), float32(*b), float32(*c), float32(*d)}
	ok := imgui.SliderFloat4(label, &f, float32(min), float32(max))
	(*a) = float64(f[0])
	(*b) = float64(f[1])
	(*c) = float64(f[2])
	(*d) = float64(f[3])
	return ok
}

// DragVec2d creates a dragger with a itype.Vec2d.
func DragVec2d(label string, vec *itype.Vec2d, speed float64) bool {
	f := [2]float32{float32((*vec)[0]), float32((*vec)[1])}
	ok := imgui.DragFloat2V(label, &f, float32(speed), 0, 0, "%.3f", imgui.SliderFlagsNoRoundToFormat)
	(*vec)[0] = float64(f[0])
	(*vec)[1] = float64(f[1])
	return ok
}

// DragVec2dv creates a dragger with two float64s.
func DragVec2dv(label string, a, b *float64, speed float64) bool {
	f := [2]float32{float32(*a), float32(*b)}
	ok := imgui.DragFloat2V(label, &f, float32(speed), 0, 0, "%.3f", imgui.SliderFlagsNoRoundToFormat)
	(*a) = float64(f[0])
	(*b) = float64(f[1])
	return ok
}

// Dragd creates a dragger with one float64.
func Dragd(label string, a *float64, speed float64) bool {
	f := float32(*a)
	ok := imgui.DragFloatV(label, &f, float32(speed), 0, 0, "%.3f", imgui.SliderFlagsNoRoundToFormat)
	(*a) = float64(f)
	return ok
}

// Checkbox wraps imgui.Checkbox.
//
// It returns true when selected.
func Checkbox(label string, selected bool) bool {
	imgui.Checkbox(label, &selected)
	return selected
}

// DragButtonV wraps imgui.Button to create a button
// that can be dragged around.
//
// The pushid, if not empty, is pushed into the ImGUI stack.
//
// It returns the mouse delta, if dragged, in the last frame.
func DragButtonV(label string, pushid string) (delta itype.Vec2d) {
	if len(pushid) != 0 {
		imgui.PushID(pushid)
		defer imgui.PopID()
	}

	imgui.Button(label)
	if imgui.IsItemActive() {
		d := imgui.CurrentIO().MouseDelta()
		delta = itype.Vec2d{float64(d.X), float64(d.Y)}
	}
	return
}

// DragButton calls DragButtonV("Drag", pushid) to create a button
// that can be dragged around.
//
// The pushid, if not empty, is pushed into the ImGUI stack.
//
// It returns the mouse delta, if dragged, in the last frame.
func DragButton(pushid string) (delta itype.Vec2d) {
	return DragButtonV("Drag", pushid)
}

// DragButtonv wraps DragButton to create a button to be dragged around.
//
// The pushid, if not empty, is pushed into the ImGUI stack.
//
// It returns whether the values has changed.
func DragButtonv(pushid string, x, y *float64, speed float64) bool {
	delta := DragButton(pushid).Multiply(speed)
	if delta == (itype.Vec2d{}) {
		return false
	}
	(*x) += delta[0]
	(*y) += delta[1]
	return true
}

// InputInt calls imgui.InputInt.
//
// It accepts int instead of int32.
func InputInt(label string, val *int) bool {
	x := int32(*val)
	ok := imgui.InputInt(label, &x)
	(*val) = int(x)
	return ok
}
