{-# LANGUAGE OverloadedStrings #-}

{- |
Module      : Network.MPD.Applicative.Stickers
Copyright   : (c) Joachim Fasting 2012
License     : MIT

Maintainer  : joachifm@fastmail.fm
Stability   : stable
Portability : unportable

Stickers.
-}

module Network.MPD.Applicative.Stickers
    ( stickerGet
    , stickerSet
    , stickerDelete
    , stickerList
    , stickerFind
    ) where

import           Network.MPD.Applicative.Internal
import           Network.MPD.Applicative.Util
import           Network.MPD.Commands.Arg hiding (Command)
import           Network.MPD.Commands.Types
import           Network.MPD.Util

import qualified Data.ByteString.UTF8 as UTF8

-- | Read sticker value for the object specified.
stickerGet :: ObjectType -> String -> String -> Command [String]
stickerGet :: ObjectType -> String -> String -> Command [String]
stickerGet ObjectType
typ String
uri String
name = forall a. Parser a -> [String] -> Command a
Command Parser [String]
p [String]
c
    where
        p :: Parser [String]
        p :: Parser [String]
p = forall a b. (a -> b) -> [a] -> [b]
map ByteString -> String
UTF8.toString forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> [ByteString]
takeValues forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [ByteString]
getResponse

        c :: [String]
c = [Command
"sticker get" forall a. MPDArg a => Command -> a -> String
<@> ObjectType
typ forall a b. (MPDArg a, MPDArg b) => a -> b -> Args
<++> String
uri forall a b. (MPDArg a, MPDArg b) => a -> b -> Args
<++> String
name]

-- | Add sticker value to the object. Will overwrite existing stickers
-- with the same name.
stickerSet :: ObjectType -> String -> String -> String -> Command ()
stickerSet :: ObjectType -> String -> String -> String -> Command ()
stickerSet ObjectType
typ String
uri String
name String
value = forall a. Parser a -> [String] -> Command a
Command Parser ()
emptyResponse [String]
c
    where
        c :: [String]
c = [Command
"sticker set" forall a. MPDArg a => Command -> a -> String
<@> ObjectType
typ forall a b. (MPDArg a, MPDArg b) => a -> b -> Args
<++> String
uri forall a b. (MPDArg a, MPDArg b) => a -> b -> Args
<++> String
name forall a b. (MPDArg a, MPDArg b) => a -> b -> Args
<++> String
value]

-- | Delete a sticker value from the object. If no sticker name is
-- given, all sticker values attached to the object are deleted.
stickerDelete :: ObjectType -> String -> String -> Command ()
stickerDelete :: ObjectType -> String -> String -> Command ()
stickerDelete ObjectType
typ String
uri String
name = forall a. Parser a -> [String] -> Command a
Command Parser ()
emptyResponse [String]
c
    where
        c :: [String]
c = [Command
"sticker delete" forall a. MPDArg a => Command -> a -> String
<@> ObjectType
typ forall a b. (MPDArg a, MPDArg b) => a -> b -> Args
<++> String
uri forall a b. (MPDArg a, MPDArg b) => a -> b -> Args
<++> String
name]

-- | List stickers for the object.
stickerList :: ObjectType -> String -> Command [(String, String)]
stickerList :: ObjectType -> String -> Command [(String, String)]
stickerList ObjectType
typ String
uri = forall a. Parser a -> [String] -> Command a
Command Parser [(String, String)]
p [String]
c
    where
        p :: Parser [(String, String)]
p = forall a b. (a -> b) -> [a] -> [b]
map (ByteString, ByteString) -> (String, String)
decodePair forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> [(ByteString, ByteString)]
toAssocList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [ByteString]
getResponse

        c :: [String]
c = [Command
"sticker list" forall a. MPDArg a => Command -> a -> String
<@> ObjectType
typ forall a b. (MPDArg a, MPDArg b) => a -> b -> Args
<++> String
uri]

-- | Search the sticker database for stickers with the specified name,
-- below the specified directory.
stickerFind :: ObjectType -> String -> String -> Command [(String, String)]
stickerFind :: ObjectType -> String -> String -> Command [(String, String)]
stickerFind ObjectType
typ String
uri String
name = forall a. Parser a -> [String] -> Command a
Command Parser [(String, String)]
p [String]
c
    where
        p :: Parser [(String, String)]
p = forall a b. (a -> b) -> [a] -> [b]
map (ByteString, ByteString) -> (String, String)
decodePair forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> [(ByteString, ByteString)]
toAssocList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [ByteString]
getResponse

        c :: [String]
c = [Command
"sticker find" forall a. MPDArg a => Command -> a -> String
<@> ObjectType
typ forall a b. (MPDArg a, MPDArg b) => a -> b -> Args
<++> String
uri forall a b. (MPDArg a, MPDArg b) => a -> b -> Args
<++> String
name]