package media_test

import (
	"testing"

	"git.netflux.io/rob/clipper/media"
	youtubev2 "github.com/kkdai/youtube/v2"
	"github.com/stretchr/testify/assert"
)

func TestFilterAudio(t *testing.T) {
	formats := []youtubev2.Format{
		{
			MimeType:        `audio/webm; codecs="opus"`,
			ContentLength:   38573,
			AudioChannels:   1,
			AudioSampleRate: "16000",
		},
		{
			MimeType:        `audio/webm; codecs="opus"`,
			ContentLength:   39458,
			AudioChannels:   2,
			AudioSampleRate: "16000",
		},
		{
			MimeType:        `audio/mp4; codecs="mp4a.40.2"`,
			ContentLength:   118394,
			AudioChannels:   1,
			AudioSampleRate: "48000",
		},
		{
			MimeType:        `audio/webm; codecs="opus"`,
			ContentLength:   127393,
			AudioChannels:   2,
			AudioSampleRate: "48000",
		},
		{
			MimeType:        `audio/webm; codecs="opus"`,
			ContentLength:   123245,
			AudioChannels:   2,
			AudioSampleRate: "48000",
		},
	}

	sortedFormats := media.FilterYoutubeAudio(formats)

	assert.Equal(t, formats[1], sortedFormats[0])
	assert.Equal(t, formats[4], sortedFormats[1])
	assert.Equal(t, formats[3], sortedFormats[2])
	assert.Equal(t, formats[0], sortedFormats[3])
	assert.Equal(t, formats[2], sortedFormats[4])
}

func TestFilterVideo(t *testing.T) {
	formats := []youtubev2.Format{
		{
			MimeType:      `audio/webm; codecs="opus"`,
			QualityLabel:  "120p",
			FPS:           30,
			ContentLength: 39402,
		},
		{
			MimeType:      `video/mp4; codecs="avc1.42001E, mp4a.40.2"`,
			QualityLabel:  "240p",
			FPS:           30,
			ContentLength: 40353,
		},
		{
			MimeType:      `video/mp4; codecs="avc1.42001E, mp4a.40.2"`,
			QualityLabel:  "720p",
			FPS:           30,
			ContentLength: 393103,
		},
		{
			MimeType:      `video/mp4; codecs="avc1.42001E, mp4a.40.2"`,
			QualityLabel:  "360p",
			FPS:           0,
			ContentLength: 20403,
		},
	}

	sortedFormats := media.FilterYoutubeVideo(formats)

	assert.Len(t, sortedFormats, 3)
	assert.Equal(t, formats[1], sortedFormats[0])
	assert.Equal(t, formats[2], sortedFormats[1])
	assert.Equal(t, formats[0], sortedFormats[2])
}

func TestSortThumbnails(t *testing.T) {
	thumbnails := []youtubev2.Thumbnail{
		{
			Width:  250,
			Height: 100,
		},
		{
			Width:  1920,
			Height: 1080,
		},
		{
			Width:  50,
			Height: 33,
		},
		{
			Width:  75,
			Height: 51,
		},
	}

	media.SortYoutubeThumbnails(thumbnails)

	assert.Equal(t, uint(250), thumbnails[0].Width)
	assert.Equal(t, uint(1920), thumbnails[1].Width)
	assert.Equal(t, uint(50), thumbnails[2].Width)
	assert.Equal(t, uint(75), thumbnails[3].Width)
}
