package main

import (
	"fmt"
	"runtime"

	"golang.org/x/mobile/app"
	"golang.org/x/mobile/event/lifecycle"
	"golang.org/x/mobile/event/paint"
	"golang.org/x/mobile/event/size"
	"golang.org/x/mobile/event/touch"
	"golang.org/x/mobile/gl"
)

func main() {
	app.Main(func(a app.App) {
		var glctx gl.Context
		fmt.Printf("does anything log anywhere. does anything work at all?")
		fmt.Printf("does anything log anywhere. does anything work at all?")
		fmt.Printf("does anything log anywhere. does anything work at all?")
		fmt.Printf("does anything log anywhere. does anything work at all?")
		for e := range a.Events() {
			switch e := a.Filter(e).(type) {
			case lifecycle.Event:
				switch e.Crosses(lifecycle.StageVisible) {
				case lifecycle.CrossOn:
					glctx, _ = e.DrawContext.(gl.Context)
					onStart()
				case lifecycle.CrossOff:
					onStop()
					glctx = nil
				}
			case size.Event:
				onResize(e)
			case paint.Event:
				if glctx == nil {
					continue
				}
				onDraw(glctx)
				a.Publish()
			case touch.Event:
				onTouch(e)
			}
		}
	})
}

func onStart() {
	fmt.Println("App started")
}

func onStop() {
	fmt.Println("App stopped")
}

func onResize(e size.Event) {
	runtime.LockOSThread()
	gl.Viewport(0, 0, e.WidthPx, e.HeightPx)
	fmt.Printf("jwc onResize()", e.WidthPx, e.HeightPx)
	fmt.Printf("jwc onResize()", e.WidthPx, e.HeightPx)
	fmt.Printf("jwc onResize()", e.WidthPx, e.HeightPx)
	fmt.Printf("jwc onResize()", e.WidthPx, e.HeightPx)
	fmt.Printf("jwc onResize()", e.WidthPx, e.HeightPx)
	fmt.Printf("jwc onResize()", e.WidthPx, e.HeightPx)
	fmt.Printf("jwc onResize()", e.WidthPx, e.HeightPx)
	fmt.Printf("jwc onResize()", e.WidthPx, e.HeightPx)
	fmt.Printf("jwc onResize()", e.WidthPx, e.HeightPx)
	runtime.UnlockOSThread()
}

func onDraw(glctx gl.Context) {
	glctx.ClearColor(0.1, 0.1, 0.1, 1)
	glctx.Clear(gl.COLOR_BUFFER_BIT)
}

func onTouch(e touch.Event) {
	fmt.Printf("Touch event: X: %f, Y: %f\n", e.X, e.Y)
}

