package main

import (
	"log"
	"os"
	
	"github.com/p9c/p9/pkg/gel/gio/app"
	"github.com/p9c/p9/pkg/gel/gio/io/system"
	"github.com/p9c/p9/pkg/gel/gio/layout"
	"github.com/p9c/p9/pkg/gel/gio/op"
	"github.com/p9c/p9/pkg/gel/gio/op/paint"
	"github.com/p9c/p9/pkg/gel/gio/unit"
	
	"github.com/p9c/p9/pkg/gui"
	"github.com/p9c/p9/pkg/gui/fonts/p9fonts"
	"github.com/p9c/p9/pkg/gui/toast"
)

var (
	th         = gui.NewTheme(p9fonts.Collection(), nil)
	btnDanger  = th.Clickable()
	btnWarning = th.Clickable()
	btnSuccess = th.Clickable()
)

func main() {
	go func() {
		w := app.NewWindow(app.Size(unit.Px(150*6+50), unit.Px(150*6-50)))
		if e := loop(w); dbg.Chk(e) {
			log.ftl.Ln(err)
		}
		os.Exit(0)
	}()
	app.Main()
}

func loop(w *app.Window) (e error) {
	var ops op.Ops
	t := toast.New(th)
	for {
		e := <-w.Events()
		switch e := e.(type) {
		case system.DestroyEvent:
			return e.Err
		case system.FrameEvent:
			gtx := layout.NewContext(&ops, e)
			paint.Fill(gtx.Ops, gui.HexNRGB("e5e5e5FF"))
			op.InvalidateOp{}.Add(gtx.Ops)
			
			th.Inset(
				0.25,
				th.VFlex().
					Rigid(
						th.Inset(
							0.1,
							th.Button(btnDanger).Text("Danger").Background("Gray").Color("Danger").Fn,
						).Fn,
					).
					Rigid(
						th.Inset(
							0.1,
							th.Button(btnWarning).Text("Warning").Background("Gray").Color("Warning").Fn,
						).Fn,
					).
					Rigid(
						th.Inset(
							0.1,
							th.Button(btnSuccess).Text("Success").Background("Gray").Color("Success").Fn,
						).Fn,
					).Fn,
			).Fn(gtx)
			
			for btnDanger.Clicked() {
				t.AddToast("Danger", "Danger content", "Danger")
			}
			for btnSuccess.Clicked() {
				t.AddToast("Success", "Success content", "Success")
			}
			for btnWarning.Clicked() {
				t.AddToast("Warning", "Warning content", "Warning")
			}
			
			t.DrawToasts()(gtx)
			e.Frame(gtx.Ops)
			w.Invalidate()
		}
	}
}
