package main

import (
	"context"
	"io"
	"os"

	"git.jojodev.com/jolheiser/simpleicons"
	"github.com/a-h/templ"
	"gopkg.in/yaml.v3"
)

type Link struct {
	Name     string `yaml:"name"`
	URL      string `yaml:"url"`
	IconName string `yaml:"icon"`
}

func (l Link) Icon() simpleicons.Icon {
	return simpleicons.Icons[l.IconName]
}

var tailwindCSS = `
@tailwind base;
@tailwind components;
@tailwind utilities;
`

//go:generate templ generate
//go:generate go run .
//go:generate tailwind-ctp -i ./dist/styles.css -o ./dist/styles.css --minify
func main() {
	var links struct {
		Links []Link `yaml:"links"`
	}

	fi, err := os.Open("links.yaml")
	if err != nil {
		panic(err)
	}
	defer fi.Close()
	if err := yaml.NewDecoder(fi).Decode(&links); err != nil {
		panic(err)
	}

	if err := os.MkdirAll("dist", os.ModePerm); err != nil {
		panic(err)
	}

	html, err := os.Create("dist/index.html")
	if err != nil {
		panic(err)
	}
	defer html.Close()

	err = index(links.Links).Render(context.Background(), html)
	if err != nil {
		panic(err)
	}

	css, err := os.Create("dist/styles.css")
	if err != nil {
		panic(err)
	}
	defer css.Close()
	if _, err := css.WriteString(tailwindCSS); err != nil {
		panic(err)
	}
}

func SafeHTML(html string) templ.Component {
	return templ.ComponentFunc(func(ctx context.Context, w io.Writer) (err error) {
		_, err = io.WriteString(w, html)
		return
	})
}
