module TD.Data.Address
  ( Address(..)    
  , defaultAddress 
  ) 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 Address
  = Address -- ^ Describes an address
    { Address -> Maybe Text
country_code :: Maybe T.Text -- ^ A two-letter ISO 3166-1 alpha-2 country code
    , Address -> Maybe Text
state        :: Maybe T.Text -- ^ State, if applicable
    , Address -> Maybe Text
city         :: Maybe T.Text -- ^ City
    , Address -> Maybe Text
street_line1 :: Maybe T.Text -- ^ First line of the address
    , Address -> Maybe Text
street_line2 :: Maybe T.Text -- ^ Second line of the address
    , Address -> Maybe Text
postal_code  :: Maybe T.Text -- ^ Address postal code
    }
  deriving (Address -> Address -> Bool
(Address -> Address -> Bool)
-> (Address -> Address -> Bool) -> Eq Address
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Address -> Address -> Bool
== :: Address -> Address -> Bool
$c/= :: Address -> Address -> Bool
/= :: Address -> Address -> Bool
Eq, Int -> Address -> ShowS
[Address] -> ShowS
Address -> String
(Int -> Address -> ShowS)
-> (Address -> String) -> ([Address] -> ShowS) -> Show Address
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Address -> ShowS
showsPrec :: Int -> Address -> ShowS
$cshow :: Address -> String
show :: Address -> String
$cshowList :: [Address] -> ShowS
showList :: [Address] -> ShowS
Show)

instance I.ShortShow Address where
  shortShow :: Address -> String
shortShow Address
    { country_code :: Address -> Maybe Text
country_code = Maybe Text
country_code_
    , state :: Address -> Maybe Text
state        = Maybe Text
state_
    , city :: Address -> Maybe Text
city         = Maybe Text
city_
    , street_line1 :: Address -> Maybe Text
street_line1 = Maybe Text
street_line1_
    , street_line2 :: Address -> Maybe Text
street_line2 = Maybe Text
street_line2_
    , postal_code :: Address -> Maybe Text
postal_code  = Maybe Text
postal_code_
    }
      = String
"Address"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"country_code" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
country_code_
        , String
"state"        String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
state_
        , String
"city"         String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
city_
        , String
"street_line1" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
street_line1_
        , String
"street_line2" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
street_line2_
        , String
"postal_code"  String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
postal_code_
        ]

instance AT.FromJSON Address where
  parseJSON :: Value -> Parser Address
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
"address" -> Value -> Parser Address
parseAddress Value
v
      String
_         -> Parser Address
forall a. Monoid a => a
mempty
    
    where
      parseAddress :: A.Value -> AT.Parser Address
      parseAddress :: Value -> Parser Address
parseAddress = String -> (Object -> Parser Address) -> Value -> Parser Address
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Address" ((Object -> Parser Address) -> Value -> Parser Address)
-> (Object -> Parser Address) -> Value -> Parser Address
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
country_code_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"country_code"
        Maybe Text
state_        <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"state"
        Maybe Text
city_         <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"city"
        Maybe Text
street_line1_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"street_line1"
        Maybe Text
street_line2_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"street_line2"
        Maybe Text
postal_code_  <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"postal_code"
        Address -> Parser Address
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Address -> Parser Address) -> Address -> Parser Address
forall a b. (a -> b) -> a -> b
$ Address
          { country_code :: Maybe Text
country_code = Maybe Text
country_code_
          , state :: Maybe Text
state        = Maybe Text
state_
          , city :: Maybe Text
city         = Maybe Text
city_
          , street_line1 :: Maybe Text
street_line1 = Maybe Text
street_line1_
          , street_line2 :: Maybe Text
street_line2 = Maybe Text
street_line2_
          , postal_code :: Maybe Text
postal_code  = Maybe Text
postal_code_
          }
  parseJSON Value
_ = Parser Address
forall a. Monoid a => a
mempty

instance AT.ToJSON Address where
  toJSON :: Address -> Value
toJSON Address
    { country_code :: Address -> Maybe Text
country_code = Maybe Text
country_code_
    , state :: Address -> Maybe Text
state        = Maybe Text
state_
    , city :: Address -> Maybe Text
city         = Maybe Text
city_
    , street_line1 :: Address -> Maybe Text
street_line1 = Maybe Text
street_line1_
    , street_line2 :: Address -> Maybe Text
street_line2 = Maybe Text
street_line2_
    , postal_code :: Address -> Maybe Text
postal_code  = Maybe Text
postal_code_
    }
      = [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
"address"
        , Key
"country_code" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
country_code_
        , Key
"state"        Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
state_
        , Key
"city"         Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
city_
        , Key
"street_line1" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
street_line1_
        , Key
"street_line2" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
street_line2_
        , Key
"postal_code"  Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
postal_code_
        ]

defaultAddress :: Address
defaultAddress :: Address
defaultAddress =
  Address
    { country_code :: Maybe Text
country_code = Maybe Text
forall a. Maybe a
Nothing
    , state :: Maybe Text
state        = Maybe Text
forall a. Maybe a
Nothing
    , city :: Maybe Text
city         = Maybe Text
forall a. Maybe a
Nothing
    , street_line1 :: Maybe Text
street_line1 = Maybe Text
forall a. Maybe a
Nothing
    , street_line2 :: Maybe Text
street_line2 = Maybe Text
forall a. Maybe a
Nothing
    , postal_code :: Maybe Text
postal_code  = Maybe Text
forall a. Maybe a
Nothing
    }