//go:build nicenano
// +build nicenano

package main

import (
	"image/color"
	"machine"

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

const (
	hardwareName  = "NICEBADGE"
	displayWidth  = 240
	displayHeight = 135

	hasAccel = false
)

// UI layout constants for nicebadge (240x135)
const (
	// myNameIs layout
	cornerRadius     = 10
	topBandHeight    = 20
	bottomBandHeight = 10
	helloY           = 20
	myNameIsY        = 21
	gophersY         = 110
	gophersX         = 48

	// menu layout
	menuItemSpace = 20
	menuCircleR   = 6

	numLEDs = 2

	buttonBack = buttonB
)

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

var menuOptions = []int8{
	modeBadge,
	modeSchedule,
	modeAdventure,
	modeLEDs,
	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
	btnStart machine.Pin

	ax  machine.ADC
	ay  machine.ADC
	enc *encoders.QuadratureDevice
)

func initHardware() {
	machine.SPI0.Configure(machine.SPIConfig{
		SCK:       machine.P1_01,
		SDO:       machine.P1_02,
		Frequency: 2.0 * machine.MHz,
		Mode:      0,
	})

	machine.I2C1.Configure(machine.I2CConfig{
		SDA:       machine.P0_17,
		SCL:       machine.P0_20,
		Frequency: 2.0 * machine.MHz,
	})
	display = st7789.New(machine.SPI0,
		machine.P1_15, // TFT_RESET
		machine.P1_13, // TFT_DC
		machine.P0_10, // TFT_CS
		machine.P0_09) // TFT_LITE

	display.Configure(st7789.Config{
		Rotation:     st7789.ROTATION_90,
		Width:        135,
		Height:       240,
		RowOffset:    40,
		ColumnOffset: 53,
		//FrameRate:  st7789.FRAMERATE_111,
		//VSyncLines: st7789.MAX_VSYNC_SCANLINES,
	})

	neo := machine.P1_11
	neo.Configure(machine.PinConfig{Mode: machine.PinOutput})
	leds = ws2812.NewWS2812(neo)

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

	machine.InitADC()
	ax = machine.ADC{Pin: machine.P0_02}
	ay = machine.ADC{Pin: machine.P0_29}
	ax.Configure(machine.ADCConfig{})
	ay.Configure(machine.ADCConfig{})

	enc = encoders.NewQuadratureViaInterrupt(machine.P1_00, machine.P0_24)
	enc.Configure(encoders.QuadratureConfig{
		Precision: 4,
	})

	btnStart = machine.P0_22
	btnA = machine.P1_06
	btnB = machine.P1_04
	btnStart.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
	btnA.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
	btnB.Configure(machine.PinConfig{Mode: machine.PinInputPullup})

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

}

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

	buttonsState[buttonA] = !btnA.Get()
	buttonsState[buttonB] = !btnB.Get()
	buttonsState[buttonStart] = !btnStart.Get()

	buttonsState[buttonLeft] = (ax.Get() < 12000)
	buttonsState[buttonRight] = (ax.Get() > 52000)
	buttonsState[buttonDown] = (ay.Get() < 12000)
	buttonsState[buttonUp] = (ay.Get() > 52000)
}
