module TD.Data.ChecklistTask
  (ChecklistTask(..)) 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 ChecklistTask
  = ChecklistTask -- ^ Describes a task in a checklist
    { ChecklistTask -> Maybe Int
_id                  :: Maybe Int                         -- ^ Unique identifier of the task
    , ChecklistTask -> Maybe FormattedText
text                 :: Maybe FormattedText.FormattedText -- ^ Text of the task; may contain only Bold, Italic, Underline, Strikethrough, Spoiler, CustomEmoji, Url, EmailAddress, Mention, Hashtag, Cashtag and PhoneNumber entities
    , ChecklistTask -> Maybe Int
completed_by_user_id :: Maybe Int                         -- ^ Identifier of the user that completed the task; 0 if the task isn't completed
    , ChecklistTask -> Maybe Int
completion_date      :: Maybe Int                         -- ^ Point in time (Unix timestamp) when the task was completed; 0 if the task isn't completed
    }
  deriving (ChecklistTask -> ChecklistTask -> Bool
(ChecklistTask -> ChecklistTask -> Bool)
-> (ChecklistTask -> ChecklistTask -> Bool) -> Eq ChecklistTask
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChecklistTask -> ChecklistTask -> Bool
== :: ChecklistTask -> ChecklistTask -> Bool
$c/= :: ChecklistTask -> ChecklistTask -> Bool
/= :: ChecklistTask -> ChecklistTask -> Bool
Eq, Int -> ChecklistTask -> ShowS
[ChecklistTask] -> ShowS
ChecklistTask -> String
(Int -> ChecklistTask -> ShowS)
-> (ChecklistTask -> String)
-> ([ChecklistTask] -> ShowS)
-> Show ChecklistTask
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChecklistTask -> ShowS
showsPrec :: Int -> ChecklistTask -> ShowS
$cshow :: ChecklistTask -> String
show :: ChecklistTask -> String
$cshowList :: [ChecklistTask] -> ShowS
showList :: [ChecklistTask] -> ShowS
Show)

instance I.ShortShow ChecklistTask where
  shortShow :: ChecklistTask -> String
shortShow ChecklistTask
    { _id :: ChecklistTask -> Maybe Int
_id                  = Maybe Int
_id_
    , text :: ChecklistTask -> Maybe FormattedText
text                 = Maybe FormattedText
text_
    , completed_by_user_id :: ChecklistTask -> Maybe Int
completed_by_user_id = Maybe Int
completed_by_user_id_
    , completion_date :: ChecklistTask -> Maybe Int
completion_date      = Maybe Int
completion_date_
    }
      = String
"ChecklistTask"
        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
"text"                 String -> Maybe FormattedText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe FormattedText
text_
        , String
"completed_by_user_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
completed_by_user_id_
        , String
"completion_date"      String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
completion_date_
        ]

instance AT.FromJSON ChecklistTask where
  parseJSON :: Value -> Parser ChecklistTask
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
"checklistTask" -> Value -> Parser ChecklistTask
parseChecklistTask Value
v
      String
_               -> Parser ChecklistTask
forall a. Monoid a => a
mempty
    
    where
      parseChecklistTask :: A.Value -> AT.Parser ChecklistTask
      parseChecklistTask :: Value -> Parser ChecklistTask
parseChecklistTask = String
-> (Object -> Parser ChecklistTask)
-> Value
-> Parser ChecklistTask
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChecklistTask" ((Object -> Parser ChecklistTask) -> Value -> Parser ChecklistTask)
-> (Object -> Parser ChecklistTask)
-> Value
-> Parser ChecklistTask
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_id_                  <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        Maybe FormattedText
text_                 <- Object
o Object -> Key -> Parser (Maybe FormattedText)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"text"
        Maybe Int
completed_by_user_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"completed_by_user_id"
        Maybe Int
completion_date_      <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"completion_date"
        ChecklistTask -> Parser ChecklistTask
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChecklistTask -> Parser ChecklistTask)
-> ChecklistTask -> Parser ChecklistTask
forall a b. (a -> b) -> a -> b
$ ChecklistTask
          { _id :: Maybe Int
_id                  = Maybe Int
_id_
          , text :: Maybe FormattedText
text                 = Maybe FormattedText
text_
          , completed_by_user_id :: Maybe Int
completed_by_user_id = Maybe Int
completed_by_user_id_
          , completion_date :: Maybe Int
completion_date      = Maybe Int
completion_date_
          }
  parseJSON Value
_ = Parser ChecklistTask
forall a. Monoid a => a
mempty