-- | Track API module
{-# OPTIONS_HADDOCK prune #-}
module Network.Lastfm.API.Track
  ( addTags, ban, getBuyLinks, getCorrection, getFingerprintMetadata
  , getInfo, getShouts, getSimilar, getTags, getTopFans, getTopTags
  , love, removeTag, scrobble, search, share, unban, unlove, updateNowPlaying
  ) where

import Control.Arrow ((|||))
import Network.Lastfm

-- | Tag a track using a list of user supplied tags.
--
-- More: <http://www.lastfm.ru/api/show/track.addTags>
addTags :: Artist -> Track -> [Tag] -> APIKey -> SessionKey -> Secret -> Lastfm Response
addTags artist track tags apiKey sessionKey secret = callAPIsigned secret
  [ (#) (Method "track.addTags")
  , (#) artist
  , (#) track
  , (#) tags
  , (#) apiKey
  , (#) sessionKey
  ]

-- | Ban a track for a given user profile.
--
-- More: <http://www.lastfm.ru/api/show/track.ban>
ban :: Artist -> Track -> APIKey -> SessionKey -> Secret -> Lastfm Response
ban artist track apiKey sessionKey secret = callAPIsigned secret
  [ (#) (Method "track.ban")
  , (#) artist
  , (#) track
  , (#) apiKey
  , (#) sessionKey
  ]

-- | Get a list of Buy Links for a particular track.
--
-- More: <http://www.lastfm.ru/api/show/track.getBuylinks>
getBuyLinks :: Either (Artist, Track) Mbid -> Maybe Autocorrect -> Country -> APIKey -> Lastfm Response
getBuyLinks a autocorrect country apiKey = callAPI $
  target a ++
  [ (#) (Method "track.getBuyLinks")
  , (#) autocorrect
  , (#) country
  , (#) apiKey
  ]

-- | Use the last.fm corrections data to check whether the supplied track has a correction to a canonical track.
--
-- More: <http://www.lastfm.ru/api/show/track.getCorrection>
getCorrection :: Artist -> Track -> APIKey -> Lastfm Response
getCorrection artist track apiKey = callAPI
  [ (#) (Method "track.getCorrection")
  , (#) artist
  , (#) track
  , (#) apiKey
  ]

-- | Retrieve track metadata associated with a fingerprint id generated by the Last.fm Fingerprinter. Returns track elements, along with a 'rank' value between 0 and 1 reflecting the confidence for each match.
--
-- More: <http://www.lastfm.ru/api/show/track.getFingerprintMetadata>
getFingerprintMetadata :: Fingerprint -> APIKey -> Lastfm Response
getFingerprintMetadata fingerprint apiKey = callAPI
  [ (#) (Method "track.getFingerprintMetadata")
  , (#) fingerprint
  , (#) apiKey
  ]

-- | Get the metadata for a track on Last.fm.
--
-- More: <http://www.lastfm.ru/api/show/track.getInfo>
getInfo :: Either (Artist, Track) Mbid -> Maybe Autocorrect -> Maybe Username -> APIKey -> Lastfm Response
getInfo a autocorrect username apiKey = callAPI $
  target a ++
  [ (#) (Method "track.getInfo")
  , (#) autocorrect
  , (#) username
  , (#) apiKey
  ]

-- | Get shouts for this track. Also available as an rss feed.
--
-- More: <http://www.lastfm.ru/api/show/track.getShouts>
getShouts :: Either (Artist, Track) Mbid -> Maybe Autocorrect -> Maybe Page -> Maybe Limit -> APIKey -> Lastfm Response
getShouts a autocorrect page limit apiKey = callAPI $
  target a ++
  [ (#) (Method "track.getShouts")
  , (#) autocorrect
  , (#) page
  , (#) limit
  , (#) apiKey
  ]

-- | Get the similar tracks for this track on Last.fm, based on listening data.
--
-- More: <http://www.lastfm.ru/api/show/track.getSimilar>
getSimilar :: Either (Artist, Track) Mbid -> Maybe Autocorrect -> Maybe Limit -> APIKey -> Lastfm Response
getSimilar a autocorrect limit apiKey = callAPI $
  target a ++
  [ (#) (Method "track.getSimilar")
  , (#) autocorrect
  , (#) limit
  , (#) apiKey
  ]

-- | Get the tags applied by an individual user to a track on Last.fm.
--
-- More: <http://www.lastfm.ru/api/show/track.getTags>
getTags :: Either (Artist, Track) Mbid -> Maybe Autocorrect -> Either User (SessionKey, Secret) -> APIKey -> Lastfm Response
getTags a autocorrect b apiKey = case b of
  Left user -> callAPI $ target a ++ [(#) user] ++ args
  Right (sessionKey, secret) -> callAPIsigned secret $ target a ++ [(#) sessionKey] ++ args
  where args =
          [ (#) (Method "track.getTags")
          , (#) autocorrect
          , (#) apiKey
          ]

-- | Get the top fans for this track on Last.fm, based on listening data.
--
-- More: <http://www.lastfm.ru/api/show/track.getTopFans>
getTopFans :: Either (Artist, Track) Mbid -> Maybe Autocorrect -> APIKey -> Lastfm Response
getTopFans a autocorrect apiKey = callAPI $
  target a ++
  [ (#) (Method "track.getTopFans")
  , (#) autocorrect
  , (#) apiKey
  ]

-- | Get the top tags for this track on Last.fm, ordered by tag count.
--
-- More: <http://www.lastfm.ru/api/show/track.getTopTags>
getTopTags :: Either (Artist, Track) Mbid -> Maybe Autocorrect -> APIKey -> Lastfm Response
getTopTags a autocorrect apiKey = callAPI $
  target a ++
  [ (#) (Method "track.getTopTags")
  , (#) autocorrect
  , (#) apiKey
  ]

-- | Love a track for a user profile.
--
-- More: <http://www.lastfm.ru/api/show/track.love>
love :: Artist -> Track -> APIKey -> SessionKey -> Secret -> Lastfm Response
love artist track apiKey sessionKey secret = callAPIsigned secret
  [ (#) (Method "track.love")
  , (#) artist
  , (#) track
  , (#) apiKey
  , (#) sessionKey
  ]

-- | Remove a user's tag from a track.
--
-- More: <http://www.lastfm.ru/api/show/track.removeTag>
removeTag :: Artist -> Track -> Tag -> APIKey -> SessionKey -> Secret -> Lastfm Response
removeTag artist track tag apiKey sessionKey secret = callAPIsigned secret
  [ (#) (Method "track.removeTag")
  , (#) artist
  , (#) track
  , (#) tag
  , (#) apiKey
  , (#) sessionKey
  ]

-- | Used to add a track-play to a user's profile.
--
-- More; <http://www.lastfm.ru/api/show/track.scrobble>
scrobble :: ( Timestamp, Maybe Album, Artist, Track, Maybe AlbumArtist
           , Maybe Duration, Maybe StreamId, Maybe ChosenByUser
           , Maybe Context, Maybe TrackNumber, Maybe Mbid )
         -> APIKey
         -> SessionKey
         -> Secret
         -> Lastfm Response
scrobble (timestamp, album, artist, track, albumArtist, duration, streamId, chosenByUser, context, trackNumber, mbid) apiKey sessionKey secret = callAPIsigned secret
  [ (#) (Method "track.scrobble")
  , (#) timestamp
  , (#) artist
  , (#) track
  , (#) album
  , (#) albumArtist
  , (#) duration
  , (#) streamId
  , (#) chosenByUser
  , (#) context
  , (#) trackNumber
  , (#) mbid
  , (#) apiKey
  , (#) sessionKey
  ]

-- | Search for a track by track name. Returns track matches sorted by relevance.
--
-- More: <http://www.lastfm.ru/api/show/track.search>
search :: Track -> Maybe Page -> Maybe Limit -> Maybe Artist -> APIKey -> Lastfm Response
search track page limit artist apiKey = callAPI
  [ (#) (Method "track.search")
  , (#) track
  , (#) page
  , (#) limit
  , (#) artist
  , (#) apiKey
  ]

-- | Share a track twith one or more Last.fm users or other friends.
--
-- More: <http://www.lastfm.ru/api/show/track.share>
share :: Artist -> Track -> Recipient -> Maybe Message -> Maybe Public -> APIKey -> SessionKey -> Secret -> Lastfm Response
share artist track recipient message public apiKey sessionKey secret = callAPIsigned secret
  [ (#) (Method "track.share")
  , (#) artist
  , (#) track
  , (#) recipient
  , (#) public
  , (#) message
  , (#) apiKey
  , (#) sessionKey
  ]

-- | Unban a track for a user profile.
--
-- More: <http://www.lastfm.ru/api/show/track.unban>
unban :: Artist -> Track -> APIKey -> SessionKey -> Secret -> Lastfm Response
unban artist track apiKey sessionKey secret = callAPIsigned secret
  [ (#) (Method "track.unban")
  , (#) artist
  , (#) track
  , (#) apiKey
  , (#) sessionKey
  ]

-- | Unlove a track for a user profile.
--
-- More: <http://www.lastfm.ru/api/show/track.unlove>
unlove :: Artist -> Track -> APIKey -> SessionKey -> Secret -> Lastfm Response
unlove artist track apiKey sessionKey secret = callAPIsigned secret
  [ (#) (Method "track.unlove")
  , (#) artist
  , (#) track
  , (#) apiKey
  , (#) sessionKey
  ]


-- | Used to notify Last.fm that a user has started listening to a track. Parameter names are case sensitive.
--
-- More: <http://www.lastfm.ru/api/show/track.updateNowPlaying>
updateNowPlaying :: Artist
                 -> Track
                 -> Maybe Album
                 -> Maybe AlbumArtist
                 -> Maybe Context
                 -> Maybe TrackNumber
                 -> Maybe Mbid
                 -> Maybe Duration
                 -> APIKey
                 -> SessionKey
                 -> Secret
                 -> Lastfm Response
updateNowPlaying artist track album albumArtist context trackNumber mbid duration apiKey sessionKey secret = callAPIsigned secret
  [ (#) (Method "track.updateNowPlaying")
  , (#) artist
  , (#) track
  , (#) album
  , (#) albumArtist
  , (#) context
  , (#) trackNumber
  , (#) mbid
  , (#) duration
  , (#) apiKey
  , (#) sessionKey
  ]

target :: Either (Artist, Track) Mbid -> [(String, String)]
target = (\(artist, track) -> [(#) artist, (#) track]) ||| return . (#)