module TD.Data.Checklist
  (Checklist(..)) 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 qualified TD.Data.ChecklistTask as ChecklistTask

data Checklist
  = Checklist -- ^ Describes a checklist
    { Checklist -> Maybe FormattedText
title                         :: Maybe FormattedText.FormattedText   -- ^ Title of the checklist; may contain only Bold, Italic, Underline, Strikethrough, Spoiler, and CustomEmoji entities
    , Checklist -> Maybe [ChecklistTask]
tasks                         :: Maybe [ChecklistTask.ChecklistTask] -- ^ List of tasks in the checklist
    , Checklist -> Maybe Bool
others_can_add_tasks          :: Maybe Bool                          -- ^ True, if users other than creator of the list can add tasks to the list
    , Checklist -> Maybe Bool
can_add_tasks                 :: Maybe Bool                          -- ^ True, if the current user can add tasks to the list if they have Telegram Premium subscription
    , Checklist -> Maybe Bool
others_can_mark_tasks_as_done :: Maybe Bool                          -- ^ True, if users other than creator of the list can mark tasks as done or not done. If true, then the checklist is called "group checklist"
    , Checklist -> Maybe Bool
can_mark_tasks_as_done        :: Maybe Bool                          -- ^ True, if the current user can mark tasks as done or not done if they have Telegram Premium subscription
    }
  deriving (Checklist -> Checklist -> Bool
(Checklist -> Checklist -> Bool)
-> (Checklist -> Checklist -> Bool) -> Eq Checklist
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Checklist -> Checklist -> Bool
== :: Checklist -> Checklist -> Bool
$c/= :: Checklist -> Checklist -> Bool
/= :: Checklist -> Checklist -> Bool
Eq, Int -> Checklist -> ShowS
[Checklist] -> ShowS
Checklist -> String
(Int -> Checklist -> ShowS)
-> (Checklist -> String)
-> ([Checklist] -> ShowS)
-> Show Checklist
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Checklist -> ShowS
showsPrec :: Int -> Checklist -> ShowS
$cshow :: Checklist -> String
show :: Checklist -> String
$cshowList :: [Checklist] -> ShowS
showList :: [Checklist] -> ShowS
Show)

instance I.ShortShow Checklist where
  shortShow :: Checklist -> String
shortShow Checklist
    { title :: Checklist -> Maybe FormattedText
title                         = Maybe FormattedText
title_
    , tasks :: Checklist -> Maybe [ChecklistTask]
tasks                         = Maybe [ChecklistTask]
tasks_
    , others_can_add_tasks :: Checklist -> Maybe Bool
others_can_add_tasks          = Maybe Bool
others_can_add_tasks_
    , can_add_tasks :: Checklist -> Maybe Bool
can_add_tasks                 = Maybe Bool
can_add_tasks_
    , others_can_mark_tasks_as_done :: Checklist -> Maybe Bool
others_can_mark_tasks_as_done = Maybe Bool
others_can_mark_tasks_as_done_
    , can_mark_tasks_as_done :: Checklist -> Maybe Bool
can_mark_tasks_as_done        = Maybe Bool
can_mark_tasks_as_done_
    }
      = String
"Checklist"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"title"                         String -> Maybe FormattedText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe FormattedText
title_
        , String
"tasks"                         String -> Maybe [ChecklistTask] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [ChecklistTask]
tasks_
        , String
"others_can_add_tasks"          String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
others_can_add_tasks_
        , String
"can_add_tasks"                 String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
can_add_tasks_
        , String
"others_can_mark_tasks_as_done" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
others_can_mark_tasks_as_done_
        , String
"can_mark_tasks_as_done"        String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
can_mark_tasks_as_done_
        ]

instance AT.FromJSON Checklist where
  parseJSON :: Value -> Parser Checklist
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
"checklist" -> Value -> Parser Checklist
parseChecklist Value
v
      String
_           -> Parser Checklist
forall a. Monoid a => a
mempty
    
    where
      parseChecklist :: A.Value -> AT.Parser Checklist
      parseChecklist :: Value -> Parser Checklist
parseChecklist = String -> (Object -> Parser Checklist) -> Value -> Parser Checklist
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Checklist" ((Object -> Parser Checklist) -> Value -> Parser Checklist)
-> (Object -> Parser Checklist) -> Value -> Parser Checklist
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe FormattedText
title_                         <- Object
o Object -> Key -> Parser (Maybe FormattedText)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"title"
        Maybe [ChecklistTask]
tasks_                         <- Object
o Object -> Key -> Parser (Maybe [ChecklistTask])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"tasks"
        Maybe Bool
others_can_add_tasks_          <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"others_can_add_tasks"
        Maybe Bool
can_add_tasks_                 <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"can_add_tasks"
        Maybe Bool
others_can_mark_tasks_as_done_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"others_can_mark_tasks_as_done"
        Maybe Bool
can_mark_tasks_as_done_        <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"can_mark_tasks_as_done"
        Checklist -> Parser Checklist
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Checklist -> Parser Checklist) -> Checklist -> Parser Checklist
forall a b. (a -> b) -> a -> b
$ Checklist
          { title :: Maybe FormattedText
title                         = Maybe FormattedText
title_
          , tasks :: Maybe [ChecklistTask]
tasks                         = Maybe [ChecklistTask]
tasks_
          , others_can_add_tasks :: Maybe Bool
others_can_add_tasks          = Maybe Bool
others_can_add_tasks_
          , can_add_tasks :: Maybe Bool
can_add_tasks                 = Maybe Bool
can_add_tasks_
          , others_can_mark_tasks_as_done :: Maybe Bool
others_can_mark_tasks_as_done = Maybe Bool
others_can_mark_tasks_as_done_
          , can_mark_tasks_as_done :: Maybe Bool
can_mark_tasks_as_done        = Maybe Bool
can_mark_tasks_as_done_
          }
  parseJSON Value
_ = Parser Checklist
forall a. Monoid a => a
mempty