module TD.Data.ChatLocation
  ( ChatLocation(..)    
  , defaultChatLocation 
  ) 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.Location as Location
import qualified Data.Text as T

data ChatLocation
  = ChatLocation -- ^ Represents a location to which a chat is connected
    { ChatLocation -> Maybe Location
location :: Maybe Location.Location -- ^ The location
    , ChatLocation -> Maybe Text
address  :: Maybe T.Text            -- ^ Location address; 1-64 characters, as defined by the chat owner
    }
  deriving (ChatLocation -> ChatLocation -> Bool
(ChatLocation -> ChatLocation -> Bool)
-> (ChatLocation -> ChatLocation -> Bool) -> Eq ChatLocation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChatLocation -> ChatLocation -> Bool
== :: ChatLocation -> ChatLocation -> Bool
$c/= :: ChatLocation -> ChatLocation -> Bool
/= :: ChatLocation -> ChatLocation -> Bool
Eq, Int -> ChatLocation -> ShowS
[ChatLocation] -> ShowS
ChatLocation -> String
(Int -> ChatLocation -> ShowS)
-> (ChatLocation -> String)
-> ([ChatLocation] -> ShowS)
-> Show ChatLocation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChatLocation -> ShowS
showsPrec :: Int -> ChatLocation -> ShowS
$cshow :: ChatLocation -> String
show :: ChatLocation -> String
$cshowList :: [ChatLocation] -> ShowS
showList :: [ChatLocation] -> ShowS
Show)

instance I.ShortShow ChatLocation where
  shortShow :: ChatLocation -> String
shortShow ChatLocation
    { location :: ChatLocation -> Maybe Location
location = Maybe Location
location_
    , address :: ChatLocation -> Maybe Text
address  = Maybe Text
address_
    }
      = String
"ChatLocation"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"location" String -> Maybe Location -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Location
location_
        , String
"address"  String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
address_
        ]

instance AT.FromJSON ChatLocation where
  parseJSON :: Value -> Parser ChatLocation
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
"chatLocation" -> Value -> Parser ChatLocation
parseChatLocation Value
v
      String
_              -> Parser ChatLocation
forall a. Monoid a => a
mempty
    
    where
      parseChatLocation :: A.Value -> AT.Parser ChatLocation
      parseChatLocation :: Value -> Parser ChatLocation
parseChatLocation = String
-> (Object -> Parser ChatLocation) -> Value -> Parser ChatLocation
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ChatLocation" ((Object -> Parser ChatLocation) -> Value -> Parser ChatLocation)
-> (Object -> Parser ChatLocation) -> Value -> Parser ChatLocation
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Location
location_ <- Object
o Object -> Key -> Parser (Maybe Location)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"location"
        Maybe Text
address_  <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"address"
        ChatLocation -> Parser ChatLocation
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ChatLocation -> Parser ChatLocation)
-> ChatLocation -> Parser ChatLocation
forall a b. (a -> b) -> a -> b
$ ChatLocation
          { location :: Maybe Location
location = Maybe Location
location_
          , address :: Maybe Text
address  = Maybe Text
address_
          }
  parseJSON Value
_ = Parser ChatLocation
forall a. Monoid a => a
mempty

instance AT.ToJSON ChatLocation where
  toJSON :: ChatLocation -> Value
toJSON ChatLocation
    { location :: ChatLocation -> Maybe Location
location = Maybe Location
location_
    , address :: ChatLocation -> Maybe Text
address  = Maybe Text
address_
    }
      = [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
"chatLocation"
        , Key
"location" Key -> Maybe Location -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Location
location_
        , Key
"address"  Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
address_
        ]

defaultChatLocation :: ChatLocation
defaultChatLocation :: ChatLocation
defaultChatLocation =
  ChatLocation
    { location :: Maybe Location
location = Maybe Location
forall a. Maybe a
Nothing
    , address :: Maybe Text
address  = Maybe Text
forall a. Maybe a
Nothing
    }