//go:build pybadge
// +build pybadge

package main

import (
	"machine"

	"tinygo.org/x/drivers/lis3dh"
	"tinygo.org/x/drivers/shifter"
	"tinygo.org/x/drivers/st7735"
	"tinygo.org/x/drivers/ws2812"
)

const (
	hardwareName  = "GOBADGE"
	displayWidth  = 160
	displayHeight = 128

	hasAccel = true
)

// UI layout constants for pybadge/gobadge (160x128)
const (
	// myNameIs layout
	cornerRadius     = 8
	topBandHeight    = 26
	bottomBandHeight = 8
	helloY           = 24 // not used, uses "my NAME is" instead
	myNameIsY        = 24
	gophersY         = 110
	gophersX         = 48

	// menu layout
	menuItemSpace = 10
	menuCircleR   = 4

	numLEDs = 5

	buttonBack = buttonSelect
)

// snake
const (
	snakeWidthBLOCKS  = 16
	snakeHeightBLOCKS = 12
)

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

// Font size thresholds for pybadge (smaller screen, use smaller fonts)
const (
	FontWidthThreshold = 150
)

var display st7735.Device
var buttons shifter.Device

func initHardware() {
	machine.SPI1.Configure(machine.SPIConfig{
		SCK:       machine.SPI1_SCK_PIN,
		SDO:       machine.SPI1_SDO_PIN,
		SDI:       machine.SPI1_SDI_PIN,
		Frequency: 8000000,
	})
	machine.I2C0.Configure(machine.I2CConfig{SCL: machine.SCL_PIN, SDA: machine.SDA_PIN})

	accel = lis3dh.New(machine.I2C0)
	accel.Address = lis3dh.Address0
	accel.Configure()

	display = st7735.New(machine.SPI1, machine.TFT_RST, machine.TFT_DC, machine.TFT_CS, machine.TFT_LITE)
	display.Configure(st7735.Config{
		Rotation: st7735.ROTATION_90,
	})

	buttons = shifter.NewButtons()
	buttons.Configure()

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

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

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

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

	buttons.ReadInput()

	buttonsState[buttonUp] = buttons.Pins[shifter.BUTTON_UP].Get()
	buttonsState[buttonRight] = buttons.Pins[shifter.BUTTON_RIGHT].Get()
	buttonsState[buttonDown] = buttons.Pins[shifter.BUTTON_DOWN].Get()
	buttonsState[buttonLeft] = buttons.Pins[shifter.BUTTON_LEFT].Get()
	buttonsState[buttonA] = buttons.Pins[shifter.BUTTON_A].Get()
	buttonsState[buttonB] = buttons.Pins[shifter.BUTTON_B].Get()
	buttonsState[buttonStart] = buttons.Pins[shifter.BUTTON_START].Get()
	buttonsState[buttonSelect] = buttons.Pins[shifter.BUTTON_SELECT].Get()
}
