{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}

module Ebay.Category
    ( getCategories

    , module Ebay.Types.Category
    ) where

import           Control.Monad.Reader
import           Data.XML.Pickle

import           Ebay
import           Ebay.Types.Authentication
import           Ebay.Types.Category

getCategories :: AuthToken -> Maybe Int -> Maybe CategoryID ->
                    EbayT (StandardOutput, CategoryList)
getCategories tok ll mid = do
        (EbayConf{..}) <- ask
        let level = case ll of
                        Nothing -> Just 1
                        Just n  -> Just n
            input = (fromAuthToken tok, level, mid, ebaySiteId, DTLReturnAll)
        ebayPost "GetCategories" xpInput xpCategoryList input
    where xpInput = xp5Tuple
            xpWithAuth
            (xpOption $ xpElemNodes "{urn:ebay:apis:eBLBaseComponents}LevelLimit" $
                xpContent xpPrim)
            (xpOption $ xpElemNodes "{urn:ebay:apis:eBLBaseComponents}CategoryParent" $
                xpContent xpCategoryId)
            (xpElemNodes "{urn:ebay:apis:eBLBaseComponents}CategorySiteID" $
                xpContent xpSiteId)
            (xpElemNodes "{urn:ebay:apis:eBLBaseComponents}DetailLevel" $
                xpContent xpDetailLevelCode)
