module TD.Data.PollVoters
  (PollVoters(..)) 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.PollVoter as PollVoter

data PollVoters
  = PollVoters -- ^ Represents a list of poll voters
    { PollVoters -> Maybe Int
total_count :: Maybe Int                   -- ^ Approximate total number of poll voters found
    , PollVoters -> Maybe [PollVoter]
voters      :: Maybe [PollVoter.PollVoter] -- ^ List of poll voters
    }
  deriving (PollVoters -> PollVoters -> Bool
(PollVoters -> PollVoters -> Bool)
-> (PollVoters -> PollVoters -> Bool) -> Eq PollVoters
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PollVoters -> PollVoters -> Bool
== :: PollVoters -> PollVoters -> Bool
$c/= :: PollVoters -> PollVoters -> Bool
/= :: PollVoters -> PollVoters -> Bool
Eq, Int -> PollVoters -> ShowS
[PollVoters] -> ShowS
PollVoters -> String
(Int -> PollVoters -> ShowS)
-> (PollVoters -> String)
-> ([PollVoters] -> ShowS)
-> Show PollVoters
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PollVoters -> ShowS
showsPrec :: Int -> PollVoters -> ShowS
$cshow :: PollVoters -> String
show :: PollVoters -> String
$cshowList :: [PollVoters] -> ShowS
showList :: [PollVoters] -> ShowS
Show)

instance I.ShortShow PollVoters where
  shortShow :: PollVoters -> String
shortShow PollVoters
    { total_count :: PollVoters -> Maybe Int
total_count = Maybe Int
total_count_
    , voters :: PollVoters -> Maybe [PollVoter]
voters      = Maybe [PollVoter]
voters_
    }
      = String
"PollVoters"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"total_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
total_count_
        , String
"voters"      String -> Maybe [PollVoter] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [PollVoter]
voters_
        ]

instance AT.FromJSON PollVoters where
  parseJSON :: Value -> Parser PollVoters
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
"pollVoters" -> Value -> Parser PollVoters
parsePollVoters Value
v
      String
_            -> Parser PollVoters
forall a. Monoid a => a
mempty
    
    where
      parsePollVoters :: A.Value -> AT.Parser PollVoters
      parsePollVoters :: Value -> Parser PollVoters
parsePollVoters = String
-> (Object -> Parser PollVoters) -> Value -> Parser PollVoters
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PollVoters" ((Object -> Parser PollVoters) -> Value -> Parser PollVoters)
-> (Object -> Parser PollVoters) -> Value -> Parser PollVoters
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
total_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"total_count"
        Maybe [PollVoter]
voters_      <- Object
o Object -> Key -> Parser (Maybe [PollVoter])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"voters"
        PollVoters -> Parser PollVoters
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PollVoters -> Parser PollVoters)
-> PollVoters -> Parser PollVoters
forall a b. (a -> b) -> a -> b
$ PollVoters
          { total_count :: Maybe Int
total_count = Maybe Int
total_count_
          , voters :: Maybe [PollVoter]
voters      = Maybe [PollVoter]
voters_
          }
  parseJSON Value
_ = Parser PollVoters
forall a. Monoid a => a
mempty