module TD.Data.PremiumLimit
  (PremiumLimit(..)) 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.PremiumLimitType as PremiumLimitType

data PremiumLimit
  = PremiumLimit -- ^ Contains information about a limit, increased for Premium users
    { PremiumLimit -> Maybe PremiumLimitType
_type         :: Maybe PremiumLimitType.PremiumLimitType -- ^ The type of the limit
    , PremiumLimit -> Maybe Int
default_value :: Maybe Int                               -- ^ Default value of the limit
    , PremiumLimit -> Maybe Int
premium_value :: Maybe Int                               -- ^ Value of the limit for Premium users
    }
  deriving (PremiumLimit -> PremiumLimit -> Bool
(PremiumLimit -> PremiumLimit -> Bool)
-> (PremiumLimit -> PremiumLimit -> Bool) -> Eq PremiumLimit
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PremiumLimit -> PremiumLimit -> Bool
== :: PremiumLimit -> PremiumLimit -> Bool
$c/= :: PremiumLimit -> PremiumLimit -> Bool
/= :: PremiumLimit -> PremiumLimit -> Bool
Eq, Int -> PremiumLimit -> ShowS
[PremiumLimit] -> ShowS
PremiumLimit -> String
(Int -> PremiumLimit -> ShowS)
-> (PremiumLimit -> String)
-> ([PremiumLimit] -> ShowS)
-> Show PremiumLimit
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PremiumLimit -> ShowS
showsPrec :: Int -> PremiumLimit -> ShowS
$cshow :: PremiumLimit -> String
show :: PremiumLimit -> String
$cshowList :: [PremiumLimit] -> ShowS
showList :: [PremiumLimit] -> ShowS
Show)

instance I.ShortShow PremiumLimit where
  shortShow :: PremiumLimit -> String
shortShow PremiumLimit
    { _type :: PremiumLimit -> Maybe PremiumLimitType
_type         = Maybe PremiumLimitType
_type_
    , default_value :: PremiumLimit -> Maybe Int
default_value = Maybe Int
default_value_
    , premium_value :: PremiumLimit -> Maybe Int
premium_value = Maybe Int
premium_value_
    }
      = String
"PremiumLimit"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_type"         String -> Maybe PremiumLimitType -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe PremiumLimitType
_type_
        , String
"default_value" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
default_value_
        , String
"premium_value" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
premium_value_
        ]

instance AT.FromJSON PremiumLimit where
  parseJSON :: Value -> Parser PremiumLimit
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
"premiumLimit" -> Value -> Parser PremiumLimit
parsePremiumLimit Value
v
      String
_              -> Parser PremiumLimit
forall a. Monoid a => a
mempty
    
    where
      parsePremiumLimit :: A.Value -> AT.Parser PremiumLimit
      parsePremiumLimit :: Value -> Parser PremiumLimit
parsePremiumLimit = String
-> (Object -> Parser PremiumLimit) -> Value -> Parser PremiumLimit
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PremiumLimit" ((Object -> Parser PremiumLimit) -> Value -> Parser PremiumLimit)
-> (Object -> Parser PremiumLimit) -> Value -> Parser PremiumLimit
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe PremiumLimitType
_type_         <- Object
o Object -> Key -> Parser (Maybe PremiumLimitType)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"type"
        Maybe Int
default_value_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"default_value"
        Maybe Int
premium_value_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"premium_value"
        PremiumLimit -> Parser PremiumLimit
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PremiumLimit -> Parser PremiumLimit)
-> PremiumLimit -> Parser PremiumLimit
forall a b. (a -> b) -> a -> b
$ PremiumLimit
          { _type :: Maybe PremiumLimitType
_type         = Maybe PremiumLimitType
_type_
          , default_value :: Maybe Int
default_value = Maybe Int
default_value_
          , premium_value :: Maybe Int
premium_value = Maybe Int
premium_value_
          }
  parseJSON Value
_ = Parser PremiumLimit
forall a. Monoid a => a
mempty