package libamuse

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

	"notabug.org/apiote/gott"

	"golang.org/x/text/language"
)

func getPerson(args ...interface{}) (interface{}, error) {
	data := args[0].(requestData)
	languages := args[2].([]language.Tag)
	tmdbPerson, err := tmdb.GetPerson(data.id, languages[0].String(), data.etag)
	args[1] = tmdbPerson
	return gott.Tuple(args), err
}

func renderPerson(args ...interface{}) interface{} {
	film := args[1].(*tmdb.Person)
	renderer := args[3].(front.Renderer)
	languages := args[2].([]language.Tag)
	args[1] = renderer.RenderPerson(film, languages)
	return gott.Tuple(args)
}

func ShowPerson(id, etag, languages, mimetype string) (string, error) {
	result, err := gott.
		NewResult(gott.Tuple{requestData{id, etag, languages, mimetype}, nil, nil, nil}).
		Bind(parseLanguage).
		Bind(getPerson).
		Bind(createRenderer).
		Map(renderPerson).
		Finish()

	if err != nil {
		return "", err
	} else {
		return result.(gott.Tuple)[1].(string), nil
	}
}
