package libamuse

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

	"notabug.org/apiote/gott"

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

func getRandomTitle(args ...interface{}) (interface{}, error) {
	languages := args[2].([]language.Tag)
	randomTitle, err := tmdb.GetRandomComedy(languages[0].String())
	args[1] = randomTitle
	return gott.Tuple(args), err
}

func renderIndex(args ...interface{}) interface{} {
	randomComedy := args[1].(string)
	languages := args[2].([]language.Tag)
	renderer := args[3].(front.Renderer)
	args[1] = renderer.RenderIndex(randomComedy, languages)
	return gott.Tuple(args)
}

func ShowIndex(language, mimetype string) (string, error) {
	result, err := gott.
		NewResult(gott.Tuple{requestData{"", "", language, mimetype}, nil, nil, nil}).
		Bind(parseLanguage).
		Bind(getRandomTitle).
		Bind(createRenderer).
		Map(renderIndex).
		Finish()

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