module TD.Data.PollVoter
  (PollVoter(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified TD.Data.MessageSender as MessageSender

data PollVoter
  = PollVoter -- ^ Represents a poll voter
    { PollVoter -> Maybe MessageSender
voter_id :: Maybe MessageSender.MessageSender -- ^ The voter identifier
    , PollVoter -> Maybe Int
date     :: Maybe Int                         -- ^ Point in time (Unix timestamp) when the vote was added
    }
  deriving (PollVoter -> PollVoter -> Bool
(PollVoter -> PollVoter -> Bool)
-> (PollVoter -> PollVoter -> Bool) -> Eq PollVoter
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PollVoter -> PollVoter -> Bool
== :: PollVoter -> PollVoter -> Bool
$c/= :: PollVoter -> PollVoter -> Bool
/= :: PollVoter -> PollVoter -> Bool
Eq, Int -> PollVoter -> ShowS
[PollVoter] -> ShowS
PollVoter -> String
(Int -> PollVoter -> ShowS)
-> (PollVoter -> String)
-> ([PollVoter] -> ShowS)
-> Show PollVoter
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PollVoter -> ShowS
showsPrec :: Int -> PollVoter -> ShowS
$cshow :: PollVoter -> String
show :: PollVoter -> String
$cshowList :: [PollVoter] -> ShowS
showList :: [PollVoter] -> ShowS
Show)

instance I.ShortShow PollVoter where
  shortShow :: PollVoter -> String
shortShow PollVoter
    { voter_id :: PollVoter -> Maybe MessageSender
voter_id = Maybe MessageSender
voter_id_
    , date :: PollVoter -> Maybe Int
date     = Maybe Int
date_
    }
      = String
"PollVoter"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"voter_id" String -> Maybe MessageSender -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe MessageSender
voter_id_
        , String
"date"     String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
date_
        ]

instance AT.FromJSON PollVoter where
  parseJSON :: Value -> Parser PollVoter
parseJSON v :: Value
v@(AT.Object Object
obj) = do
    String
t <- Object
obj Object -> Key -> Parser String
forall a. FromJSON a => Object -> Key -> Parser a
A..: Key
"@type" :: AT.Parser String

    case String
t of
      String
"pollVoter" -> Value -> Parser PollVoter
parsePollVoter Value
v
      String
_           -> Parser PollVoter
forall a. Monoid a => a
mempty
    
    where
      parsePollVoter :: A.Value -> AT.Parser PollVoter
      parsePollVoter :: Value -> Parser PollVoter
parsePollVoter = String -> (Object -> Parser PollVoter) -> Value -> Parser PollVoter
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PollVoter" ((Object -> Parser PollVoter) -> Value -> Parser PollVoter)
-> (Object -> Parser PollVoter) -> Value -> Parser PollVoter
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe MessageSender
voter_id_ <- Object
o Object -> Key -> Parser (Maybe MessageSender)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"voter_id"
        Maybe Int
date_     <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"date"
        PollVoter -> Parser PollVoter
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PollVoter -> Parser PollVoter) -> PollVoter -> Parser PollVoter
forall a b. (a -> b) -> a -> b
$ PollVoter
          { voter_id :: Maybe MessageSender
voter_id = Maybe MessageSender
voter_id_
          , date :: Maybe Int
date     = Maybe Int
date_
          }
  parseJSON Value
_ = Parser PollVoter
forall a. Monoid a => a
mempty