module TD.Data.Proxy
  (Proxy(..)) 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
import qualified TD.Data.ProxyType as ProxyType

data Proxy
  = Proxy -- ^ Contains information about a proxy server
    { Proxy -> Maybe Int
_id            :: Maybe Int                 -- ^ Unique identifier of the proxy
    , Proxy -> Maybe Text
server         :: Maybe T.Text              -- ^ Proxy server domain or IP address
    , Proxy -> Maybe Int
port           :: Maybe Int                 -- ^ Proxy server port
    , Proxy -> Maybe Int
last_used_date :: Maybe Int                 -- ^ Point in time (Unix timestamp) when the proxy was last used; 0 if never
    , Proxy -> Maybe Bool
is_enabled     :: Maybe Bool                -- ^ True, if the proxy is enabled now
    , Proxy -> Maybe ProxyType
_type          :: Maybe ProxyType.ProxyType -- ^ Type of the proxy
    }
  deriving (Proxy -> Proxy -> Bool
(Proxy -> Proxy -> Bool) -> (Proxy -> Proxy -> Bool) -> Eq Proxy
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Proxy -> Proxy -> Bool
== :: Proxy -> Proxy -> Bool
$c/= :: Proxy -> Proxy -> Bool
/= :: Proxy -> Proxy -> Bool
Eq, Int -> Proxy -> ShowS
[Proxy] -> ShowS
Proxy -> String
(Int -> Proxy -> ShowS)
-> (Proxy -> String) -> ([Proxy] -> ShowS) -> Show Proxy
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Proxy -> ShowS
showsPrec :: Int -> Proxy -> ShowS
$cshow :: Proxy -> String
show :: Proxy -> String
$cshowList :: [Proxy] -> ShowS
showList :: [Proxy] -> ShowS
Show)

instance I.ShortShow Proxy where
  shortShow :: Proxy -> String
shortShow Proxy
    { _id :: Proxy -> Maybe Int
_id            = Maybe Int
_id_
    , server :: Proxy -> Maybe Text
server         = Maybe Text
server_
    , port :: Proxy -> Maybe Int
port           = Maybe Int
port_
    , last_used_date :: Proxy -> Maybe Int
last_used_date = Maybe Int
last_used_date_
    , is_enabled :: Proxy -> Maybe Bool
is_enabled     = Maybe Bool
is_enabled_
    , _type :: Proxy -> Maybe ProxyType
_type          = Maybe ProxyType
_type_
    }
      = String
"Proxy"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_id"            String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
_id_
        , String
"server"         String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
server_
        , String
"port"           String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
port_
        , String
"last_used_date" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
last_used_date_
        , String
"is_enabled"     String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
is_enabled_
        , String
"_type"          String -> Maybe ProxyType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ProxyType
_type_
        ]

instance AT.FromJSON Proxy where
  parseJSON :: Value -> Parser Proxy
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
"proxy" -> Value -> Parser Proxy
parseProxy Value
v
      String
_       -> Parser Proxy
forall a. Monoid a => a
mempty
    
    where
      parseProxy :: A.Value -> AT.Parser Proxy
      parseProxy :: Value -> Parser Proxy
parseProxy = String -> (Object -> Parser Proxy) -> Value -> Parser Proxy
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Proxy" ((Object -> Parser Proxy) -> Value -> Parser Proxy)
-> (Object -> Parser Proxy) -> Value -> Parser Proxy
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_id_            <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        Maybe Text
server_         <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"server"
        Maybe Int
port_           <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"port"
        Maybe Int
last_used_date_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"last_used_date"
        Maybe Bool
is_enabled_     <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"is_enabled"
        Maybe ProxyType
_type_          <- Object
o Object -> Key -> Parser (Maybe ProxyType)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"type"
        Proxy -> Parser Proxy
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Proxy -> Parser Proxy) -> Proxy -> Parser Proxy
forall a b. (a -> b) -> a -> b
$ Proxy
          { _id :: Maybe Int
_id            = Maybe Int
_id_
          , server :: Maybe Text
server         = Maybe Text
server_
          , port :: Maybe Int
port           = Maybe Int
port_
          , last_used_date :: Maybe Int
last_used_date = Maybe Int
last_used_date_
          , is_enabled :: Maybe Bool
is_enabled     = Maybe Bool
is_enabled_
          , _type :: Maybe ProxyType
_type          = Maybe ProxyType
_type_
          }
  parseJSON Value
_ = Parser Proxy
forall a. Monoid a => a
mempty