//go:build gopher_badge
// +build gopher_badge

package main

import (
	"time"

	"tinygo.org/x/tinydraw"
	"tinygo.org/x/tinyfont"
	"tinygo.org/x/tinyfont/freesans"
)

func Accel3D() {
	display.FillScreen(colorWhite)
	tinydraw.Rectangle(&display, 50, 16, 260, 16, colorBlack)
	tinydraw.Rectangle(&display, 50, 56, 260, 16, colorBlack)
	tinydraw.Rectangle(&display, 50, 96, 260, 16, colorBlack)

	tinyfont.WriteLine(&display, &freesans.Regular12pt7b, 20, 180, "MOVE the Gopher to see", colorBlack)
	tinyfont.WriteLine(&display, &freesans.Regular12pt7b, 32, 200, "the accelerometer in", colorBlack)
	tinyfont.WriteLine(&display, &freesans.Regular12pt7b, 90, 220, "action.", colorBlack)

	tinyfont.WriteLine(&display, &freesans.Regular24pt7b, 4, 40, "X:", colorBlack)
	tinyfont.WriteLine(&display, &freesans.Regular24pt7b, 4, 80, "Y:", colorBlack)
	tinyfont.WriteLine(&display, &freesans.Regular24pt7b, 4, 120, "Z:", colorBlack)

	x, y, z := accel.ReadRawAcceleration()
	for {
		if !btnA.Get() || !btnB.Get() {
			break
		}

		x, y, z = accel.ReadRawAcceleration()
		x = x / 250
		y = y / 250
		z = z / 250
		if x > 128 {
			x = 128
		}
		if y > 128 {
			y = 128
		}
		if z > 128 {
			z = 128
		}
		if x < -128 {
			x = -128
		}
		if y < -128 {
			y = -128
		}
		if z < -128 {
			z = -128
		}
		display.FillRectangle(51, 22, 258, 6, colorWhite)
		display.FillRectangle(51, 62, 258, 6, colorWhite)
		display.FillRectangle(51, 102, 258, 6, colorWhite)
		if x < 0 {
			display.FillRectangle(179+x, 22, -x, 6, colorRed)
		} else {
			display.FillRectangle(179, 22, x, 6, colorRed)
		}
		if y < 0 {
			display.FillRectangle(179+y, 62, -y, 6, colorGreen)
		} else {
			display.FillRectangle(179, 62, y, 6, colorGreen)
		}
		if z < 0 {
			display.FillRectangle(179+z, 102, -z, 6, colorBlue)
		} else {
			display.FillRectangle(179, 102, z, 6, colorBlue)
		}

		println("X:", x, "Y:", y, "Z:", z)
		time.Sleep(50 * time.Millisecond)
	}
}
