package scanner

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Metadata", func() {
	// TODO Need to mock `ffmpeg`
	XContext("ExtractAllMetadata", func() {
		It("correctly parses metadata from all files in folder", func() {
			mds, err := ExtractAllMetadata("tests/fixtures")
			Expect(err).NotTo(HaveOccurred())
			Expect(mds).To(HaveLen(3))

			m := mds["tests/fixtures/test.mp3"]
			Expect(m.Title()).To(Equal("Song"))
			Expect(m.Album()).To(Equal("Album"))
			Expect(m.Artist()).To(Equal("Artist"))
			Expect(m.AlbumArtist()).To(Equal("Album Artist"))
			Expect(m.Composer()).To(Equal("Composer"))
			Expect(m.Compilation()).To(BeTrue())
			Expect(m.Genre()).To(Equal("Rock"))
			Expect(m.Year()).To(Equal(2014))
			n, t := m.TrackNumber()
			Expect(n).To(Equal(2))
			Expect(t).To(Equal(10))
			n, t = m.DiscNumber()
			Expect(n).To(Equal(1))
			Expect(t).To(Equal(2))
			Expect(m.HasPicture()).To(BeTrue())
			Expect(m.Duration()).To(Equal(1))
			Expect(m.BitRate()).To(Equal(476))
			Expect(m.FilePath()).To(Equal("tests/fixtures/test.mp3"))
			Expect(m.Suffix()).To(Equal("mp3"))
			Expect(m.Size()).To(Equal(60845))

			m = mds["tests/fixtures/test.ogg"]
			Expect(err).To(BeNil())
			Expect(m.Title()).To(BeEmpty())
			Expect(m.HasPicture()).To(BeFalse())
			Expect(m.Duration()).To(Equal(3))
			Expect(m.BitRate()).To(Equal(9))
			Expect(m.Suffix()).To(Equal("ogg"))
			Expect(m.FilePath()).To(Equal("tests/fixtures/test.ogg"))
			Expect(m.Size()).To(Equal(4408))
		})

		It("returns error if path does not exist", func() {
			_, err := ExtractAllMetadata("./INVALID/PATH")
			Expect(err).To(HaveOccurred())
		})

		It("returns empty map if there are no audio files in path", func() {
			Expect(ExtractAllMetadata(".")).To(BeEmpty())
		})
	})

	Context("extractMetadata", func() {
		It("parses correctly the compilation tag", func() {
			const outputWithOverlappingTitleTag = `
Input #0, mp3, from '/Users/deluan/Music/iTunes/iTunes Media/Music/Compilations/Putumayo Presents Blues Lounge/09 Pablo's Blues.mp3':
  Metadata:
    compilation     : 1
  Duration: 00:05:02.63, start: 0.000000, bitrate: 140 kb/s`
			md, _ := extractMetadata("tests/fixtures/test.mp3", outputWithOverlappingTitleTag)
			Expect(md.Compilation()).To(BeTrue())
		})

		It("parses correct the title without overlapping with the stream tag", func() {
			const outputWithOverlappingTitleTag = `
Input #0, mp3, from 'groovin.mp3':
  Metadata:
    title           : Groovin' (feat. Daniel Sneijers, Susanne Alt)
  Duration: 00:03:34.28, start: 0.025056, bitrate: 323 kb/s
    Metadata:
      title           : cover
At least one output file must be specified`
			md, _ := extractMetadata("tests/fixtures/test.mp3", outputWithOverlappingTitleTag)
			Expect(md.Title()).To(Equal("Groovin' (feat. Daniel Sneijers, Susanne Alt)"))
		})

		It("ignores case in the tag name", func() {
			const outputWithOverlappingTitleTag = `
Input #0, flac, from '/Users/deluan/Downloads/06. Back In Black.flac':
  Metadata:
    ALBUM           : Back In Black
    DATE            : 1980.07.25
    disc            : 1
    GENRE           : Hard Rock
    TITLE           : Back In Black
    DISCTOTAL       : 1
    TRACKTOTAL      : 10
    track           : 6
  Duration: 00:04:16.00, start: 0.000000, bitrate: 995 kb/s`
			md, _ := extractMetadata("tests/fixtures/test.mp3", outputWithOverlappingTitleTag)
			Expect(md.Title()).To(Equal("Back In Black"))
			Expect(md.Album()).To(Equal("Back In Black"))
			Expect(md.Genre()).To(Equal("Hard Rock"))
			n, t := md.TrackNumber()
			Expect(n).To(Equal(6))
			Expect(t).To(Equal(10))
			n, t = md.DiscNumber()
			Expect(n).To(Equal(1))
			Expect(t).To(Equal(1))
			Expect(md.Year()).To(Equal(1980))
		})

		// TODO Handle multiline tags
		XIt("parses multiline tags", func() {
			const outputWithMultilineComment = `
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'modulo.m4a':
  Metadata:
    comment         : https://www.mixcloud.com/codigorock/30-minutos-com-saara-saara/
                    :
                    : Tracklist:
                    :
                    : 01. Saara Saara
                    : 02. Carta Corrente
                    : 03. X
                    : 04. Eclipse Lunar
                    : 05. Vírus de Sírius
                    : 06. Doktor Fritz
                    : 07. Wunderbar
                    : 08. Quarta Dimensão
  Duration: 00:26:46.96, start: 0.052971, bitrate: 69 kb/s`
			const expectedComment = `https://www.mixcloud.com/codigorock/30-minutos-com-saara-saara/

Tracklist:

01. Saara Saara
02. Carta Corrente
03. X
04. Eclipse Lunar
05. Vírus de Sírius
06. Doktor Fritz
07. Wunderbar
08. Quarta Dimensão
`
			md, _ := extractMetadata("tests/fixtures/test.mp3", outputWithMultilineComment)
			Expect(md.Comment()).To(Equal(expectedComment))
		})
	})

	Context("parseYear", func() {
		It("parses the year correctly", func() {
			var examples = map[string]int{
				"1985":       1985,
				"2002-01":    2002,
				"1969.06":    1969,
				"1980.07.25": 1980,
			}
			for tag, expected := range examples {
				md := &Metadata{tags: map[string]string{"year": tag}}
				Expect(md.Year()).To(Equal(expected))
			}
		})

		It("returns 0 if year is invalid", func() {
			md := &Metadata{tags: map[string]string{"year": "invalid"}}
			Expect(md.Year()).To(Equal(0))
		})
	})
})
