module TD.Data.BusinessLocation
  ( BusinessLocation(..)    
  , defaultBusinessLocation 
  ) 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 BusinessLocation
  = BusinessLocation -- ^ Represents a location of a business
    { BusinessLocation -> Maybe Location
location :: Maybe Location.Location -- ^ The location; may be null if not specified
    , BusinessLocation -> Maybe Text
address  :: Maybe T.Text            -- ^ Location address; 1-96 characters
    }
  deriving (BusinessLocation -> BusinessLocation -> Bool
(BusinessLocation -> BusinessLocation -> Bool)
-> (BusinessLocation -> BusinessLocation -> Bool)
-> Eq BusinessLocation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BusinessLocation -> BusinessLocation -> Bool
== :: BusinessLocation -> BusinessLocation -> Bool
$c/= :: BusinessLocation -> BusinessLocation -> Bool
/= :: BusinessLocation -> BusinessLocation -> Bool
Eq, Int -> BusinessLocation -> ShowS
[BusinessLocation] -> ShowS
BusinessLocation -> String
(Int -> BusinessLocation -> ShowS)
-> (BusinessLocation -> String)
-> ([BusinessLocation] -> ShowS)
-> Show BusinessLocation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BusinessLocation -> ShowS
showsPrec :: Int -> BusinessLocation -> ShowS
$cshow :: BusinessLocation -> String
show :: BusinessLocation -> String
$cshowList :: [BusinessLocation] -> ShowS
showList :: [BusinessLocation] -> ShowS
Show)

instance I.ShortShow BusinessLocation where
  shortShow :: BusinessLocation -> String
shortShow BusinessLocation
    { location :: BusinessLocation -> Maybe Location
location = Maybe Location
location_
    , address :: BusinessLocation -> Maybe Text
address  = Maybe Text
address_
    }
      = String
"BusinessLocation"
        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 BusinessLocation where
  parseJSON :: Value -> Parser BusinessLocation
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
"businessLocation" -> Value -> Parser BusinessLocation
parseBusinessLocation Value
v
      String
_                  -> Parser BusinessLocation
forall a. Monoid a => a
mempty
    
    where
      parseBusinessLocation :: A.Value -> AT.Parser BusinessLocation
      parseBusinessLocation :: Value -> Parser BusinessLocation
parseBusinessLocation = String
-> (Object -> Parser BusinessLocation)
-> Value
-> Parser BusinessLocation
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"BusinessLocation" ((Object -> Parser BusinessLocation)
 -> Value -> Parser BusinessLocation)
-> (Object -> Parser BusinessLocation)
-> Value
-> Parser BusinessLocation
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"
        BusinessLocation -> Parser BusinessLocation
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (BusinessLocation -> Parser BusinessLocation)
-> BusinessLocation -> Parser BusinessLocation
forall a b. (a -> b) -> a -> b
$ BusinessLocation
          { location :: Maybe Location
location = Maybe Location
location_
          , address :: Maybe Text
address  = Maybe Text
address_
          }
  parseJSON Value
_ = Parser BusinessLocation
forall a. Monoid a => a
mempty

instance AT.ToJSON BusinessLocation where
  toJSON :: BusinessLocation -> Value
toJSON BusinessLocation
    { location :: BusinessLocation -> Maybe Location
location = Maybe Location
location_
    , address :: BusinessLocation -> 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
"businessLocation"
        , 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_
        ]

defaultBusinessLocation :: BusinessLocation
defaultBusinessLocation :: BusinessLocation
defaultBusinessLocation =
  BusinessLocation
    { location :: Maybe Location
location = Maybe Location
forall a. Maybe a
Nothing
    , address :: Maybe Text
address  = Maybe Text
forall a. Maybe a
Nothing
    }