// **********************************
// * pipewire buffersize and sample *
// * rate setup                     *
// * ------------------------------ *
// * (c) by THS 2026                *
// **********************************

package main

import (
	"log"
	"os/exec"

	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
)

type PWSetup struct {
	samplerate string
	buffersize string
}

var pwsetup PWSetup

func pwset() (*string, error) {
	cmd := exec.Command("ls -l")
	output, err := cmd.Output()
	if err != nil {
		log.Fatal(err)
	}
	res := (string(output))
	return &res, err
}

func main() {
	app := tview.NewApplication()
	pages := tview.NewPages()

	text := tview.NewTextArea()

	form := tview.NewForm().
		AddDropDown("SampleRate: ", []string{"44100", "48000", "96000"}, 0, func(option string, index int) {
			pwsetup.samplerate = option
		}).
		AddDropDown("Buffer    : ", []string{"64", "128", "256", "512", "1024", "2048"}, 0, func(option string, index int) {
			pwsetup.buffersize = option
		}).
		AddTextView("Notes", "Set sample rate and buffersize for Pipewire", 40, 2, true, false).
		AddButton("Set", func() {
			_, err := pwset()
			if err != nil {
				text.SetText("ok", true)
			} else {
				text.SetText("Fehler", true)
			}
			//text.SetText("Hallo Welt", true)
			pages.SwitchToPage("result")
		}).
		AddButton("Quit", func() {
			app.Stop()
		})

	form.SetBorder(true).SetTitle(" Pipewire setup ").SetTitleAlign(tview.AlignLeft)
	form.SetFieldBackgroundColor(tcell.ColorDarkBlue)
	form.SetFieldTextColor(tcell.ColorAntiqueWhite)

	form.SetButtonBackgroundColor(tcell.ColorDarkBlue)
	pages.AddPage("pwsetup", form, true, true)
	pages.AddPage("result", text, true, false)

	if err := app.SetRoot(pages, true).EnableMouse(true).EnablePaste(true).Run(); err != nil {
		panic(err)
	}
}
