package main

import (
	"embed"
	"fmt"
	"math/rand"
	"os"
	"os/exec"
	"path"
	"strings"

	"github.com/charmbracelet/glamour"
	"github.com/charmbracelet/huh"
)

//go:embed banners/*
var banners embed.FS

//go:embed md/*
var md embed.FS

func banner() (string, error) {
	dirs, err := banners.ReadDir("banners")
	if err != nil {
		return "", err
	}
	mx := len(dirs)
	ix := rand.Intn(mx)
	content, err := banners.ReadFile(path.Join("banners", dirs[ix].Name()))
	if err != nil {
		return "", err
	}

	return string(content), nil

}

func pager() *exec.Cmd {
	return exec.Command("/usr/bin/bat")
}

func _main() error {
	b, err := banner()
	if err != nil {
		return err
	}

	fmt.Println(b)

	var mode string

	huh.NewSelect[string]().Title("whuddyu wanna doo?").
		Options(
			huh.NewOption("Get an info dump", "info"),
			huh.NewOption("RSVP", "rsvp"),
			huh.NewOption("Submit a talk proposal or finished work", "submit"),
			huh.NewOption("Propose a creative jam", "jam"),
			huh.NewOption("Quit", "quit"),
		).Value(&mode).Run()

	switch mode {
	case "info":
		infoContent, err := md.ReadFile("md/info.md")
		if err != nil {
			return err
		}
		out, err := glamour.Render(string(infoContent), "dracula")
		if err != nil {
			return err
		}
		cmd := pager()
		cmd.Stdin = strings.NewReader(out)
		cmd.Stdout = os.Stdout
		err = cmd.Run()
		if err != nil {
			return err
		}
	case "rsvp":
	case "submit":
	case "jam":
	case "quit":
	default:
		return fmt.Errorf("wtf '%s'", mode)
	}

	return nil
}

func main() {
	if err := _main(); err != nil {
		fmt.Fprintf(os.Stderr, ":( failed: %s\n", err.Error())
		os.Exit(1)
	}
}
