package libamuse

import (
	"notabug.org/apiote/amuse/accounts"
	"notabug.org/apiote/amuse/db"

	"log"
	"strings"

	"notabug.org/apiote/gott"
)

func MakeAdmin(username string) error {
	return db.MakeAdmin(username)
}

func TouchWantlist() {
	touchableFilms, err := db.GetTouchableFilms()
	if err != nil {
		log.Println(err)
	}
	for _, film := range touchableFilms {
		log.Printf("Touching %s\n", film.ItemUri)
		itemId := strings.Split(film.ItemUri, "/")[1]
		touchFilm(itemId, film.Language, film.Username, film.Country, film.AutoCalendar)
	}
	touchableSeries, err := db.GetTouchableSeries()
	if err != nil {
		log.Println(err)
	}
	auth := accounts.Authentication{Token: ""}
	for _, serie := range touchableSeries {
		log.Printf("Touching %s\n", serie.ItemUri)
		itemId := strings.Split(serie.ItemUri, "/")[1]
		_, _ = ShowTvSerie(itemId, "", "en-GB", "text/html", auth)
	}
}

func touchFilm(id, language, username, country string, autoCalendar bool) {
	request := &RequestData{id: id, language: language, mimetype: "text/html"}
	gott.
		NewResult(gott.Tuple{request, &Result{user: accounts.User{Username: username, Country: country, AutoCalendar: autoCalendar, Language: language}}}).
		Bind(parseLanguage).
		Bind(getFilm).
		Bind(updateCache).
		Bind(isOnWantList).
		Tee(addToCalendar).
		Finish()
}
