module Web.Last.Tasteometer where

import Control.Applicative
import Control.Arrow

import Web.Last.Types
import Web.Last.Request
import qualified Web.Last.Parsing as P
import Text.JSON.Combinators

import Data.List (intercalate)

data TasteArg =
      ArgUser String       -- username
    | ArgArtists [String]  -- artist names
    | ArgMySpace String    -- myspace url (of user?)

formatArgs :: Int -> TasteArg -> [(String,String)]
formatArgs i (ArgUser userName) = [("type" ++ show i, "user"),("value" ++ show i, userName)]
formatArgs i (ArgArtists artistNames) = [("type" ++ show i, "artists"),("value" ++ show i, intercalate "," artistNames)]
formatArgs i (ArgMySpace url) = [("type" ++ show i, "myspace"),("value" ++ show i, url)]

-- String is correlation between args
compare :: TasteArg -> TasteArg -> Int -> Last (String,[(Artist,[Image])])
compare arg1 arg2 limit = anonRequest "tasteometer.compare" (formatArgs 1 arg1 ++ formatArgs 2 arg2 ++ [("limit",show limit)]) [] pa
    where pa = objOf $ inObj "comparison" $ inObj "result" $ (ll "score" strJS) &&& sharedArtists
          sharedArtists = inObj "artists" $ llArr "artist" (P.artist &&& (objOf $ llArr "image" P.image))