module TD.Data.PollType
  (PollType(..)) 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.MessageContent as MessageContent

-- | Describes the type of poll
data PollType
  = PollTypeRegular -- ^ A regular poll
  | PollTypeQuiz -- ^ A poll in quiz mode, which has predefined correct answers
    { PollType -> Maybe [Int]
correct_option_ids :: Maybe [Int]                         -- ^ Increasing list of 0-based identifiers of the correct answer options; empty for a yet unanswered poll
    , PollType -> Maybe FormattedText
explanation        :: Maybe FormattedText.FormattedText   -- ^ Text that is shown when the user chooses an incorrect answer or taps on the lamp icon; empty for a yet unanswered poll
    , PollType -> Maybe MessageContent
explanation_media  :: Maybe MessageContent.MessageContent -- ^ Media that is shown when the user chooses an incorrect answer or taps on the lamp icon; may be null if none or the poll is unanswered yet. Currently, can be only of the types messageAnimation, messageAudio, messageDocument, messageLocation, messagePhoto, messageVenue, or messageVideo without caption
    }
  deriving (PollType -> PollType -> Bool
(PollType -> PollType -> Bool)
-> (PollType -> PollType -> Bool) -> Eq PollType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PollType -> PollType -> Bool
== :: PollType -> PollType -> Bool
$c/= :: PollType -> PollType -> Bool
/= :: PollType -> PollType -> Bool
Eq, Int -> PollType -> ShowS
[PollType] -> ShowS
PollType -> String
(Int -> PollType -> ShowS)
-> (PollType -> String) -> ([PollType] -> ShowS) -> Show PollType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PollType -> ShowS
showsPrec :: Int -> PollType -> ShowS
$cshow :: PollType -> String
show :: PollType -> String
$cshowList :: [PollType] -> ShowS
showList :: [PollType] -> ShowS
Show)

instance I.ShortShow PollType where
  shortShow :: PollType -> String
shortShow PollType
PollTypeRegular
      = String
"PollTypeRegular"
  shortShow PollTypeQuiz
    { correct_option_ids :: PollType -> Maybe [Int]
correct_option_ids = Maybe [Int]
correct_option_ids_
    , explanation :: PollType -> Maybe FormattedText
explanation        = Maybe FormattedText
explanation_
    , explanation_media :: PollType -> Maybe MessageContent
explanation_media  = Maybe MessageContent
explanation_media_
    }
      = String
"PollTypeQuiz"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"correct_option_ids" String -> Maybe [Int] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Int]
correct_option_ids_
        , String
"explanation"        String -> Maybe FormattedText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe FormattedText
explanation_
        , String
"explanation_media"  String -> Maybe MessageContent -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe MessageContent
explanation_media_
        ]

instance AT.FromJSON PollType where
  parseJSON :: Value -> Parser PollType
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
"pollTypeRegular" -> PollType -> Parser PollType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure PollType
PollTypeRegular
      String
"pollTypeQuiz"    -> Value -> Parser PollType
parsePollTypeQuiz Value
v
      String
_                 -> Parser PollType
forall a. Monoid a => a
mempty
    
    where
      parsePollTypeQuiz :: A.Value -> AT.Parser PollType
      parsePollTypeQuiz :: Value -> Parser PollType
parsePollTypeQuiz = String -> (Object -> Parser PollType) -> Value -> Parser PollType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PollTypeQuiz" ((Object -> Parser PollType) -> Value -> Parser PollType)
-> (Object -> Parser PollType) -> Value -> Parser PollType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [Int]
correct_option_ids_ <- Object
o Object -> Key -> Parser (Maybe [Int])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"correct_option_ids"
        Maybe FormattedText
explanation_        <- Object
o Object -> Key -> Parser (Maybe FormattedText)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"explanation"
        Maybe MessageContent
explanation_media_  <- Object
o Object -> Key -> Parser (Maybe MessageContent)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"explanation_media"
        PollType -> Parser PollType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PollType -> Parser PollType) -> PollType -> Parser PollType
forall a b. (a -> b) -> a -> b
$ PollTypeQuiz
          { correct_option_ids :: Maybe [Int]
correct_option_ids = Maybe [Int]
correct_option_ids_
          , explanation :: Maybe FormattedText
explanation        = Maybe FormattedText
explanation_
          , explanation_media :: Maybe MessageContent
explanation_media  = Maybe MessageContent
explanation_media_
          }
  parseJSON Value
_ = Parser PollType
forall a. Monoid a => a
mempty