package main

import (
	"fmt"
	"html/template"
	"net/http"
)

type pageData struct {
	Title         string
	SubTitle      string
	Stylesheets   []string
	HeaderScripts []string
	Scripts       []string
	FlashMessage  string
	FlashClass    string
	LoggedIn      bool
	session       *pageSession
	Site          *siteData

	TemplateData interface{}
}

func (p *pageData) show(tmplName string, w http.ResponseWriter) error {
	for _, tmpl := range []string{
		"htmlheader.html",
		"header.html",
		tmplName,
		"footer.html",
		"htmlfooter.html",
	} {
		if err := outputTemplate(tmpl, p, w); err != nil {
			fmt.Printf("%s\n", err)
			return err
		}
	}
	return nil
}

// outputTemplate
// Spit out a template
func outputTemplate(tmplName string, tmplData interface{}, w http.ResponseWriter) error {
	n := "/templates/" + tmplName
	l := template.Must(template.New("layout").Parse(FSMustString(site.DevMode, n)))
	t := template.Must(l.Parse(FSMustString(site.DevMode, n)))
	return t.Execute(w, tmplData)
}

// redirect can be used only for GET redirects
func redirect(url string, w http.ResponseWriter, req *http.Request) {
	http.Redirect(w, req, url, 303)
}
