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

-- | Describes the type of poll to send
data InputPollType
  = InputPollTypeRegular -- ^ A regular poll
    { InputPollType -> Maybe Bool
allow_adding_options :: Maybe Bool -- ^ True, if answer options can be added to the poll after creation; not supported in channel chats and for anonymous polls
    }
  | InputPollTypeQuiz -- ^ A poll in quiz mode, which has predefined correct answers
    { InputPollType -> Maybe [Int]
correct_option_ids :: Maybe [Int]                       -- ^ Increasing list of 0-based identifiers of the correct answer options; must be non-empty
    , InputPollType -> Maybe FormattedText
explanation        :: Maybe FormattedText.FormattedText -- ^ Text that is shown when the user chooses an incorrect answer or taps on the lamp icon; 0-200 characters with at most 2 line feeds
    }
  deriving (InputPollType -> InputPollType -> Bool
(InputPollType -> InputPollType -> Bool)
-> (InputPollType -> InputPollType -> Bool) -> Eq InputPollType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: InputPollType -> InputPollType -> Bool
== :: InputPollType -> InputPollType -> Bool
$c/= :: InputPollType -> InputPollType -> Bool
/= :: InputPollType -> InputPollType -> Bool
Eq, Int -> InputPollType -> ShowS
[InputPollType] -> ShowS
InputPollType -> String
(Int -> InputPollType -> ShowS)
-> (InputPollType -> String)
-> ([InputPollType] -> ShowS)
-> Show InputPollType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> InputPollType -> ShowS
showsPrec :: Int -> InputPollType -> ShowS
$cshow :: InputPollType -> String
show :: InputPollType -> String
$cshowList :: [InputPollType] -> ShowS
showList :: [InputPollType] -> ShowS
Show)

instance I.ShortShow InputPollType where
  shortShow :: InputPollType -> String
shortShow InputPollTypeRegular
    { allow_adding_options :: InputPollType -> Maybe Bool
allow_adding_options = Maybe Bool
allow_adding_options_
    }
      = String
"InputPollTypeRegular"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"allow_adding_options" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
allow_adding_options_
        ]
  shortShow InputPollTypeQuiz
    { correct_option_ids :: InputPollType -> Maybe [Int]
correct_option_ids = Maybe [Int]
correct_option_ids_
    , explanation :: InputPollType -> Maybe FormattedText
explanation        = Maybe FormattedText
explanation_
    }
      = String
"InputPollTypeQuiz"
        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_
        ]

instance AT.FromJSON InputPollType where
  parseJSON :: Value -> Parser InputPollType
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
"inputPollTypeRegular" -> Value -> Parser InputPollType
parseInputPollTypeRegular Value
v
      String
"inputPollTypeQuiz"    -> Value -> Parser InputPollType
parseInputPollTypeQuiz Value
v
      String
_                      -> Parser InputPollType
forall a. Monoid a => a
mempty
    
    where
      parseInputPollTypeRegular :: A.Value -> AT.Parser InputPollType
      parseInputPollTypeRegular :: Value -> Parser InputPollType
parseInputPollTypeRegular = String
-> (Object -> Parser InputPollType)
-> Value
-> Parser InputPollType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"InputPollTypeRegular" ((Object -> Parser InputPollType) -> Value -> Parser InputPollType)
-> (Object -> Parser InputPollType)
-> Value
-> Parser InputPollType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Bool
allow_adding_options_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"allow_adding_options"
        InputPollType -> Parser InputPollType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputPollType -> Parser InputPollType)
-> InputPollType -> Parser InputPollType
forall a b. (a -> b) -> a -> b
$ InputPollTypeRegular
          { allow_adding_options :: Maybe Bool
allow_adding_options = Maybe Bool
allow_adding_options_
          }
      parseInputPollTypeQuiz :: A.Value -> AT.Parser InputPollType
      parseInputPollTypeQuiz :: Value -> Parser InputPollType
parseInputPollTypeQuiz = String
-> (Object -> Parser InputPollType)
-> Value
-> Parser InputPollType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"InputPollTypeQuiz" ((Object -> Parser InputPollType) -> Value -> Parser InputPollType)
-> (Object -> Parser InputPollType)
-> Value
-> Parser InputPollType
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"
        InputPollType -> Parser InputPollType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (InputPollType -> Parser InputPollType)
-> InputPollType -> Parser InputPollType
forall a b. (a -> b) -> a -> b
$ InputPollTypeQuiz
          { correct_option_ids :: Maybe [Int]
correct_option_ids = Maybe [Int]
correct_option_ids_
          , explanation :: Maybe FormattedText
explanation        = Maybe FormattedText
explanation_
          }
  parseJSON Value
_ = Parser InputPollType
forall a. Monoid a => a
mempty

instance AT.ToJSON InputPollType where
  toJSON :: InputPollType -> Value
toJSON InputPollTypeRegular
    { allow_adding_options :: InputPollType -> Maybe Bool
allow_adding_options = Maybe Bool
allow_adding_options_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"                Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"inputPollTypeRegular"
        , Key
"allow_adding_options" Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
allow_adding_options_
        ]
  toJSON InputPollTypeQuiz
    { correct_option_ids :: InputPollType -> Maybe [Int]
correct_option_ids = Maybe [Int]
correct_option_ids_
    , explanation :: InputPollType -> Maybe FormattedText
explanation        = Maybe FormattedText
explanation_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type"              Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"inputPollTypeQuiz"
        , Key
"correct_option_ids" Key -> Maybe [Int] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe [Int]
correct_option_ids_
        , Key
"explanation"        Key -> Maybe FormattedText -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe FormattedText
explanation_
        ]