module Reddit.Types.Captcha where

import Reddit.Types.Reddit

import Control.Applicative
import Data.Aeson
import Data.Monoid
import Data.Text (Text)
import Network.API.Builder
import Prelude

newtype CaptchaID = CaptchaID Text
  deriving (ReadPrec [CaptchaID]
ReadPrec CaptchaID
Int -> ReadS CaptchaID
ReadS [CaptchaID]
(Int -> ReadS CaptchaID)
-> ReadS [CaptchaID]
-> ReadPrec CaptchaID
-> ReadPrec [CaptchaID]
-> Read CaptchaID
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CaptchaID]
$creadListPrec :: ReadPrec [CaptchaID]
readPrec :: ReadPrec CaptchaID
$creadPrec :: ReadPrec CaptchaID
readList :: ReadS [CaptchaID]
$creadList :: ReadS [CaptchaID]
readsPrec :: Int -> ReadS CaptchaID
$creadsPrec :: Int -> ReadS CaptchaID
Read, Int -> CaptchaID -> ShowS
[CaptchaID] -> ShowS
CaptchaID -> String
(Int -> CaptchaID -> ShowS)
-> (CaptchaID -> String)
-> ([CaptchaID] -> ShowS)
-> Show CaptchaID
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CaptchaID] -> ShowS
$cshowList :: [CaptchaID] -> ShowS
show :: CaptchaID -> String
$cshow :: CaptchaID -> String
showsPrec :: Int -> CaptchaID -> ShowS
$cshowsPrec :: Int -> CaptchaID -> ShowS
Show, CaptchaID -> CaptchaID -> Bool
(CaptchaID -> CaptchaID -> Bool)
-> (CaptchaID -> CaptchaID -> Bool) -> Eq CaptchaID
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CaptchaID -> CaptchaID -> Bool
$c/= :: CaptchaID -> CaptchaID -> Bool
== :: CaptchaID -> CaptchaID -> Bool
$c== :: CaptchaID -> CaptchaID -> Bool
Eq, Eq CaptchaID
Eq CaptchaID
-> (CaptchaID -> CaptchaID -> Ordering)
-> (CaptchaID -> CaptchaID -> Bool)
-> (CaptchaID -> CaptchaID -> Bool)
-> (CaptchaID -> CaptchaID -> Bool)
-> (CaptchaID -> CaptchaID -> Bool)
-> (CaptchaID -> CaptchaID -> CaptchaID)
-> (CaptchaID -> CaptchaID -> CaptchaID)
-> Ord CaptchaID
CaptchaID -> CaptchaID -> Bool
CaptchaID -> CaptchaID -> Ordering
CaptchaID -> CaptchaID -> CaptchaID
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CaptchaID -> CaptchaID -> CaptchaID
$cmin :: CaptchaID -> CaptchaID -> CaptchaID
max :: CaptchaID -> CaptchaID -> CaptchaID
$cmax :: CaptchaID -> CaptchaID -> CaptchaID
>= :: CaptchaID -> CaptchaID -> Bool
$c>= :: CaptchaID -> CaptchaID -> Bool
> :: CaptchaID -> CaptchaID -> Bool
$c> :: CaptchaID -> CaptchaID -> Bool
<= :: CaptchaID -> CaptchaID -> Bool
$c<= :: CaptchaID -> CaptchaID -> Bool
< :: CaptchaID -> CaptchaID -> Bool
$c< :: CaptchaID -> CaptchaID -> Bool
compare :: CaptchaID -> CaptchaID -> Ordering
$ccompare :: CaptchaID -> CaptchaID -> Ordering
$cp1Ord :: Eq CaptchaID
Ord)

instance FromJSON CaptchaID where
  parseJSON :: Value -> Parser CaptchaID
parseJSON Value
j = Text -> CaptchaID
CaptchaID (Text -> CaptchaID) -> Parser Text -> Parser CaptchaID
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser Text
forall a. FromJSON a => Value -> Parser a
parseJSON Value
j

instance FromJSON (POSTWrapped CaptchaID) where
  parseJSON :: Value -> Parser (POSTWrapped CaptchaID)
parseJSON (Object Object
o) =
    CaptchaID -> POSTWrapped CaptchaID
forall a. a -> POSTWrapped a
POSTWrapped (CaptchaID -> POSTWrapped CaptchaID)
-> Parser CaptchaID -> Parser (POSTWrapped CaptchaID)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Object
o Object -> Key -> Parser Object
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"json") Parser Object -> (Object -> Parser Object) -> Parser Object
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Object -> Key -> Parser Object
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"data") Parser Object -> (Object -> Parser CaptchaID) -> Parser CaptchaID
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Object -> Key -> Parser CaptchaID
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"iden"))
  parseJSON Value
_ = Parser (POSTWrapped CaptchaID)
forall a. Monoid a => a
mempty

withCaptcha :: Route -> (CaptchaID, Text) -> Route
withCaptcha :: Route -> (CaptchaID, Text) -> Route
withCaptcha (Route [Text]
pieces [URLParam]
params Method
meth) (CaptchaID Text
i, Text
c) =
  [Text] -> [URLParam] -> Method -> Route
Route [Text]
pieces (URLParam
iden URLParam -> [URLParam] -> [URLParam]
forall a. a -> [a] -> [a]
: URLParam
captcha URLParam -> [URLParam] -> [URLParam]
forall a. a -> [a] -> [a]
: [URLParam]
params) Method
meth
  where iden :: URLParam
iden = (Text
"iden" :: Text) Text -> Text -> URLParam
forall a. ToQuery a => Text -> a -> URLParam
=. Text
i
        captcha :: URLParam
captcha = (Text
"captcha" :: Text) Text -> Text -> URLParam
forall a. ToQuery a => Text -> a -> URLParam
=. Text
c