package layouts

import (
	"fyne.io/fyne/v2"
)

type FloatingControlsLocationEnum uint32

const (
	FloatingControlsTopLeft = iota
	FloatingControlsTopRight
	FloatingControlsBottomLeft
	FloatingControlsBottomRight
	FloatingControlsCenter
	FloatingControlsTopMiddle
	FloatingControlsRightMiddle
	FloatingControlsBottomMiddle
	FloatingControlsLeftMiddle
)

type floatingControlsLayout struct {
	FloatingControlsLocation FloatingControlsLocationEnum
}

// NewFloatingControlsLayout creates a new floatingControlsLayout instance
// This layout contains exactly TWO items, one background item (first) and one
//  floating container that floats according to FloatingControlsLocation
func NewFloatingControlsLayout() floatingControlsLayout {
	return floatingControlsLayout{FloatingControlsTopRight}
}

func (d *floatingControlsLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
	if len(objects) > 1 {
		return objects[0].MinSize()
	}
	return fyne.NewSize(0, 0)
}

func (d *floatingControlsLayout) Layout(objects []fyne.CanvasObject, containerSize fyne.Size) {
	if len(objects) == 2 {
		o := objects[0]
		size := containerSize
		o.Resize(size)
		o.Move(fyne.NewPos(0, 0))

		o = objects[1]
		size = o.MinSize()
		o.Resize(size)
		switch d.FloatingControlsLocation {
		case FloatingControlsTopLeft:
			o.Move(fyne.NewPos(0, 0))
		case FloatingControlsTopMiddle:
			o.Move(fyne.NewPos((containerSize.Width/2)-(size.Width/2), 0))
		case FloatingControlsTopRight:
			o.Move(fyne.NewPos(containerSize.Width-size.Width, 0))
		case FloatingControlsBottomLeft:
			o.Move(fyne.NewPos(0, containerSize.Height-size.Height))
		case FloatingControlsBottomRight:
			o.Move(fyne.NewPos(containerSize.Width-size.Width, containerSize.Height-size.Height))
		case FloatingControlsBottomMiddle:
			o.Move(fyne.NewPos((containerSize.Width/2)-(size.Width/2), containerSize.Height-size.Height))
		case FloatingControlsCenter:
			o.Move(fyne.NewPos((containerSize.Width/2)-(size.Width/2), (containerSize.Height/2)-(size.Height/2)))
		case FloatingControlsLeftMiddle:
			o.Move(fyne.NewPos(0, (containerSize.Height/2)-(size.Height/2)))
		case FloatingControlsRightMiddle:
			o.Move(fyne.NewPos(containerSize.Width-size.Width, (containerSize.Height/2)-(size.Height/2)))
		}

	}
}
