module TD.Data.Poll
  (Poll(..)) 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.FormattedText as FormattedText
import {-# SOURCE #-} qualified TD.Data.PollOption as PollOption
import qualified TD.Data.MessageSender as MessageSender
import {-# SOURCE #-} qualified TD.Data.PollType as PollType

data Poll
  = Poll -- ^ Describes a poll
    { Poll -> Maybe Int
_id                     :: Maybe Int                           -- ^ Unique poll identifier
    , Poll -> Maybe FormattedText
question                :: Maybe FormattedText.FormattedText   -- ^ Poll question; 1-300 characters; may contain only custom emoji entities
    , Poll -> Maybe [PollOption]
options                 :: Maybe [PollOption.PollOption]       -- ^ List of poll answer options
    , Poll -> Maybe Int
total_voter_count       :: Maybe Int                           -- ^ Total number of voters, participating in the poll
    , Poll -> Maybe [MessageSender]
recent_voter_ids        :: Maybe [MessageSender.MessageSender] -- ^ Identifiers of recent voters, if the poll is non-anonymous and poll results are available
    , Poll -> Maybe Bool
can_get_voters          :: Maybe Bool                          -- ^ True, if the current user can get voters in the poll
    , Poll -> Maybe Bool
is_anonymous            :: Maybe Bool                          -- ^ True, if the poll is anonymous
    , Poll -> Maybe Bool
allows_multiple_answers :: Maybe Bool                          -- ^ True, if multiple answer options can be chosen simultaneously
    , Poll -> Maybe Bool
allows_revoting         :: Maybe Bool                          -- ^ True, if the poll can be answered multiple times
    , Poll -> Maybe [Int]
option_order            :: Maybe [Int]                         -- ^ The list of 0-based poll identifiers in which the options of the poll must be shown; empty if the order of options must not be changed
    , Poll -> Maybe PollType
_type                   :: Maybe PollType.PollType             -- ^ Type of the poll
    , Poll -> Maybe Int
open_period             :: Maybe Int                           -- ^ Amount of time the poll will be active after creation, in seconds
    , Poll -> Maybe Int
close_date              :: Maybe Int                           -- ^ Point in time (Unix timestamp) when the poll will automatically be closed
    , Poll -> Maybe Bool
is_closed               :: Maybe Bool                          -- ^ True, if the poll is closed
    }
  deriving (Poll -> Poll -> Bool
(Poll -> Poll -> Bool) -> (Poll -> Poll -> Bool) -> Eq Poll
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Poll -> Poll -> Bool
== :: Poll -> Poll -> Bool
$c/= :: Poll -> Poll -> Bool
/= :: Poll -> Poll -> Bool
Eq, Int -> Poll -> ShowS
[Poll] -> ShowS
Poll -> String
(Int -> Poll -> ShowS)
-> (Poll -> String) -> ([Poll] -> ShowS) -> Show Poll
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Poll -> ShowS
showsPrec :: Int -> Poll -> ShowS
$cshow :: Poll -> String
show :: Poll -> String
$cshowList :: [Poll] -> ShowS
showList :: [Poll] -> ShowS
Show)

instance I.ShortShow Poll where
  shortShow :: Poll -> String
shortShow Poll
    { _id :: Poll -> Maybe Int
_id                     = Maybe Int
_id_
    , question :: Poll -> Maybe FormattedText
question                = Maybe FormattedText
question_
    , options :: Poll -> Maybe [PollOption]
options                 = Maybe [PollOption]
options_
    , total_voter_count :: Poll -> Maybe Int
total_voter_count       = Maybe Int
total_voter_count_
    , recent_voter_ids :: Poll -> Maybe [MessageSender]
recent_voter_ids        = Maybe [MessageSender]
recent_voter_ids_
    , can_get_voters :: Poll -> Maybe Bool
can_get_voters          = Maybe Bool
can_get_voters_
    , is_anonymous :: Poll -> Maybe Bool
is_anonymous            = Maybe Bool
is_anonymous_
    , allows_multiple_answers :: Poll -> Maybe Bool
allows_multiple_answers = Maybe Bool
allows_multiple_answers_
    , allows_revoting :: Poll -> Maybe Bool
allows_revoting         = Maybe Bool
allows_revoting_
    , option_order :: Poll -> Maybe [Int]
option_order            = Maybe [Int]
option_order_
    , _type :: Poll -> Maybe PollType
_type                   = Maybe PollType
_type_
    , open_period :: Poll -> Maybe Int
open_period             = Maybe Int
open_period_
    , close_date :: Poll -> Maybe Int
close_date              = Maybe Int
close_date_
    , is_closed :: Poll -> Maybe Bool
is_closed               = Maybe Bool
is_closed_
    }
      = String
"Poll"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_id"                     String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
_id_
        , String
"question"                String -> Maybe FormattedText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe FormattedText
question_
        , String
"options"                 String -> Maybe [PollOption] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [PollOption]
options_
        , String
"total_voter_count"       String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
total_voter_count_
        , String
"recent_voter_ids"        String -> Maybe [MessageSender] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [MessageSender]
recent_voter_ids_
        , String
"can_get_voters"          String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
can_get_voters_
        , String
"is_anonymous"            String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_anonymous_
        , String
"allows_multiple_answers" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
allows_multiple_answers_
        , String
"allows_revoting"         String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
allows_revoting_
        , String
"option_order"            String -> Maybe [Int] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Int]
option_order_
        , String
"_type"                   String -> Maybe PollType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe PollType
_type_
        , String
"open_period"             String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
open_period_
        , String
"close_date"              String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
close_date_
        , String
"is_closed"               String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_closed_
        ]

