package main

import (
	"html/template"
	"io/fs"
	"net/url"
	"path/filepath"
	"strings"
	"time"

	"coffey.dad/internal/models"
	"coffey.dad/ui"
)

type templateData struct {
	CSRFToken       string
	CurrentPage     string
	Post            models.Post
	Posts           []models.Post
	Tags            []models.Tag
	Repo            models.Repo
	Repos           []models.Repo
	GitObjects      []models.GitObject
	Branch          string
	Path            string
	ParentPath      string
	Text            string
	Filter          string
	NewPost         bool
	Form            any
	Flash           string
	IsAuthenticated bool
	CurrentObject   string
	FileNames       []string
}

func humanDate(t time.Time) string {
	l, err := time.LoadLocation("America/New_York")

	var final string
	if err != nil {
		final = t.Format("Jan 02, 2006 at 3:04 PM")
	} else {
		final = t.In(l).Format("Jan 02, 2006 at 3:04 PM")
	}

	return final
}

func concat(s1, s2 string) string {
	return s1 + s2
}

var functions = template.FuncMap{
	"humanDate":  humanDate,
	"pathEscape": url.PathEscape,
	"hasSuffix":  strings.HasSuffix,
	"concat":     concat,
}

func newTemplateCache() (map[string]*template.Template, error) {
	cache := map[string]*template.Template{}

	pages, err := fs.Glob(ui.Files, "html/pages/*.tmpl")
	if err != nil {
		return nil, err
	}

	for _, page := range pages {
		name := filepath.Base(page)

		patterns := []string{
			"html/base.tmpl",
			"html/partials/*.tmpl",
			page,
		}
		ts, err := template.New(name).Funcs(functions).ParseFS(ui.Files, patterns...)
		if err != nil {
			return nil, err
		}

		cache[name] = ts
	}

	return cache, nil
}