module TD.Data.AffiliateProgramParameters
  ( AffiliateProgramParameters(..)    
  , defaultAffiliateProgramParameters 
  ) where

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

data AffiliateProgramParameters
  = AffiliateProgramParameters -- ^ Describes parameters of an affiliate program
    { AffiliateProgramParameters -> Maybe Int
commission_per_mille :: Maybe Int -- ^ The number of Telegram Stars received by the affiliate for each 1000 Telegram Stars received by the program owner; getOption("affiliate_program_commission_per_mille_min")-getOption("affiliate_program_commission_per_mille_max")
    , AffiliateProgramParameters -> Maybe Int
month_count          :: Maybe Int -- ^ Number of months the program will be active; 0-36. If 0, then the program is eternal
    }
  deriving (AffiliateProgramParameters -> AffiliateProgramParameters -> Bool
(AffiliateProgramParameters -> AffiliateProgramParameters -> Bool)
-> (AffiliateProgramParameters
    -> AffiliateProgramParameters -> Bool)
-> Eq AffiliateProgramParameters
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AffiliateProgramParameters -> AffiliateProgramParameters -> Bool
== :: AffiliateProgramParameters -> AffiliateProgramParameters -> Bool
$c/= :: AffiliateProgramParameters -> AffiliateProgramParameters -> Bool
/= :: AffiliateProgramParameters -> AffiliateProgramParameters -> Bool
Eq, Int -> AffiliateProgramParameters -> ShowS
[AffiliateProgramParameters] -> ShowS
AffiliateProgramParameters -> String
(Int -> AffiliateProgramParameters -> ShowS)
-> (AffiliateProgramParameters -> String)
-> ([AffiliateProgramParameters] -> ShowS)
-> Show AffiliateProgramParameters
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AffiliateProgramParameters -> ShowS
showsPrec :: Int -> AffiliateProgramParameters -> ShowS
$cshow :: AffiliateProgramParameters -> String
show :: AffiliateProgramParameters -> String
$cshowList :: [AffiliateProgramParameters] -> ShowS
showList :: [AffiliateProgramParameters] -> ShowS
Show)

instance I.ShortShow AffiliateProgramParameters where
  shortShow :: AffiliateProgramParameters -> String
shortShow AffiliateProgramParameters
    { commission_per_mille :: AffiliateProgramParameters -> Maybe Int
commission_per_mille = Maybe Int
commission_per_mille_
    , month_count :: AffiliateProgramParameters -> Maybe Int
month_count          = Maybe Int
month_count_
    }
      = String
"AffiliateProgramParameters"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"commission_per_mille" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
commission_per_mille_
        , String
"month_count"          String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
month_count_
        ]

instance AT.FromJSON AffiliateProgramParameters where
  parseJSON :: Value -> Parser AffiliateProgramParameters
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
"affiliateProgramParameters" -> Value -> Parser AffiliateProgramParameters
parseAffiliateProgramParameters Value
v
      String
_                            -> Parser AffiliateProgramParameters
forall a. Monoid a => a
mempty
    
    where
      parseAffiliateProgramParameters :: A.Value -> AT.Parser AffiliateProgramParameters
      parseAffiliateProgramParameters :: Value -> Parser AffiliateProgramParameters
parseAffiliateProgramParameters = String
-> (Object -> Parser AffiliateProgramParameters)
-> Value
-> Parser AffiliateProgramParameters
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"AffiliateProgramParameters" ((Object -> Parser AffiliateProgramParameters)
 -> Value -> Parser AffiliateProgramParameters)
-> (Object -> Parser AffiliateProgramParameters)
-> Value
-> Parser AffiliateProgramParameters
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
commission_per_mille_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"commission_per_mille"
        Maybe Int
month_count_          <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"month_count"
        AffiliateProgramParameters -> Parser AffiliateProgramParameters
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AffiliateProgramParameters -> Parser AffiliateProgramParameters)
-> AffiliateProgramParameters -> Parser AffiliateProgramParameters
forall a b. (a -> b) -> a -> b
$ AffiliateProgramParameters
          { commission_per_mille :: Maybe Int
commission_per_mille = Maybe Int
commission_per_mille_
          , month_count :: Maybe Int
month_count          = Maybe Int
month_count_
          }
  parseJSON Value
_ = Parser AffiliateProgramParameters
forall a. Monoid a => a
mempty

instance AT.ToJSON AffiliateProgramParameters where
  toJSON :: AffiliateProgramParameters -> Value
toJSON AffiliateProgramParameters
    { commission_per_mille :: AffiliateProgramParameters -> Maybe Int
commission_per_mille = Maybe Int
commission_per_mille_
    , month_count :: AffiliateProgramParameters -> Maybe Int
month_count          = Maybe Int
month_count_
    }
      = [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
"affiliateProgramParameters"
        , Key
"commission_per_mille" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
commission_per_mille_
        , Key
"month_count"          Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
month_count_
        ]

defaultAffiliateProgramParameters :: AffiliateProgramParameters
defaultAffiliateProgramParameters :: AffiliateProgramParameters
defaultAffiliateProgramParameters =
  AffiliateProgramParameters
    { commission_per_mille :: Maybe Int
commission_per_mille = Maybe Int
forall a. Maybe a
Nothing
    , month_count :: Maybe Int
month_count          = Maybe Int
forall a. Maybe a
Nothing
    }