module TD.Data.LiveLocation
  ( LiveLocation(..)    
  , defaultLiveLocation 
  ) 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

data LiveLocation
  = LiveLocation -- ^ A live location
    { LiveLocation -> Maybe Location
location               :: Maybe Location.Location -- ^ The current location
    , LiveLocation -> Maybe Int
live_period            :: Maybe Int               -- ^ Time relative to the message send date, for which the location can be updated, in seconds; if 0x7FFFFFFF, then location can be updated forever
    , LiveLocation -> Maybe Int
heading                :: Maybe Int               -- ^ The direction in which the location moves, in degrees; 1-360; 0 if unknown
    , LiveLocation -> Maybe Int
proximity_alert_radius :: Maybe Int               -- ^ The maximum distance to another chat member for proximity alerts, in meters (0-100000). 0 if the notification is disabled. Can't be enabled in direct messages chats, channels and Saved Messages. Available only to the message sender
    }
  deriving (LiveLocation -> LiveLocation -> Bool
(LiveLocation -> LiveLocation -> Bool)
-> (LiveLocation -> LiveLocation -> Bool) -> Eq LiveLocation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LiveLocation -> LiveLocation -> Bool
== :: LiveLocation -> LiveLocation -> Bool
$c/= :: LiveLocation -> LiveLocation -> Bool
/= :: LiveLocation -> LiveLocation -> Bool
Eq, Int -> LiveLocation -> ShowS
[LiveLocation] -> ShowS
LiveLocation -> String
(Int -> LiveLocation -> ShowS)
-> (LiveLocation -> String)
-> ([LiveLocation] -> ShowS)
-> Show LiveLocation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LiveLocation -> ShowS
showsPrec :: Int -> LiveLocation -> ShowS
$cshow :: LiveLocation -> String
show :: LiveLocation -> String
$cshowList :: [LiveLocation] -> ShowS
showList :: [LiveLocation] -> ShowS
Show)

instance I.ShortShow LiveLocation where
  shortShow :: LiveLocation -> String
shortShow LiveLocation
    { location :: LiveLocation -> Maybe Location
location               = Maybe Location
location_
    , live_period :: LiveLocation -> Maybe Int
live_period            = Maybe Int
live_period_
    , heading :: LiveLocation -> Maybe Int
heading                = Maybe Int
heading_
    , proximity_alert_radius :: LiveLocation -> Maybe Int
proximity_alert_radius = Maybe Int
proximity_alert_radius_
    }
      = String
"LiveLocation"
        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
"live_period"            String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
live_period_
        , String
"heading"                String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
heading_
        , String
"proximity_alert_radius" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
proximity_alert_radius_
        ]

instance AT.FromJSON LiveLocation where
  parseJSON :: Value -> Parser LiveLocation
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
"liveLocation" -> Value -> Parser LiveLocation
parseLiveLocation Value
v
      String
_              -> Parser LiveLocation
forall a. Monoid a => a
mempty
    
    where
      parseLiveLocation :: A.Value -> AT.Parser LiveLocation
      parseLiveLocation :: Value -> Parser LiveLocation
parseLiveLocation = String
-> (Object -> Parser LiveLocation) -> Value -> Parser LiveLocation
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"LiveLocation" ((Object -> Parser LiveLocation) -> Value -> Parser LiveLocation)
-> (Object -> Parser LiveLocation) -> Value -> Parser LiveLocation
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 Int
live_period_            <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"live_period"
        Maybe Int
heading_                <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"heading"
        Maybe Int
proximity_alert_radius_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"proximity_alert_radius"
        LiveLocation -> Parser LiveLocation
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LiveLocation -> Parser LiveLocation)
-> LiveLocation -> Parser LiveLocation
forall a b. (a -> b) -> a -> b
$ LiveLocation
          { location :: Maybe Location
location               = Maybe Location
location_
          , live_period :: Maybe Int
live_period            = Maybe Int
live_period_
          , heading :: Maybe Int
heading                = Maybe Int
heading_
          , proximity_alert_radius :: Maybe Int
proximity_alert_radius = Maybe Int
proximity_alert_radius_
          }
  parseJSON Value
_ = Parser LiveLocation
forall a. Monoid a => a
mempty

instance AT.ToJSON LiveLocation where
  toJSON :: LiveLocation -> Value
toJSON LiveLocation
    { location :: LiveLocation -> Maybe Location
location               = Maybe Location
location_
    , live_period :: LiveLocation -> Maybe Int
live_period            = Maybe Int
live_period_
    , heading :: LiveLocation -> Maybe Int
heading                = Maybe Int
heading_
    , proximity_alert_radius :: LiveLocation -> Maybe Int
proximity_alert_radius = Maybe Int
proximity_alert_radius_
    }
      = [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
"liveLocation"
        , 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
"live_period"            Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
live_period_
        , Key
"heading"                Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
heading_
        , Key
"proximity_alert_radius" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
proximity_alert_radius_
        ]

defaultLiveLocation :: LiveLocation
defaultLiveLocation :: LiveLocation
defaultLiveLocation =
  LiveLocation
    { location :: Maybe Location
location               = Maybe Location
forall a. Maybe a
Nothing
    , live_period :: Maybe Int
live_period            = Maybe Int
forall a. Maybe a
Nothing
    , heading :: Maybe Int
heading                = Maybe Int
forall a. Maybe a
Nothing
    , proximity_alert_radius :: Maybe Int
proximity_alert_radius = Maybe Int
forall a. Maybe a
Nothing
    }