package protocol

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

	"zombiezen.com/go/capnproto2"

	"io"
	"time"
	"bytes"
)

const (
	posterWidth   = "w154"
	backdropWidth = "w1280"
)


func Recode(tmdbFilm *tmdb.Film, tmdbCollection *tmdb.Collection) (io.Writer, error) {
	var err error = nil

	msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	film, err2 := NewRootFilm(seg)
	err = utils.Or(err, err2)

	err = utils.Or(film.SetBackdropPath("https://image.tmdb.org/t/p/"+backdropWidth+tmdbFilm.Backdrop_path), err)

	collection, err2 := film.NewCollection()
	err = utils.Or(err, err2)
	err = utils.Or(collection.SetName(tmdbCollection.Name), err)
	parts, err2 := collection.NewParts(int32(len(tmdbCollection.Parts)))
	err = utils.Or(err, err2)
	for i, tmdbPart := range tmdbCollection.Parts {
		part := parts.At(i)
		part.SetId(uint64(tmdbPart.Id))
		err = utils.Or(part.SetTitle(tmdbPart.Title), err)
		err = utils.Or(part.SetPosterPath("https://image.tmdb.org/t/p/"+posterWidth+tmdbPart.Poster_path), err)
		releaseDate, err2 := part.NewReleaseDate()
		err = utils.Or(err, err2)
		releaseDate.SetYear(int16(tmdbFilm.Release_date.Year()))
		releaseDate.SetMonth(uint8(tmdbFilm.Release_date.Month()))
		releaseDate.SetDay(uint8(tmdbFilm.Release_date.Day()))
		err = utils.Or(part.SetReleaseDate(releaseDate), err)
		//parts.Set(i, part)
	}

	genres, err2 := film.NewGenres(int32(len(tmdbFilm.Genres)))
	err = utils.Or(err, err2)
	for i, tmdbGenre := range tmdbFilm.Genres {
		genres.Set(i, tmdbGenre.Name)
	}

	err = utils.Or(film.SetOriginalTitle(tmdbFilm.Original_title), err)
	err = utils.Or(film.SetOverview(tmdbFilm.Overview), err)
	err = utils.Or(film.SetPosterPath("https://image.tmdb.org/t/p/"+posterWidth+tmdbFilm.Poster_path), err)

	releaseDate, err2 := film.NewReleaseDate()
	err = utils.Or(err, err2)
	date, err2 := time.Parse("2006-01-02", tmdbFilm.Release_date_str)
	err = utils.Or(err, err2)
	releaseDate.SetYear(int16(date.Year()))
	releaseDate.SetMonth(uint8(date.Month()))
	releaseDate.SetDay(uint8(date.Day()))
	err = utils.Or(film.SetReleaseDate(releaseDate), err)

	film.SetRuntime(uint16(tmdbFilm.Runtime))
	film.SetStatus(Film_StatusFromString(tmdbFilm.Status))
	err = utils.Or(film.SetTagline(tmdbFilm.Tagline), err)
	err = utils.Or(film.SetTitle(tmdbFilm.Title), err)
	film.SetMark(tmdbFilm.Vote_average)
	film.SetVoteCount(uint64(tmdbFilm.Vote_count))

	cast, err2 := film.NewCast(int32(len(tmdbFilm.Credits.Cast)))
	err = utils.Or(err, err2)
	for i, tmdbCharacter := range tmdbFilm.Credits.Cast {
		character := cast.At(i)
		err = utils.Or(character.SetName(tmdbCharacter.Character), err)
		person, err2 := character.NewPerson()
		err = utils.Or(err, err2)
		person.SetId(uint64(tmdbCharacter.Id))
		err = utils.Or(person.SetName(tmdbCharacter.Name), err)
		err = utils.Or(person.SetPhotoPath("https://image.tmdb.org/t/p/"+posterWidth+tmdbCharacter.Profile_path), err)
	}

	crew, err2 := film.NewCrew(int32(len(tmdbFilm.Credits.Crew)))
	err = utils.Or(err, err2)
	for i, tmdbCrewMember := range tmdbFilm.Credits.Crew {
		crewMember := crew.At(i)
		err = utils.Or(crewMember.SetJob(tmdbCrewMember.Job), err)
		person, err2 := crewMember.NewPerson()
		err = utils.Or(err, err2)
		person.SetId(uint64(tmdbCrewMember.Id))
		err = utils.Or(person.SetName(tmdbCrewMember.Name), err)
		err = utils.Or(person.SetPhotoPath("https://image.tmdb.org/t/p/"+posterWidth+tmdbCrewMember.Profile_path), err)
	}
	b := bytes.NewBuffer([]byte{})
	err = utils.Or(capnp.NewEncoder(b).Encode(msg), err)
	return b, err
}
