module TD.Data.PollOption
  (PollOption(..)) 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

data PollOption
  = PollOption -- ^ Describes one answer option of a poll
    { PollOption -> Maybe FormattedText
text            :: Maybe FormattedText.FormattedText -- ^ Option text; 1-100 characters. Only custom emoji entities are allowed
    , PollOption -> Maybe Int
voter_count     :: Maybe Int                         -- ^ Number of voters for this option, available only for closed or voted polls
    , PollOption -> Maybe Int
vote_percentage :: Maybe Int                         -- ^ The percentage of votes for this option; 0-100
    , PollOption -> Maybe Bool
is_chosen       :: Maybe Bool                        -- ^ True, if the option was chosen by the user
    , PollOption -> Maybe Bool
is_being_chosen :: Maybe Bool                        -- ^ True, if the option is being chosen by a pending setPollAnswer request
    }
  deriving (PollOption -> PollOption -> Bool
(PollOption -> PollOption -> Bool)
-> (PollOption -> PollOption -> Bool) -> Eq PollOption
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PollOption -> PollOption -> Bool
== :: PollOption -> PollOption -> Bool
$c/= :: PollOption -> PollOption -> Bool
/= :: PollOption -> PollOption -> Bool
Eq, Int -> PollOption -> ShowS
[PollOption] -> ShowS
PollOption -> String
(Int -> PollOption -> ShowS)
-> (PollOption -> String)
-> ([PollOption] -> ShowS)
-> Show PollOption
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PollOption -> ShowS
showsPrec :: Int -> PollOption -> ShowS
$cshow :: PollOption -> String
show :: PollOption -> String
$cshowList :: [PollOption] -> ShowS
showList :: [PollOption] -> ShowS
Show)

instance I.ShortShow PollOption where
  shortShow :: PollOption -> String
shortShow PollOption
    { text :: PollOption -> Maybe FormattedText
text            = Maybe FormattedText
text_
    , voter_count :: PollOption -> Maybe Int
voter_count     = Maybe Int
voter_count_
    , vote_percentage :: PollOption -> Maybe Int
vote_percentage = Maybe Int
vote_percentage_
    , is_chosen :: PollOption -> Maybe Bool
is_chosen       = Maybe Bool
is_chosen_
    , is_being_chosen :: PollOption -> Maybe Bool
is_being_chosen = Maybe Bool
is_being_chosen_
    }
      = String
"PollOption"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"text"            String -> Maybe FormattedText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe FormattedText
text_
        , String
"voter_count"     String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
voter_count_
        , String
"vote_percentage" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
vote_percentage_
        , String
"is_chosen"       String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_chosen_
        , String
"is_being_chosen" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_being_chosen_
        ]

instance AT.FromJSON PollOption where
  parseJSON :: Value -> Parser PollOption
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
"pollOption" -> Value -> Parser PollOption
parsePollOption Value
v
      String
_            -> Parser PollOption
forall a. Monoid a => a
mempty
    
    where
      parsePollOption :: A.Value -> AT.Parser PollOption
      parsePollOption :: Value -> Parser PollOption
parsePollOption = String
-> (Object -> Parser PollOption) -> Value -> Parser PollOption
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PollOption" ((Object -> Parser PollOption) -> Value -> Parser PollOption)
-> (Object -> Parser PollOption) -> Value -> Parser PollOption
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe FormattedText
text_            <- Object
o Object -> Key -> Parser (Maybe FormattedText)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"text"
        Maybe Int
voter_count_     <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"voter_count"
        Maybe Int
vote_percentage_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"vote_percentage"
        Maybe Bool
is_chosen_       <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_chosen"
        Maybe Bool
is_being_chosen_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_being_chosen"
        PollOption -> Parser PollOption
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PollOption -> Parser PollOption)
-> PollOption -> Parser PollOption
forall a b. (a -> b) -> a -> b
$ PollOption
          { text :: Maybe FormattedText
text            = Maybe FormattedText
text_
          , voter_count :: Maybe Int
voter_count     = Maybe Int
voter_count_
          , vote_percentage :: Maybe Int
vote_percentage = Maybe Int
vote_percentage_
          , is_chosen :: Maybe Bool
is_chosen       = Maybe Bool
is_chosen_
          , is_being_chosen :: Maybe Bool
is_being_chosen = Maybe Bool
is_being_chosen_
          }
  parseJSON Value
_ = Parser PollOption
forall a. Monoid a => a
mempty