module TD.Data.Location
  ( Location(..)    
  , defaultLocation 
  ) where

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

data Location
  = Location -- ^ Describes a location on planet Earth
    { Location -> Maybe Double
latitude            :: Maybe Double -- ^ Latitude of the location in degrees; as defined by the sender
    , Location -> Maybe Double
longitude           :: Maybe Double -- ^ Longitude of the location, in degrees; as defined by the sender
    , Location -> Maybe Double
horizontal_accuracy :: Maybe Double -- ^ The estimated horizontal accuracy of the location, in meters; as defined by the sender. 0 if unknown
    }
  deriving (Location -> Location -> Bool
(Location -> Location -> Bool)
-> (Location -> Location -> Bool) -> Eq Location
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Location -> Location -> Bool
== :: Location -> Location -> Bool
$c/= :: Location -> Location -> Bool
/= :: Location -> Location -> Bool
Eq, Int -> Location -> ShowS
[Location] -> ShowS
Location -> String
(Int -> Location -> ShowS)
-> (Location -> String) -> ([Location] -> ShowS) -> Show Location
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Location -> ShowS
showsPrec :: Int -> Location -> ShowS
$cshow :: Location -> String
show :: Location -> String
$cshowList :: [Location] -> ShowS
showList :: [Location] -> ShowS
Show)

instance I.ShortShow Location where
  shortShow :: Location -> String
shortShow Location
    { latitude :: Location -> Maybe Double
latitude            = Maybe Double
latitude_
    , longitude :: Location -> Maybe Double
longitude           = Maybe Double
longitude_
    , horizontal_accuracy :: Location -> Maybe Double
horizontal_accuracy = Maybe Double
horizontal_accuracy_
    }
      = String
"Location"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"latitude"            String -> Maybe Double -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Double
latitude_
        , String
"longitude"           String -> Maybe Double -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Double
longitude_
        , String
"horizontal_accuracy" String -> Maybe Double -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Double
horizontal_accuracy_
        ]

instance AT.FromJSON Location where
  parseJSON :: Value -> Parser Location
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
"location" -> Value -> Parser Location
parseLocation Value
v
      String
_          -> Parser Location
forall a. Monoid a => a
mempty
    
    where
      parseLocation :: A.Value -> AT.Parser Location
      parseLocation :: Value -> Parser Location
parseLocation = String -> (Object -> Parser Location) -> Value -> Parser Location
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Location" ((Object -> Parser Location) -> Value -> Parser Location)
-> (Object -> Parser Location) -> Value -> Parser Location
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Double
latitude_            <- Object
o Object -> Key -> Parser (Maybe Double)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"latitude"
        Maybe Double
longitude_           <- Object
o Object -> Key -> Parser (Maybe Double)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"longitude"
        Maybe Double
horizontal_accuracy_ <- Object
o Object -> Key -> Parser (Maybe Double)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"horizontal_accuracy"
        Location -> Parser Location
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Location -> Parser Location) -> Location -> Parser Location
forall a b. (a -> b) -> a -> b
$ Location
          { latitude :: Maybe Double
latitude            = Maybe Double
latitude_
          , longitude :: Maybe Double
longitude           = Maybe Double
longitude_
          , horizontal_accuracy :: Maybe Double
horizontal_accuracy = Maybe Double
horizontal_accuracy_
          }
  parseJSON Value
_ = Parser Location
forall a. Monoid a => a
mempty

instance AT.ToJSON Location where
  toJSON :: Location -> Value
toJSON Location
    { latitude :: Location -> Maybe Double
latitude            = Maybe Double
latitude_
    , longitude :: Location -> Maybe Double
longitude           = Maybe Double
longitude_
    , horizontal_accuracy :: Location -> Maybe Double
horizontal_accuracy = Maybe Double
horizontal_accuracy_
    }
      = [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
"location"
        , Key
"latitude"            Key -> Maybe Double -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Double
latitude_
        , Key
"longitude"           Key -> Maybe Double -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Double
longitude_
        , Key
"horizontal_accuracy" Key -> Maybe Double -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Double
horizontal_accuracy_
        ]

defaultLocation :: Location
defaultLocation :: Location
defaultLocation =
  Location
    { latitude :: Maybe Double
latitude            = Maybe Double
forall a. Maybe a
Nothing
    , longitude :: Maybe Double
longitude           = Maybe Double
forall a. Maybe a
Nothing
    , horizontal_accuracy :: Maybe Double
horizontal_accuracy = Maybe Double
forall a. Maybe a
Nothing
    }