package main

import (
	"image/color"
	"math"
	"math/rand"
	"strconv"
	"time"

	"git.milar.in/milarin/gui"
	"git.milar.in/milarin/gui/layouts"
	"git.milar.in/milarin/gui/views"
)

func main() {
	//AnchorViewTest()
	//FlowLayoutTest()
	ScrollLayoutTest()
	//BorderLayoutTest()
	//TextViewTest()
}

func AnchorViewTest() {
	textView := views.NewTextView("hello world")
	textView.SetBackground(color.NRGBA{255, 0, 0, 255})
	anchorView := views.NewAnchorView(textView, gui.AnchorBottomRight)

	a, err := gui.NewApp(anchorView)
	if err != nil {
		panic(err)
	}

	a.Background = color.Transparent

	if err := a.Start(); err != nil {
		panic(err)
	}
}

func ScrollLayoutTest() {
	textViews := make([]gui.View, 0, 100)
	rnd := rand.New(rand.NewSource(time.Now().Unix()))
	for i := 0; i < cap(textViews); i++ {
		textView := views.NewTextView(strconv.Itoa(i + 1))
		marginView := views.NewMarginView(textView, 10, 10, 10, 10)
		growView := views.NewGrowView(marginView, true, false)
		constrainView := views.NewConstrainView(growView, 500, -1)

		textViews = append(textViews, constrainView)
		textViews[i].SetBackground(color.NRGBA{
			uint8(rnd.Intn(math.MaxUint8)),
			uint8(rnd.Intn(math.MaxUint8)),
			uint8(rnd.Intn(math.MaxUint8)),
			255,
		})
		/*textView.MouseEvent = func(event gui.MouseEvent) (consumed bool) {
			textView.SetBackground(color.White)
			return true
		}*/
	}

	flowLayout := layouts.NewFlowLayout(gui.Vertical)
	flowLayout.AppendViews(textViews...)
	flowLayout.SetBackground(color.White)

	scrollView := views.NewScrollView(flowLayout)
	//scrollView.Scroll(0, 100)

	a, err := gui.NewApp(scrollView)
	if err != nil {
		panic(err)
	}

	a.Background = color.Transparent

	if err := a.Start(); err != nil {
		panic(err)
	}
}

func FlowLayoutTest() {
	textViews := make([]gui.View, 0, 10)
	rnd := rand.New(rand.NewSource(time.Now().Unix()))
	for i := 0; i < cap(textViews); i++ {
		textView := views.NewTextView(strconv.Itoa(i + 1))
		textViews = append(textViews, textView)
		textViews[i].SetBackground(color.NRGBA{
			uint8(rnd.Intn(math.MaxUint8)),
			uint8(rnd.Intn(math.MaxUint8)),
			uint8(rnd.Intn(math.MaxUint8)),
			255,
		})
		textView.MouseMove = func(event gui.MouseEvent) (consumed bool) {
			textView.SetBackground(color.White)
			return true
		}
	}

	flowLayout := layouts.NewFlowLayout(gui.Vertical)
	flowLayout.AppendViews(textViews...)

	a, err := gui.NewApp(flowLayout)
	if err != nil {
		panic(err)
	}

	if err := a.Start(); err != nil {
		panic(err)
	}
}

func TextViewTest() {
	a, err := gui.NewApp(views.NewTextView("hello world"))
	if err != nil {
		panic(err)
	}

	if err := a.Start(); err != nil {
		panic(err)
	}
}

func BorderLayoutTest() {
	top := views.NewTextView("top")
	bottom := views.NewTextView("bottom")
	left := views.NewTextView("left")
	right := views.NewTextView("right")
	center := views.NewTextView("center")

	top.SetBackground(color.NRGBA{255, 200, 200, 255})
	bottom.SetBackground(color.NRGBA{200, 255, 200, 255})
	left.SetBackground(color.NRGBA{200, 200, 255, 255})
	right.SetBackground(color.NRGBA{255, 255, 200, 255})
	center.SetBackground(color.NRGBA{200, 255, 255, 255})

	top.SetFontSize(48)

	borderLayout := layouts.NewBorderLayout()
	borderLayout.SetView(top, layouts.Top)
	borderLayout.SetView(bottom, layouts.Bottom)
	borderLayout.SetView(left, layouts.Left)
	borderLayout.SetView(right, layouts.Right)
	borderLayout.SetView(center, layouts.Center)

	a, err := gui.NewApp(borderLayout)
	if err != nil {
		panic(err)
	}

	if err := a.Start(); err != nil {
		panic(err)
	}
}
