package ui

import (
	"icdb/internal/ui/routing"
	"icdb/internal/ui/widgets"
	t_ui "icdb/pkg/types/ui"
	"image"
	"image/color"

	"gio.tools/icons"
	"gioui.org/layout"
	"gioui.org/op/clip"
	"gioui.org/op/paint"
	"gioui.org/unit"
	"gioui.org/widget"
	"gioui.org/widget/material"
)

var (
	mainButton       widget.Clickable
	partsButton      widget.Clickable
	categoriesButton widget.Clickable
	locationsButton  widget.Clickable
	settingsButton   widget.Clickable
)

var sidebarWidth = unit.Dp(200)
var sidebarBackground = color.NRGBA{R: 225, G: 225, B: 225, A: 255}

// renderSidebar creates the sidebar UI.
// It renders a colored background and then adds the navigation buttons.
func renderSidebar(th *material.Theme) layout.Widget {
	return func(gtx layout.Context) layout.Dimensions {
		return layout.Stack{Alignment: layout.Center}.Layout(gtx,
			// Expanded: Draw the background to fill the entire left pane.
			layout.Expanded(func(gtx layout.Context) layout.Dimensions {
				// Use a rectangle that covers the full widget area.
				rect := clip.Rect{
					Min: image.Pt(0, 0),
					Max: gtx.Constraints.Max,
				}
				paint.FillShape(gtx.Ops, sidebarBackground, rect.Op())

				widgets.DrawLine(
					gtx.Ops,
					image.Pt(gtx.Constraints.Max.X, 0),
					image.Pt(0, gtx.Constraints.Max.Y),
					2,
					color.NRGBA{R: 204, G: 204, B: 204, A: 255},
				)
				return layout.Dimensions{Size: gtx.Constraints.Max}
			}),
			// Stacked: Lay out the navigation buttons.
			layout.Stacked(func(gtx layout.Context) layout.Dimensions {
				// Internal padding for the sidebar.
				in := layout.Inset{
					Top:    unit.Dp(5),
					Left:   unit.Dp(5),
					Right:  unit.Dp(5),
					Bottom: unit.Dp(5),
				}
				return in.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
					return layout.Flex{
						Axis:      layout.Vertical,
						Alignment: layout.Start,
					}.Layout(gtx,
						layout.Rigid(renderNavButton(th, icons.ActionHome, "Main", &mainButton, t_ui.UIViewHome)),
						layout.Rigid(renderNavButton(th, icons.ActionList, "Parts", &partsButton, t_ui.UIViewParts)),
						layout.Rigid(renderNavButton(th, icons.ActionClass, "Categories", &categoriesButton, t_ui.UIViewCategories)),
						layout.Rigid(renderNavButton(th, icons.ContentInbox, "Locations", &locationsButton, t_ui.UIViewLocations)),
						layout.Rigid(renderNavButton(th, icons.ActionSettings, "Settings", &settingsButton, t_ui.UIViewSettings)),
					)
				})
			}),
		)
	}
}

// renderNavButton creates a navigation icon button.
func renderNavButton(th *material.Theme, icon *widget.Icon, label string, btn *widget.Clickable, target t_ui.UIView) layout.Widget {
	return func(gtx layout.Context) layout.Dimensions {
		if btn.Clicked(gtx) {
			routing.Next(target)
		}

		// Padding for each button, to add spacing between them.
		in := layout.Inset{
			Bottom: unit.Dp(12),
			Left:   unit.Dp(4),
			Right:  unit.Dp(4),
		}

		return in.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
			btn := material.IconButton(th, btn, icon, label)
			btn.Size = unit.Dp(gtx.Constraints.Max.X / 2)
			return btn.Layout(gtx)
		})
	}
}
