/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>

Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.

Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/

package deezer_test

import (
	"encoding/json"
	"os"
	"testing"
	"time"

	"github.com/spf13/viper"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.uploadedlobster.com/scotty/internal/backends/deezer"
	"go.uploadedlobster.com/scotty/internal/config"
)

func TestFromConfig(t *testing.T) {
	c := viper.New()
	c.Set("client-id", "someclientid")
	c.Set("client-secret", "someclientsecret")
	service := config.NewServiceConfig("test", c)
	backend := (&deezer.DeezerApiBackend{}).FromConfig(&service)
	assert.IsType(t, &deezer.DeezerApiBackend{}, backend)
}

func TestListenAsListen(t *testing.T) {
	data, err := os.ReadFile("testdata/listen.json")
	require.NoError(t, err)
	track := deezer.Listen{}
	err = json.Unmarshal(data, &track)
	require.NoError(t, err)
	listen := track.AsListen()
	assert.Equal(t, time.Unix(1700753817, 0), listen.ListenedAt)
	assert.Equal(t, time.Duration(268*time.Second), listen.Duration)
	assert.Equal(t, "New Divide", listen.TrackName)
	assert.Equal(t, "New Divide (Int'l DMD Maxi)", listen.ReleaseName)
	assert.Equal(t, "Linkin Park", listen.ArtistName())
	assert.Equal(t, "deezer.com", listen.AdditionalInfo["music_service"])
	assert.Equal(t, "https://www.deezer.com/track/14631511", listen.AdditionalInfo["origin_url"])
	assert.Equal(t, "https://www.deezer.com/track/14631511", listen.AdditionalInfo["deezer_id"])
}

func TestLovedTrackAsLove(t *testing.T) {
	data, err := os.ReadFile("testdata/track.json")
	require.NoError(t, err)
	track := deezer.LovedTrack{}
	err = json.Unmarshal(data, &track)
	require.NoError(t, err)
	love := track.AsLove()
	assert.Equal(t, time.Unix(1700743848, 0), love.Created)
	assert.Equal(t, time.Duration(255*time.Second), love.Duration)
	assert.Equal(t, "Never Take Me Alive", love.TrackName)
	assert.Equal(t, "Outland", love.ReleaseName)
	assert.Equal(t, "Spear Of Destiny", love.ArtistName())
	assert.Equal(t, "deezer.com", love.AdditionalInfo["music_service"])
	assert.Equal(t, "https://www.deezer.com/track/3265090", love.AdditionalInfo["origin_url"])
	assert.Equal(t, "https://www.deezer.com/track/3265090", love.AdditionalInfo["deezer_id"])
}