package ytsclient

import (
	"github.com/stretchr/testify/assert"
	"strconv"
	"strings"
	"testing"
)

var (
	southParkID    = "2806"
	southParkMovie Movie
	// Removed [YTS.AG] as it may change and does not matter
	southParkMagnet = "magnet:?xt=urn:btih:04AC27DCF18C68470F35969517B477003C325301&" +
		"dn=South+Park%3A+Bigger%2C+Longer+%26+Uncut+%281999%29+%5B1080p%5D&" +
		"tr=udp%3A%2F%2Fglotorrents.pw%3A6969%2Fannounce&" +
		"tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&" +
		"tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&" +
		"tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&" +
		"tr=udp%3A%2F%2Fp4p.arenabg.ch%3A1337&" +
		"tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337"

	tests = []struct {
		name string
		test func(t *testing.T)
	}{{
		name: "default url",
		test: func(t *testing.T) {
			c := NewClient("")
			assert.Equal(t, "https://yts.am/api/v2", c.url)
		},
	}, {
		name: "list",
		test: func(t *testing.T) {
			c := NewClient("")
			_, err := c.List(nil)
			assert.NoError(t, err)
		},
	}, {
		name: "search",
		test: func(t *testing.T) {
			c := NewClient("")
			l, err := c.Search("South Park", nil)
			assert.NoError(t, err)
			southParkMovie = l[0]
			assert.Equal(t, southParkID, strconv.Itoa(southParkMovie.ID))
		},
	}, {
		name: "list 50",
		test: func(t *testing.T) {
			c := NewClient("")
			p := &ListParams{Limit: 50}
			l, err := c.List(p)
			assert.NoError(t, err)
			assert.Equal(t, 50, len(l))
		},
	}, {
		name: "get",
		test: func(t *testing.T) {
			c := NewClient("")
			m, err := c.Get(southParkID, nil)
			assert.NoError(t, err)
			assert.NotEmpty(t, m)
			assert.Equal(t, southParkMovie.Torrents, m.Torrents)
		},
	}, {
		name: "magnet",
		test: func(t *testing.T) {
			m, err := southParkMovie.Magnet(southParkMovie.Torrents[1])
			assert.NoError(t, err)
			// Don't test trackers list nor YTS extension
			e := strings.Split(southParkMagnet, "tr=")
			a := strings.Split(m, "tr=")
			assert.Equal(t, e[0], a[0])
		},
	}, {
		name: "suggestions",
		test: func(t *testing.T) {
			c := NewClient("")
			s, err := c.Suggestions(10)
			assert.NoError(t, err)
			assert.NotEmpty(t, s)
		},
	}}
)

func TestYTSClient(t *testing.T) {
	for _, test := range tests {
		t.Run(test.name, test.test)
	}
}
