//go:build gopher_badge
// +build gopher_badge

package main

import (
	"image/color"
	"machine"

	"tinygo.org/x/drivers/lis3dh"
	"tinygo.org/x/drivers/st7789"
	"tinygo.org/x/drivers/ws2812"
)

const (
	hardwareName  = "GOPHERBADGE"
	displayWidth  = 320
	displayHeight = 240

	hasAccel = true
)

// UI layout constants for gopherbadge (320x240)
const (
	// myNameIs layout
	cornerRadius     = 10
	topBandHeight    = 54
	bottomBandHeight = 20
	helloY           = 34
	myNameIsY        = 54
	gophersY         = 208
	gophersX         = 84

	// menu layout
	menuItemSpace = 20
	menuCircleR   = 6

	numLEDs = 2

	buttonBack = buttonB
)

// snake
const (
	snakeWidthBLOCKS  = 32
	snakeHeightBLOCKS = 24
)

var menuOptions = []int8{
	modeBadge,
	modeSchedule,
	modeAdventure,
	modeLEDs,
	modeAccelerometer,
	modeMusic,
	modeGameSnake,
	modeGameLife,
	modeGameColors,
	modeGameReflex,
	modeGamePacman,
	modeInfo,
}

// Font size thresholds for gopherbadge
const (
	FontWidthThreshold = 300
)

var (
	display  st7789.Device
	btnA     machine.Pin
	btnB     machine.Pin
	btnUp    machine.Pin
	btnLeft  machine.Pin
	btnDown  machine.Pin
	btnRight machine.Pin
)

func initHardware() {
	machine.SPI0.Configure(machine.SPIConfig{
		Frequency: 8000000,
		Mode:      0,
	})

	machine.I2C0.Configure(machine.I2CConfig{
		SCL: machine.I2C0_SCL_PIN,
		SDA: machine.I2C0_SDA_PIN,
	})
	accel = lis3dh.New(machine.I2C0)
	accel.Configure(lis3dh.Config{Address: lis3dh.Address0})

	display = st7789.New(machine.SPI0,
		machine.TFT_RST,       // TFT_RESET
		machine.TFT_WRX,       // TFT_DC
		machine.TFT_CS,        // TFT_CS
		machine.TFT_BACKLIGHT) // TFT_LITE

	display.Configure(st7789.Config{
		Rotation: st7789.ROTATION_270,
		Height:   320,
	})

	neo := machine.NEOPIXELS
	neo.Configure(machine.PinConfig{Mode: machine.PinOutput})
	leds = ws2812.New(neo)

	bzrPin = machine.SPEAKER
	bzrPin.Configure(machine.PinConfig{Mode: machine.PinOutput})

	speaker := machine.SPEAKER_ENABLE
	speaker.Configure(machine.PinConfig{Mode: machine.PinOutput})
	speaker.High()

	btnA = machine.BUTTON_A
	btnB = machine.BUTTON_B
	btnUp = machine.BUTTON_UP
	btnLeft = machine.BUTTON_LEFT
	btnDown = machine.BUTTON_DOWN
	btnRight = machine.BUTTON_RIGHT
	btnA.Configure(machine.PinConfig{Mode: machine.PinInput})
	btnB.Configure(machine.PinConfig{Mode: machine.PinInput})
	btnUp.Configure(machine.PinConfig{Mode: machine.PinInput})
	btnLeft.Configure(machine.PinConfig{Mode: machine.PinInput})
	btnDown.Configure(machine.PinConfig{Mode: machine.PinInput})
	btnRight.Configure(machine.PinConfig{Mode: machine.PinInput})

	black := color.RGBA{0, 0, 0, 255}
	display.FillScreen(black)

}

func getInput() {
	for b := 0; b < 9; b++ {
		buttonsOldState[b] = buttonsState[b]
	}

	buttonsState[buttonUp] = !btnUp.Get()
	buttonsState[buttonRight] = !btnRight.Get()
	buttonsState[buttonDown] = !btnDown.Get()
	buttonsState[buttonLeft] = !btnLeft.Get()
	buttonsState[buttonA] = !btnA.Get()
	buttonsState[buttonB] = !btnB.Get()
}
