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/widget"
)

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

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

//go:embed icon.ico
var iconBytes []byte

//go:embed datafont.ttf
var dataFont []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")

	// Apply custom font theme
	fontRes := fyne.NewStaticResource("datafont.ttf", dataFont)
	a.Settings().SetTheme(&customTheme{font: fontRes})

	w := a.NewWindow("COGITATOR-LINK::RITE-SEQUENCE-ENGAGE")
	iconRes := fyne.NewStaticResource("icon.ico", iconBytes)
	w.SetIcon(iconRes)

	const windowWidth = 480
	const windowHeight = 780
	w.Resize(fyne.NewSize(windowWidth, windowHeight))
	w.SetFixedSize(true)

	// Background black
	bg := canvas.NewRectangle(color.RGBA{R: 5, G: 5, B: 5, A: 255})

	// Header
	header := canvas.NewText("+++ INITIATE COGITATOR RITE +++", color.RGBA{R: 100, G: 255, B: 100, A: 255})
	header.TextSize = 18
	header.Alignment = fyne.TextAlignCenter
	header.TextStyle = fyne.TextStyle{Monospace: true, Bold: true}

	// Footer
	footer := canvas.NewText("<< SYSTEM: BLESSED BY THE OMNISSIAH >>", color.RGBA{R: 255, G: 100, B: 100, A: 255})
	footer.TextSize = 14
	footer.Alignment = fyne.TextAlignCenter
	footer.TextStyle = fyne.TextStyle{Italic: true, Monospace: true}

	// Terminal-style blessing
	label := widget.NewLabelWithStyle(blessing, fyne.TextAlignCenter, fyne.TextStyle{Monospace: true})
	label.Wrapping = fyne.TextWrapWord

	// Scroll container
	scroll := container.NewScroll(label)
	scroll.SetMinSize(fyne.NewSize(windowWidth-20, windowHeight-140))

	// Build layout
	content := container.NewBorder(header, footer, nil, nil, scroll)
	w.SetContent(container.NewMax(bg, content))

	w.ShowAndRun()
}
