package main

import (
	"bytes"
	_ "embed"
	"fmt"
	"image/color"
	"io"
	"time"

	"github.com/faiface/beep/mp3"
	"github.com/faiface/beep/speaker"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
)

//go:embed blessing.txt
var blessingBytes []byte

//go:embed chant.mp3
var chantBytes []byte

func playChant() {
	streamer, format, err := mp3.Decode(io.NopCloser(bytes.NewReader(chantBytes)))
	if err != nil {
		fmt.Printf("ERROR: Unable to decode chant: %v\n", err)
		return
	}
	speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
	speaker.Play(streamer)
}

func main() {
	blessing := string(blessingBytes)

	go playChant()

	a := app.NewWithID("com.omnissiah.blessing")
	a.Settings().SetTheme(theme.DarkTheme())
	w := a.NewWindow("🔧 Rite of Activation")

	// Lock resolution
	const windowWidth = 400
	const windowHeight = 800
	w.Resize(fyne.NewSize(windowWidth, windowHeight))
	w.SetFixedSize(true)

	// Background
	bg := canvas.NewRectangle(color.RGBA{R: 10, G: 10, B: 10, A: 255})
	bg.Resize(fyne.NewSize(windowWidth, windowHeight))

	// Header and footer
	header := canvas.NewText("+++ ACTIVATE RITE +++", color.RGBA{R: 200, G: 40, B: 40, A: 255})
	header.TextSize = 20
	header.Alignment = fyne.TextAlignCenter
	header.TextStyle = fyne.TextStyle{Bold: true}
	footer := canvas.NewText("<< OMNISSIAH WATCHES >>", color.RGBA{R: 255, G: 200, B: 200, A: 255})
	footer.TextSize = 16
	footer.Alignment = fyne.TextAlignCenter
	footer.TextStyle = fyne.TextStyle{Italic: true}

	// Center blessing
	label := widget.NewLabel(blessing)
	label.Alignment = fyne.TextAlignCenter
	label.Wrapping = fyne.TextWrapWord

	scroll := container.NewScroll(label)
	scroll.SetMinSize(fyne.NewSize(420, 760))

	// Use Border layout to keep footer anchored
	mainContent := container.NewBorder(header, footer, nil, nil, scroll)

	// Final layout with background
	w.SetContent(container.NewMax(bg, mainContent))
	w.ShowAndRun()
}
