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

-- | Describes the type of poll
data PollType
  = PollTypeRegular -- ^ A regular poll
    { PollType -> Maybe Bool
allow_multiple_answers :: Maybe Bool -- ^ True, if multiple answer options can be chosen simultaneously
    }
  | PollTypeQuiz -- ^ A poll in quiz mode, which has exactly one correct answer option and can be answered only once
    { PollType -> Maybe Int
correct_option_id :: Maybe Int                         -- ^ 0-based identifier of the correct answer option; -1 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; 0-200 characters with at most 2 line feeds; empty for a yet unanswered poll
    }
  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 PollTypeRegular
    { allow_multiple_answers :: PollType -> Maybe Bool
allow_multiple_answers = Maybe Bool
allow_multiple_answers_
    }
      = String
"PollTypeRegular"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"allow_multiple_answers" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
allow_multiple_answers_
        ]
  shortShow PollTypeQuiz
    { correct_option_id :: PollType -> Maybe Int
correct_option_id = Maybe Int
correct_option_id_
    , explanation :: PollType -> Maybe FormattedText
explanation       = Maybe FormattedText
explanation_
    }
      = String
"PollTypeQuiz"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"correct_option_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
correct_option_id_
        , String
"explanation"       String -> Maybe FormattedText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe FormattedText
explanation_
        ]

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" -> Value -> Parser PollType
parsePollTypeRegular Value
v
      String
"pollTypeQuiz"    -> Value -> Parser PollType
parsePollTypeQuiz Value
v
      String
_                 -> Parser PollType
forall a. Monoid a => a
mempty
    
    where
      parsePollTypeRegular :: A.Value -> AT.Parser PollType
      parsePollTypeRegular :: Value -> Parser PollType
parsePollTypeRegular = String -> (Object -> Parser PollType) -> Value -> Parser PollType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PollTypeRegular" ((Object -> Parser PollType) -> Value -> Parser PollType)
-> (Object -> Parser PollType) -> Value -> Parser PollType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Bool
allow_multiple_answers_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"allow_multiple_answers"
        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
$ PollTypeRegular
          { allow_multiple_answers :: Maybe Bool
allow_multiple_answers = Maybe Bool
allow_multiple_answers_
          }
      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_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"correct_option_id"
        Maybe FormattedText
explanation_       <- Object
o Object -> Key -> Parser (Maybe FormattedText)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"explanation"
        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_id :: Maybe Int
correct_option_id = Maybe Int
correct_option_id_
          , explanation :: Maybe FormattedText
explanation       = Maybe FormattedText
explanation_
          }
  parseJSON Value
_ = Parser PollType
forall a. Monoid a => a
mempty

instance AT.ToJSON PollType where
  toJSON :: PollType -> Value
toJSON PollTypeRegular
    { allow_multiple_answers :: PollType -> Maybe Bool
allow_multiple_answers = Maybe Bool
allow_multiple_answers_
    }
      = [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
"pollTypeRegular"
        , Key
"allow_multiple_answers" 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_multiple_answers_
        ]
  toJSON PollTypeQuiz
    { correct_option_id :: PollType -> Maybe Int
correct_option_id = Maybe Int
correct_option_id_
    , explanation :: PollType -> 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
"pollTypeQuiz"
        , Key
"correct_option_id" 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_id_
        , 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_
        ]