instance AT.FromJSON Poll where
  parseJSON :: Value -> Parser Poll
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
"poll" -> Value -> Parser Poll
parsePoll Value
v
      String
_      -> Parser Poll
forall a. Monoid a => a
mempty
    
    where
      parsePoll :: A.Value -> AT.Parser Poll
      parsePoll :: Value -> Parser Poll
parsePoll = String -> (Object -> Parser Poll) -> Value -> Parser Poll
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Poll" ((Object -> Parser Poll) -> Value -> Parser Poll)
-> (Object -> Parser Poll) -> Value -> Parser Poll
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_id_                     <- (String -> Int) -> Maybe String -> Maybe Int
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
I.readInt64 (Maybe String -> Maybe Int)
-> Parser (Maybe String) -> Parser (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        Maybe FormattedText
question_                <- Object
o Object -> Key -> Parser (Maybe FormattedText)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"question"
        Maybe [PollOption]
options_                 <- Object
o Object -> Key -> Parser (Maybe [PollOption])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"options"
        Maybe Int
total_voter_count_       <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"total_voter_count"
        Maybe [MessageSender]
recent_voter_ids_        <- Object
o Object -> Key -> Parser (Maybe [MessageSender])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"recent_voter_ids"
        Maybe Bool
can_get_voters_          <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"can_get_voters"
        Maybe Bool
is_anonymous_            <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"is_anonymous"
        Maybe Bool
allows_multiple_answers_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"allows_multiple_answers"
        Maybe Bool
allows_revoting_         <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"allows_revoting"
        Maybe [Int]
option_order_            <- Object
o Object -> Key -> Parser (Maybe [Int])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"option_order"
        Maybe PollType
_type_                   <- Object
o Object -> Key -> Parser (Maybe PollType)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"type"
        Maybe Int
open_period_             <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"open_period"
        Maybe Int
close_date_              <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"close_date"
        Maybe Bool
is_closed_               <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"is_closed"
        Poll -> Parser Poll
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Poll -> Parser Poll) -> Poll -> Parser Poll
forall a b. (a -> b) -> a -> b
$ Poll
          { _id :: Maybe Int
_id                     = Maybe Int
_id_
          , question :: Maybe FormattedText
question                = Maybe FormattedText
question_
          , options :: Maybe [PollOption]
options                 = Maybe [PollOption]
options_
          , total_voter_count :: Maybe Int
total_voter_count       = Maybe Int
total_voter_count_
          , recent_voter_ids :: Maybe [MessageSender]
recent_voter_ids        = Maybe [MessageSender]
recent_voter_ids_
          , can_get_voters :: Maybe Bool
can_get_voters          = Maybe Bool
can_get_voters_
          , is_anonymous :: Maybe Bool
is_anonymous            = Maybe Bool
is_anonymous_
          , allows_multiple_answers :: Maybe Bool
allows_multiple_answers = Maybe Bool
allows_multiple_answers_
          , allows_revoting :: Maybe Bool
allows_revoting         = Maybe Bool
allows_revoting_
          , option_order :: Maybe [Int]
option_order            = Maybe [Int]
option_order_
          , _type :: Maybe PollType
_type                   = Maybe PollType
_type_
          , open_period :: Maybe Int
open_period             = Maybe Int
open_period_
          , close_date :: Maybe Int
close_date              = Maybe Int
close_date_
          , is_closed :: Maybe Bool
is_closed               = Maybe Bool
is_closed_
          }
  parseJSON Value
_ = Parser Poll
forall a. Monoid a => a
mempty