    {-# LANGUAGE OverloadedStrings #-}

    module Web.Twitter.Conduit.Parameters
           ( UserParam(..)
           , UserListParam(..)
           , ListParam(..)
           , MediaData(..)
           , mkUserParam
           , mkUserListParam
           , mkListParam
           ) where

    import qualified Data.Text as T
    import Network.HTTP.Client (RequestBody)
    import Web.Twitter.Conduit.Request.Internal (APIQuery, PV(..))
    import Web.Twitter.Types

    -- $setup
    -- >>> import Web.Twitter.Conduit.Request.Internal

    data UserParam = UserIdParam UserId | ScreenNameParam String
                   deriving (Int -> UserParam -> ShowS
    [UserParam] -> ShowS
    UserParam -> String
    (Int -> UserParam -> ShowS)
    -> (UserParam -> String)
    -> ([UserParam] -> ShowS)
    -> Show UserParam
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    showList :: [UserParam] -> ShowS
    $cshowList :: [UserParam] -> ShowS
    show :: UserParam -> String
    $cshow :: UserParam -> String
    showsPrec :: Int -> UserParam -> ShowS
    $cshowsPrec :: Int -> UserParam -> ShowS
    Show, UserParam -> UserParam -> Bool
    (UserParam -> UserParam -> Bool)
    -> (UserParam -> UserParam -> Bool) -> Eq UserParam
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    /= :: UserParam -> UserParam -> Bool
    $c/= :: UserParam -> UserParam -> Bool
    == :: UserParam -> UserParam -> Bool
    $c== :: UserParam -> UserParam -> Bool
    Eq)
    data UserListParam = UserIdListParam [UserId] | ScreenNameListParam [String]
                       deriving (Int -> UserListParam -> ShowS
    [UserListParam] -> ShowS
    UserListParam -> String
    (Int -> UserListParam -> ShowS)
    -> (UserListParam -> String)
    -> ([UserListParam] -> ShowS)
    -> Show UserListParam
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    showList :: [UserListParam] -> ShowS
    $cshowList :: [UserListParam] -> ShowS
    show :: UserListParam -> String
    $cshow :: UserListParam -> String
    showsPrec :: Int -> UserListParam -> ShowS
    $cshowsPrec :: Int -> UserListParam -> ShowS
    Show, UserListParam -> UserListParam -> Bool
    (UserListParam -> UserListParam -> Bool)
    -> (UserListParam -> UserListParam -> Bool) -> Eq UserListParam
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    /= :: UserListParam -> UserListParam -> Bool
    $c/= :: UserListParam -> UserListParam -> Bool
    == :: UserListParam -> UserListParam -> Bool
    $c== :: UserListParam -> UserListParam -> Bool
    Eq)
    data ListParam = ListIdParam Integer | ListNameParam String
                   deriving (Int -> ListParam -> ShowS
    [ListParam] -> ShowS
    ListParam -> String
    (Int -> ListParam -> ShowS)
    -> (ListParam -> String)
    -> ([ListParam] -> ShowS)
    -> Show ListParam
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    showList :: [ListParam] -> ShowS
    $cshowList :: [ListParam] -> ShowS
    show :: ListParam -> String
    $cshow :: ListParam -> String
    showsPrec :: Int -> ListParam -> ShowS
    $cshowsPrec :: Int -> ListParam -> ShowS
    Show, ListParam -> ListParam -> Bool
    (ListParam -> ListParam -> Bool)
    -> (ListParam -> ListParam -> Bool) -> Eq ListParam
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    /= :: ListParam -> ListParam -> Bool
    $c/= :: ListParam -> ListParam -> Bool
    == :: ListParam -> ListParam -> Bool
    $c== :: ListParam -> ListParam -> Bool
    Eq)
    data MediaData = MediaFromFile FilePath
                   | MediaRequestBody FilePath RequestBody

    -- | converts 'UserParam' to 'HT.SimpleQuery'.
    --
    -- >>> makeSimpleQuery . mkUserParam $ UserIdParam 123456
    -- [("user_id","123456")]
    -- >>> makeSimpleQuery . mkUserParam $ ScreenNameParam "thimura"
    -- [("screen_name","thimura")]
    mkUserParam :: UserParam -> APIQuery
    mkUserParam :: UserParam -> APIQuery
    mkUserParam (UserIdParam uid :: UserId
    uid) =  [("user_id", UserId -> PV
    PVInteger UserId
    uid)]
    mkUserParam (ScreenNameParam sn :: String
    sn) = [("screen_name", Text -> PV
    PVString (Text -> PV) -> (String -> Text) -> String -> PV
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> Text
    T.pack (String -> PV) -> String -> PV
    forall a b. (a -> b) -> a -> b
    $ String
    sn)]

    -- | converts 'UserListParam' to 'HT.SimpleQuery'.
    --
    -- >>> makeSimpleQuery . mkUserListParam $ UserIdListParam [123456]
    -- [("user_id","123456")]
    -- >>> makeSimpleQuery . mkUserListParam $ UserIdListParam [123456, 654321]
    -- [("user_id","123456,654321")]
    -- >>> makeSimpleQuery . mkUserListParam $ ScreenNameListParam ["thimura", "NikaidouShinku"]
    -- [("screen_name","thimura,NikaidouShinku")]
    mkUserListParam :: UserListParam -> APIQuery
    mkUserListParam :: UserListParam -> APIQuery
    mkUserListParam (UserIdListParam uids :: [UserId]
    uids) =  [("user_id", [UserId] -> PV
    PVIntegerArray [UserId]
    uids)]
    mkUserListParam (ScreenNameListParam sns :: [String]
    sns) = [("screen_name", [Text] -> PV
    PVStringArray ((String -> Text) -> [String] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map String -> Text
    T.pack [String]
    sns))]

    -- | converts 'ListParam' to 'HT.SimpleQuery'.
    --
    -- >>> makeSimpleQuery . mkListParam $ ListIdParam 123123
    -- [("list_id","123123")]
    -- >>> makeSimpleQuery . mkListParam $ ListNameParam "thimura/haskell"
    -- [("slug","haskell"),("owner_screen_name","thimura")]
    mkListParam :: ListParam -> APIQuery
    mkListParam :: ListParam -> APIQuery
    mkListParam (ListIdParam lid :: UserId
    lid) =  [("list_id", UserId -> PV
    PVInteger UserId
    lid)]
    mkListParam (ListNameParam listname :: String
    listname) =
        [("slug", Text -> PV
    PVString (String -> Text
    T.pack String
    lstName)),
         ("owner_screen_name", Text -> PV
    PVString (String -> Text
    T.pack String
    screenName))]
      where
        (screenName :: String
    screenName, ln :: String
    ln) = (Char -> Bool) -> String -> (String, String)
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /= '/') String
    listname
        lstName :: String
    lstName = Int -> ShowS
    forall a. Int -> [a] -> [a]
    drop 1 String
    ln