module TD.Data.PhoneNumberInfo
  (PhoneNumberInfo(..)) 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.CountryInfo as CountryInfo
import qualified Data.Text as T

data PhoneNumberInfo
  = PhoneNumberInfo -- ^ Contains information about a phone number
    { PhoneNumberInfo -> Maybe CountryInfo
country                :: Maybe CountryInfo.CountryInfo -- ^ Information about the country to which the phone number belongs; may be null
    , PhoneNumberInfo -> Maybe Text
country_calling_code   :: Maybe T.Text                  -- ^ The part of the phone number denoting country calling code or its part
    , PhoneNumberInfo -> Maybe Text
formatted_phone_number :: Maybe T.Text                  -- ^ The phone number without country calling code formatted accordingly to local rules. Expected digits are returned as '-', but even more digits might be entered by the user
    , PhoneNumberInfo -> Maybe Bool
is_anonymous           :: Maybe Bool                    -- ^ True, if the phone number was bought at https://fragment.com and isn't tied to a SIM card. Information about the phone number can be received using getCollectibleItemInfo
    }
  deriving (PhoneNumberInfo -> PhoneNumberInfo -> Bool
(PhoneNumberInfo -> PhoneNumberInfo -> Bool)
-> (PhoneNumberInfo -> PhoneNumberInfo -> Bool)
-> Eq PhoneNumberInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PhoneNumberInfo -> PhoneNumberInfo -> Bool
== :: PhoneNumberInfo -> PhoneNumberInfo -> Bool
$c/= :: PhoneNumberInfo -> PhoneNumberInfo -> Bool
/= :: PhoneNumberInfo -> PhoneNumberInfo -> Bool
Eq, Int -> PhoneNumberInfo -> ShowS
[PhoneNumberInfo] -> ShowS
PhoneNumberInfo -> String
(Int -> PhoneNumberInfo -> ShowS)
-> (PhoneNumberInfo -> String)
-> ([PhoneNumberInfo] -> ShowS)
-> Show PhoneNumberInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PhoneNumberInfo -> ShowS
showsPrec :: Int -> PhoneNumberInfo -> ShowS
$cshow :: PhoneNumberInfo -> String
show :: PhoneNumberInfo -> String
$cshowList :: [PhoneNumberInfo] -> ShowS
showList :: [PhoneNumberInfo] -> ShowS
Show)

instance I.ShortShow PhoneNumberInfo where
  shortShow :: PhoneNumberInfo -> String
shortShow PhoneNumberInfo
    { country :: PhoneNumberInfo -> Maybe CountryInfo
country                = Maybe CountryInfo
country_
    , country_calling_code :: PhoneNumberInfo -> Maybe Text
country_calling_code   = Maybe Text
country_calling_code_
    , formatted_phone_number :: PhoneNumberInfo -> Maybe Text
formatted_phone_number = Maybe Text
formatted_phone_number_
    , is_anonymous :: PhoneNumberInfo -> Maybe Bool
is_anonymous           = Maybe Bool
is_anonymous_
    }
      = String
"PhoneNumberInfo"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"country"                String -> Maybe CountryInfo -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe CountryInfo
country_
        , String
"country_calling_code"   String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
country_calling_code_
        , String
"formatted_phone_number" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
formatted_phone_number_
        , String
"is_anonymous"           String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_anonymous_
        ]

instance AT.FromJSON PhoneNumberInfo where
  parseJSON :: Value -> Parser PhoneNumberInfo
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
"phoneNumberInfo" -> Value -> Parser PhoneNumberInfo
parsePhoneNumberInfo Value
v
      String
_                 -> Parser PhoneNumberInfo
forall a. Monoid a => a
mempty
    
    where
      parsePhoneNumberInfo :: A.Value -> AT.Parser PhoneNumberInfo
      parsePhoneNumberInfo :: Value -> Parser PhoneNumberInfo
parsePhoneNumberInfo = String
-> (Object -> Parser PhoneNumberInfo)
-> Value
-> Parser PhoneNumberInfo
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PhoneNumberInfo" ((Object -> Parser PhoneNumberInfo)
 -> Value -> Parser PhoneNumberInfo)
-> (Object -> Parser PhoneNumberInfo)
-> Value
-> Parser PhoneNumberInfo
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe CountryInfo
country_                <- Object
o Object -> Key -> Parser (Maybe CountryInfo)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"country"
        Maybe Text
country_calling_code_   <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"country_calling_code"
        Maybe Text
formatted_phone_number_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"formatted_phone_number"
        Maybe Bool
is_anonymous_           <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_anonymous"
        PhoneNumberInfo -> Parser PhoneNumberInfo
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PhoneNumberInfo -> Parser PhoneNumberInfo)
-> PhoneNumberInfo -> Parser PhoneNumberInfo
forall a b. (a -> b) -> a -> b
$ PhoneNumberInfo
          { country :: Maybe CountryInfo
country                = Maybe CountryInfo
country_
          , country_calling_code :: Maybe Text
country_calling_code   = Maybe Text
country_calling_code_
          , formatted_phone_number :: Maybe Text
formatted_phone_number = Maybe Text
formatted_phone_number_
          , is_anonymous :: Maybe Bool
is_anonymous           = Maybe Bool
is_anonymous_
          }
  parseJSON Value
_ = Parser PhoneNumberInfo
forall a. Monoid a => a
mempty