{-# LANGUAGE OverloadedStrings #-}

module WinShop.Similar (
  similarItems
) where

-- /* /gp/windowshop/ajax?func=get%2Dsimilar%2Ddata&rankType=getPurchaseSimilarities&asin=143770719X&count=12 */

import           WinShop.Api.BaseTypes (Asin (..))
import           WinShop.Invoke        (QParam, doGet, mkWSRequest, wsFunc)
import           WinShop.Query         (RankType (SimilarPurchased), WSQuery,
                                        qAsin, qIntVal, qParam, qRankType)

import qualified Data.Aeson            as A
import           Prelude               hiding (asin)

baseParams :: [QParam]
baseParams = [wsFunc "get-similar-data",
              qRankType SimilarPurchased]

mkQuery :: Asin -> Int -> WSQuery
mkQuery asin cnt =
  query
  where
    pAsin = qAsin asin
    pCnt  = qParam "count" $ qIntVal cnt
    query = pAsin : pCnt : baseParams

similarItems :: Asin -> Int -> IO (Maybe A.Object)
similarItems asin cnt = do
  rsp <- doGet $ mkWSRequest (mkQuery asin cnt)
  case rsp of
    Just (A.Object o) -> return $ Just o
    _ -> return Nothing

-- test :: IO (Maybe A.Object)
-- test = similarItems (Asin "143770719X") 12
