module TD.Data.ChatList
  (ChatList(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I

-- | Describes a list of chats
data ChatList
  = ChatListMain -- ^ A main list of chats
  | ChatListArchive -- ^ A list of chats usually located at the top of the main chat list. Unmuted chats are automatically moved from the Archive to the Main chat list when a new message arrives
  | ChatListFolder -- ^ A list of chats added to a chat folder
    { ChatList -> Maybe Int
chat_folder_id :: Maybe Int -- ^ Chat folder identifier
    }
  deriving (ChatList -> ChatList -> Bool
(ChatList -> ChatList -> Bool)
-> (ChatList -> ChatList -> Bool) -> Eq ChatList
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChatList -> ChatList -> Bool
== :: ChatList -> ChatList -> Bool
$c/= :: ChatList -> ChatList -> Bool
/= :: ChatList -> ChatList -> Bool
Eq, Int -> ChatList -> ShowS
[ChatList] -> ShowS
ChatList -> String
(Int -> ChatList -> ShowS)
-> (ChatList -> String) -> ([ChatList] -> ShowS) -> Show ChatList
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChatList -> ShowS
showsPrec :: Int -> ChatList -> ShowS
$cshow :: ChatList -> String
show :: ChatList -> String
$cshowList :: [ChatList] -> ShowS
showList :: [ChatList] -> ShowS
Show)

instance I.ShortShow ChatList where
  shortShow :: ChatList -> String
shortShow ChatList
ChatListMain
      = String
"ChatListMain"
  shortShow ChatList
ChatListArchive
      = String
"ChatListArchive"
  shortShow ChatListFolder
    { chat_folder_id :: ChatList -> Maybe Int
chat_folder_id = Maybe Int
chat_folder_id_
    }
      = String
"ChatListFolder"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"chat_folder_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
chat_folder_id_
        ]

instance AT.FromJSON ChatList where
  parseJSON :: Value -> Parser ChatList
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
"chatListMain"    -> ChatList -> Parser ChatList
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ChatList
ChatListMain
      String
"chatListArchive" -> ChatList -> Parser ChatList
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ChatList
ChatListArchive
      String
"chatListFolder"  -> Value -> Parser ChatList
parseChatListFolder Value
v
      String
_                 -> Parser ChatList
forall a. Monoid a => a
mempty
    
    where
      parseChatListFolder :: A.Value -> AT.Parser ChatList
      parseChatListFolder :: Value -> Parser ChatList
parseChatListFolder = String -> (Object -> Parser ChatList) -> Value -> Parser ChatList
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatListFolder" ((Object -> Parser ChatList) -> Value -> Parser ChatList)
-> (Object -> Parser ChatList) -> Value -> Parser ChatList
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
chat_folder_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"chat_folder_id"
        ChatList -> Parser ChatList
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatList -> Parser ChatList) -> ChatList -> Parser ChatList
forall a b. (a -> b) -> a -> b
$ ChatListFolder
          { chat_folder_id :: Maybe Int
chat_folder_id = Maybe Int
chat_folder_id_
          }
  parseJSON Value
_ = Parser ChatList
forall a. Monoid a => a
mempty

instance AT.ToJSON ChatList where
  toJSON :: ChatList -> Value
toJSON ChatList
ChatListMain
      = [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
"chatListMain"
        ]
  toJSON ChatList
ChatListArchive
      = [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
"chatListArchive"
        ]
  toJSON ChatListFolder
    { chat_folder_id :: ChatList -> Maybe Int
chat_folder_id = Maybe Int
chat_folder_id_
    }
      = [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
"chatListFolder"
        , Key
"chat_folder_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
chat_folder_id_
        ]