package view

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

var (
	available = true
	matrix = "zgvhdgh0b3nwawv6"
)

type Page struct {
	tpl *template.Template
	Title string
	Name string
	Matrix string
	Available bool
}

var titles = map[string]string {
	"home": "Home",
	"services": "Services",
	"contact": "Contact",
	"web": "Web Development",
	"projects": "Projects",
	"about": "About",
}

var paths = map[string]string {
	"home": "view/home.tpl",
	"services": "view/services.tpl",
	"contact": "view/contact.tpl",
	"web": "view/web.tpl",
	"projects": "view/projects.tpl",
	"about": "view/about.tpl",
}

var pages = map[string]Page {
	"home": cache("home"),
	"services": cache("services"),
	"contact": cache("contact"),
	"web": cache("web"),
	"projects": cache("projects"),
	"about": cache("about"),
}

func parse(path string) *template.Template {
	var paths = []string{"view/master.tpl", path}
	tpl, err := template.ParseFiles(paths...)
	if err != nil {
		log.Print(err, "| Could not parse:", path)
	}
	return tpl
}

func Create(name string) Page {
	tpl := parse(paths[name])
	title := titles[name]
	return Page{tpl: tpl,
				Title: title,
				Name: name,
				Matrix: matrix,
				Available: available}
}

func cache(name string) Page {
	var p = []string{"view/master.tpl", paths[name]}
	tpl := template.Must(template.ParseFiles(p...))
	return Page{tpl: tpl,
				Title: titles[name],
				Name: name,
				Matrix: matrix,
				Available: available}
}

func Get(name string) Page {
	return pages[name]
}
func (page Page) Render(w http.ResponseWriter) {
	err := page.tpl.Execute(w, page)
	if err != nil {
		log.Print(err)
	}
}