module TD.Data.NetworkType
  (NetworkType(..)) where

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

-- | Represents the type of network
data NetworkType
  = NetworkTypeNone -- ^ The network is not available
  | NetworkTypeMobile -- ^ A mobile network
  | NetworkTypeMobileRoaming -- ^ A mobile roaming network
  | NetworkTypeWiFi -- ^ A Wi-Fi network
  | NetworkTypeOther -- ^ A different network type (e.g., Ethernet network)
  deriving (NetworkType -> NetworkType -> Bool
(NetworkType -> NetworkType -> Bool)
-> (NetworkType -> NetworkType -> Bool) -> Eq NetworkType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NetworkType -> NetworkType -> Bool
== :: NetworkType -> NetworkType -> Bool
$c/= :: NetworkType -> NetworkType -> Bool
/= :: NetworkType -> NetworkType -> Bool
Eq, Int -> NetworkType -> ShowS
[NetworkType] -> ShowS
NetworkType -> String
(Int -> NetworkType -> ShowS)
-> (NetworkType -> String)
-> ([NetworkType] -> ShowS)
-> Show NetworkType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NetworkType -> ShowS
showsPrec :: Int -> NetworkType -> ShowS
$cshow :: NetworkType -> String
show :: NetworkType -> String
$cshowList :: [NetworkType] -> ShowS
showList :: [NetworkType] -> ShowS
Show)

instance I.ShortShow NetworkType where
  shortShow :: NetworkType -> String
shortShow NetworkType
NetworkTypeNone
      = String
"NetworkTypeNone"
  shortShow NetworkType
NetworkTypeMobile
      = String
"NetworkTypeMobile"
  shortShow NetworkType
NetworkTypeMobileRoaming
      = String
"NetworkTypeMobileRoaming"
  shortShow NetworkType
NetworkTypeWiFi
      = String
"NetworkTypeWiFi"
  shortShow NetworkType
NetworkTypeOther
      = String
"NetworkTypeOther"

instance AT.FromJSON NetworkType where
  parseJSON :: Value -> Parser NetworkType
parseJSON (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
"networkTypeNone"          -> NetworkType -> Parser NetworkType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NetworkType
NetworkTypeNone
      String
"networkTypeMobile"        -> NetworkType -> Parser NetworkType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NetworkType
NetworkTypeMobile
      String
"networkTypeMobileRoaming" -> NetworkType -> Parser NetworkType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NetworkType
NetworkTypeMobileRoaming
      String
"networkTypeWiFi"          -> NetworkType -> Parser NetworkType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NetworkType
NetworkTypeWiFi
      String
"networkTypeOther"         -> NetworkType -> Parser NetworkType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NetworkType
NetworkTypeOther
      String
_                          -> Parser NetworkType
forall a. Monoid a => a
mempty
    
  parseJSON Value
_ = Parser NetworkType
forall a. Monoid a => a
mempty

instance AT.ToJSON NetworkType where
  toJSON :: NetworkType -> Value
toJSON NetworkType
NetworkTypeNone
      = [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
"networkTypeNone"
        ]
  toJSON NetworkType
NetworkTypeMobile
      = [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
"networkTypeMobile"
        ]
  toJSON NetworkType
NetworkTypeMobileRoaming
      = [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
"networkTypeMobileRoaming"
        ]
  toJSON NetworkType
NetworkTypeWiFi
      = [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
"networkTypeWiFi"
        ]
  toJSON NetworkType
NetworkTypeOther
      = [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
"networkTypeOther"
        ]