package front

import (
	"notabug.org/apiote/amuse/i18n"
	"notabug.org/apiote/amuse/tmdb"

	"bytes"
	"golang.org/x/text/language"
	"html/template"
	"strings"
	"time"
)

type renderData struct {
	Data    interface{}
	Strings i18n.Translation
}

func (d renderData) LetAmuse0() string {
	let_amuse := strings.Split(d.Strings.Index["let_amuse_you"], "{}")
	let_amuse[0], let_amuse[1] = strings.Trim(let_amuse[0], " "), strings.Trim(let_amuse[1], " ")
	return let_amuse[0]
}

func (d renderData) LetAmuse1() string {
	let_amuse := strings.Split(d.Strings.Index["let_amuse_you"], "{}")
	let_amuse[0], let_amuse[1] = strings.Trim(let_amuse[0], " "), strings.Trim(let_amuse[1], " ")
	return let_amuse[1]
}

func (d renderData) FormatDate(date time.Time) string {
	return i18n.FormatDate(date, d.Strings.Global["date_format"], d.Strings.Global)
}

func (d renderData) RenderAsciiDoc(s string) template.HTML {
	return i18n.RenderAsciiDoc(s)
}

func (d renderData) GetErrorData(code int, kind string) string {
	return i18n.GetErrorData(code, d.Strings, kind)
}

type HtmlRenderer struct{}

func (HtmlRenderer) RenderFilm(film *tmdb.Film, languages []language.Tag) string {
	i18n.LoadServerLangs()
	language := i18n.Match(languages)
	strings, err := i18n.LoadStrings(language)
	if err != nil {
		// todo return http:500
	}
	data := renderData{film, strings}
	t, _ := template.ParseFiles("templates/film.html")
	b := bytes.NewBuffer([]byte{})
	err = t.Execute(b, data)
	if err != nil {
		// todo return http:500
	}
	return b.String()
}

func (HtmlRenderer) RenderSearch(results *tmdb.SearchResults, languages []language.Tag) string {
	i18n.LoadServerLangs()
	language := i18n.Match(languages)
	strings, err := i18n.LoadStrings(language)
	if err != nil {
		// todo return http:500
	}
	data := renderData{results, strings}
	t, _ := template.ParseFiles("templates/search.html")
	b := bytes.NewBuffer([]byte{})
	err = t.Execute(b, data)
	if err != nil {
		// todo return http:500
	}
	return b.String()
}

func (HtmlRenderer) RenderIndex(randomComedy string, languages []language.Tag) string {
	i18n.LoadServerLangs()
	language := i18n.Match(languages)
	strings, err := i18n.LoadStrings(language)
	if err != nil {
		// todo return http:500
	}
	data := renderData{randomComedy, strings}
	t, _ := template.ParseFiles("templates/index.html")
	b := bytes.NewBuffer([]byte{})
	err = t.Execute(b, data)
	if err != nil {
		// todo return http:500
	}
	return b.String()
}

func (HtmlRenderer) RenderSerie(serie *tmdb.Serie, languages []language.Tag) string {
	i18n.LoadServerLangs()
	language := i18n.Match(languages)
	strings, err := i18n.LoadStrings(language)
	if err != nil {
		// todo return http:500
	}
	data := renderData{serie, strings}
	t, _ := template.ParseFiles("templates/serie.html")
	b := bytes.NewBuffer([]byte{})
	err = t.Execute(b, data)
	if err != nil {
		// todo return http:500
	}
	return b.String()
}

func (HtmlRenderer) RenderPerson(person *tmdb.Person, languages []language.Tag) string {
	i18n.LoadServerLangs()
	language := i18n.Match(languages)
	strings, err := i18n.LoadStrings(language)
	if err != nil {
		// todo return http:500
	}
	data := renderData{person, strings}
	t, _ := template.ParseFiles("templates/person.html")
	b := bytes.NewBuffer([]byte{})
	err = t.Execute(b, data)
	if err != nil {
		// todo return http:500
	}
	return b.String()
}

func (HtmlRenderer) RenderAbout(languages []language.Tag) string {
	i18n.LoadServerLangs()
	language := i18n.Match(languages)
	strings, err := i18n.LoadStrings(language)
	if err != nil {
		// todo return http:500
	}
	i18n.RenderAsciiDoc(strings.About["doc"])
	data := renderData{nil, strings}
	t, _ := template.ParseFiles("templates/about.html")
	b := bytes.NewBuffer([]byte{})
	err = t.Execute(b, data)
	if err != nil {
		// todo return http:500
	}
	return b.String()
}

func (HtmlRenderer) RenderErrorPage(code int, languages []language.Tag) string {
	i18n.LoadServerLangs()
	language := i18n.Match(languages)
	strings, err := i18n.LoadStrings(language)
	if err != nil {
		// todo return http:500
	}
	i18n.RenderAsciiDoc(strings.About["doc"])
	data := renderData{code, strings}
	t, _ := template.ParseFiles("templates/error.html")
	b := bytes.NewBuffer([]byte{})
	err = t.Execute(b, data)
	if err != nil {
		// todo return http:500
	}
	return b.String()
}
