package ytsclient

import (
	"fmt"
	"github.com/pkg/errors"
	"net/url"
)

type Movie struct {
	ID                      int       `json:"id"`
	URL                     string    `json:"url"`
	ImdbCode                string    `json:"imdb_code"`
	Title                   string    `json:"title"`
	TitleEnglish            string    `json:"title_english"`
	TitleLong               string    `json:"title_long"`
	Slug                    string    `json:"slug"`
	Year                    int       `json:"year"`
	Rating                  float64   `json:"rating"`
	Runtime                 int       `json:"runtime"`
	Genres                  []string  `json:"genres"`
	DownloadCount           int       `json:"download_count"`
	LikeCount               int       `json:"like_count"`
	State                   string    `json:"state"`
	Summary                 string    `json:"summary"`
	Synopsis                string    `json:"synopsis"`
	DescriptionIntro        string    `json:"description_intro"`
	DescriptionFull         string    `json:"description_full"`
	YtTrailerCode           string    `json:"yt_trailer_code"`
	Language                string    `json:"language"`
	MpaRating               string    `json:"mpa_rating"`
	BackgroundImage         string    `json:"background_image"`
	BackgroundImageOriginal string    `json:"background_image_original"`
	SmallCoverImage         string    `json:"small_cover_image"`
	MediumCoverImage        string    `json:"medium_cover_image"`
	LargeCoverImage         string    `json:"large_cover_image"`
	MediumScreenshotImage1  string    `json:"medium_screenshot_image1"`
	MediumScreenshotImage2  string    `json:"medium_screenshot_image2"`
	MediumScreenshotImage3  string    `json:"medium_screenshot_image3"`
	LargeScreenshotImage1   string    `json:"large_screenshot_image1"`
	LargeScreenshotImage2   string    `json:"large_screenshot_image2"`
	LargeScreenshotImage3   string    `json:"large_screenshot_image3"`
	Cast                    []Cast    `json:"cast"`
	Torrents                []Torrent `json:"torrents"`
	DateUploaded            string    `json:"date_uploaded"`
	DateUploadedUnix        int       `json:"date_uploaded_unix"`
}

type Torrent struct {
	URL              string `json:"url"`
	Hash             string `json:"hash"`
	Quality          string `json:"quality"`
	Seeds            int    `json:"seeds"`
	Peers            int    `json:"peers"`
	Size             string `json:"size"`
	SizeBytes        int    `json:"size_bytes"`
	DateUploaded     string `json:"date_uploaded"`
	DateUploadedUnix int    `json:"date_uploaded_unix"`
}

func (m *Movie) Magnet(t Torrent) (string, error) {
	if !contains(m.Torrents, t) {
		return "", errors.New("invalid torrent")
	}
	b := "magnet:?xt=urn:btih:%s&" +
		"dn=%s&" +
		"tr=udp://glotorrents.pw:6969/announce&" +
		"re=udp://tracker.opentrackr.org:1337/announce&" +
		"tr=udp://torrent.gresille.org:80/announce&" +
		"tr=udp://tracker.openbittorrent.com:80&" +
		"tr=udp://tracker.coppersurfer.tk:6969&" +
		"tr=udp://tracker.leechers-paradise.org:6969&" +
		"tr=udp://tracker.internetwarriors.net:1337"
	title := fmt.Sprintf("%s (%d) [%s]", m.Title, m.Year, t.Quality)
	return fmt.Sprintf(b, url.QueryEscape(t.Hash), url.QueryEscape(title)), nil
}

type Cast struct {
	Name          string `json:"name"`
	CharacterName string `json:"character_name"`
	URLSmallImage string `json:"url_small_image"`
	ImdbCode      string `json:"imdb_code"`
}

func contains(slice []Torrent, item Torrent) bool {
	for _, i := range slice {
		if i == item {
			return true
		}
	}
	return false
}
