module TD.Data.Contact
  ( Contact(..)    
  , defaultContact 
  ) where

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

data Contact
  = Contact -- ^ Describes a user contact
    { Contact -> Maybe Text
phone_number :: Maybe T.Text -- ^ Phone number of the user
    , Contact -> Maybe Text
first_name   :: Maybe T.Text -- ^ First name of the user; 1-255 characters in length
    , Contact -> Maybe Text
last_name    :: Maybe T.Text -- ^ Last name of the user
    , Contact -> Maybe Text
vcard        :: Maybe T.Text -- ^ Additional data about the user in a form of vCard; 0-2048 bytes in length
    , Contact -> Maybe Int
user_id      :: Maybe Int    -- ^ Identifier of the user, if known; 0 otherwise
    }
  deriving (Contact -> Contact -> Bool
(Contact -> Contact -> Bool)
-> (Contact -> Contact -> Bool) -> Eq Contact
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Contact -> Contact -> Bool
== :: Contact -> Contact -> Bool
$c/= :: Contact -> Contact -> Bool
/= :: Contact -> Contact -> Bool
Eq, Int -> Contact -> ShowS
[Contact] -> ShowS
Contact -> String
(Int -> Contact -> ShowS)
-> (Contact -> String) -> ([Contact] -> ShowS) -> Show Contact
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Contact -> ShowS
showsPrec :: Int -> Contact -> ShowS
$cshow :: Contact -> String
show :: Contact -> String
$cshowList :: [Contact] -> ShowS
showList :: [Contact] -> ShowS
Show)

instance I.ShortShow Contact where
  shortShow :: Contact -> String
shortShow Contact
    { phone_number :: Contact -> Maybe Text
phone_number = Maybe Text
phone_number_
    , first_name :: Contact -> Maybe Text
first_name   = Maybe Text
first_name_
    , last_name :: Contact -> Maybe Text
last_name    = Maybe Text
last_name_
    , vcard :: Contact -> Maybe Text
vcard        = Maybe Text
vcard_
    , user_id :: Contact -> Maybe Int
user_id      = Maybe Int
user_id_
    }
      = String
"Contact"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"phone_number" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
phone_number_
        , String
"first_name"   String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
first_name_
        , String
"last_name"    String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
last_name_
        , String
"vcard"        String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
vcard_
        , String
"user_id"      String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
user_id_
        ]

instance AT.FromJSON Contact where
  parseJSON :: Value -> Parser Contact
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
"contact" -> Value -> Parser Contact
parseContact Value
v
      String
_         -> Parser Contact
forall a. Monoid a => a
mempty
    
    where
      parseContact :: A.Value -> AT.Parser Contact
      parseContact :: Value -> Parser Contact
parseContact = String -> (Object -> Parser Contact) -> Value -> Parser Contact
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Contact" ((Object -> Parser Contact) -> Value -> Parser Contact)
-> (Object -> Parser Contact) -> Value -> Parser Contact
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
phone_number_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"phone_number"
        Maybe Text
first_name_   <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"first_name"
        Maybe Text
last_name_    <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"last_name"
        Maybe Text
vcard_        <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"vcard"
        Maybe Int
user_id_      <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"user_id"
        Contact -> Parser Contact
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Contact -> Parser Contact) -> Contact -> Parser Contact
forall a b. (a -> b) -> a -> b
$ Contact
          { phone_number :: Maybe Text
phone_number = Maybe Text
phone_number_
          , first_name :: Maybe Text
first_name   = Maybe Text
first_name_
          , last_name :: Maybe Text
last_name    = Maybe Text
last_name_
          , vcard :: Maybe Text
vcard        = Maybe Text
vcard_
          , user_id :: Maybe Int
user_id      = Maybe Int
user_id_
          }
  parseJSON Value
_ = Parser Contact
forall a. Monoid a => a
mempty

instance AT.ToJSON Contact where
  toJSON :: Contact -> Value
toJSON Contact
    { phone_number :: Contact -> Maybe Text
phone_number = Maybe Text
phone_number_
    , first_name :: Contact -> Maybe Text
first_name   = Maybe Text
first_name_
    , last_name :: Contact -> Maybe Text
last_name    = Maybe Text
last_name_
    , vcard :: Contact -> Maybe Text
vcard        = Maybe Text
vcard_
    , user_id :: Contact -> Maybe Int
user_id      = Maybe Int
user_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
"contact"
        , Key
"phone_number" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
phone_number_
        , Key
"first_name"   Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
first_name_
        , Key
"last_name"    Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
last_name_
        , Key
"vcard"        Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
vcard_
        , Key
"user_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
user_id_
        ]

defaultContact :: Contact
defaultContact :: Contact
defaultContact =
  Contact
    { phone_number :: Maybe Text
phone_number = Maybe Text
forall a. Maybe a
Nothing
    , first_name :: Maybe Text
first_name   = Maybe Text
forall a. Maybe a
Nothing
    , last_name :: Maybe Text
last_name    = Maybe Text
forall a. Maybe a
Nothing
    , vcard :: Maybe Text
vcard        = Maybe Text
forall a. Maybe a
Nothing
    , user_id :: Maybe Int
user_id      = Maybe Int
forall a. Maybe a
Nothing
    }