module TD.Data.AffiliateType
  (AffiliateType(..)) where

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

-- | Describes type of affiliate for an affiliate program
data AffiliateType
  = AffiliateTypeCurrentUser -- ^ The affiliate is the current user
  | AffiliateTypeBot -- ^ The affiliate is a bot owned by the current user
    { AffiliateType -> Maybe Int
user_id :: Maybe Int -- ^ User identifier of the bot
    }
  | AffiliateTypeChannel -- ^ The affiliate is a channel chat where the current user has can_post_messages administrator right
    { AffiliateType -> Maybe Int
chat_id :: Maybe Int -- ^ Identifier of the channel chat
    }
  deriving (AffiliateType -> AffiliateType -> Bool
(AffiliateType -> AffiliateType -> Bool)
-> (AffiliateType -> AffiliateType -> Bool) -> Eq AffiliateType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AffiliateType -> AffiliateType -> Bool
== :: AffiliateType -> AffiliateType -> Bool
$c/= :: AffiliateType -> AffiliateType -> Bool
/= :: AffiliateType -> AffiliateType -> Bool
Eq, Int -> AffiliateType -> ShowS
[AffiliateType] -> ShowS
AffiliateType -> String
(Int -> AffiliateType -> ShowS)
-> (AffiliateType -> String)
-> ([AffiliateType] -> ShowS)
-> Show AffiliateType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AffiliateType -> ShowS
showsPrec :: Int -> AffiliateType -> ShowS
$cshow :: AffiliateType -> String
show :: AffiliateType -> String
$cshowList :: [AffiliateType] -> ShowS
showList :: [AffiliateType] -> ShowS
Show)

instance I.ShortShow AffiliateType where
  shortShow :: AffiliateType -> String
shortShow AffiliateType
AffiliateTypeCurrentUser
      = String
"AffiliateTypeCurrentUser"
  shortShow AffiliateTypeBot
    { user_id :: AffiliateType -> Maybe Int
user_id = Maybe Int
user_id_
    }
      = String
"AffiliateTypeBot"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"user_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
user_id_
        ]
  shortShow AffiliateTypeChannel
    { chat_id :: AffiliateType -> Maybe Int
chat_id = Maybe Int
chat_id_
    }
      = String
"AffiliateTypeChannel"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"chat_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
chat_id_
        ]

instance AT.FromJSON AffiliateType where
  parseJSON :: Value -> Parser AffiliateType
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
"affiliateTypeCurrentUser" -> AffiliateType -> Parser AffiliateType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure AffiliateType
AffiliateTypeCurrentUser
      String
"affiliateTypeBot"         -> Value -> Parser AffiliateType
parseAffiliateTypeBot Value
v
      String
"affiliateTypeChannel"     -> Value -> Parser AffiliateType
parseAffiliateTypeChannel Value
v
      String
_                          -> Parser AffiliateType
forall a. Monoid a => a
mempty
    
    where
      parseAffiliateTypeBot :: A.Value -> AT.Parser AffiliateType
      parseAffiliateTypeBot :: Value -> Parser AffiliateType
parseAffiliateTypeBot = String
-> (Object -> Parser AffiliateType)
-> Value
-> Parser AffiliateType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"AffiliateTypeBot" ((Object -> Parser AffiliateType) -> Value -> Parser AffiliateType)
-> (Object -> Parser AffiliateType)
-> Value
-> Parser AffiliateType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
user_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"user_id"
        AffiliateType -> Parser AffiliateType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AffiliateType -> Parser AffiliateType)
-> AffiliateType -> Parser AffiliateType
forall a b. (a -> b) -> a -> b
$ AffiliateTypeBot
          { user_id :: Maybe Int
user_id = Maybe Int
user_id_
          }
      parseAffiliateTypeChannel :: A.Value -> AT.Parser AffiliateType
      parseAffiliateTypeChannel :: Value -> Parser AffiliateType
parseAffiliateTypeChannel = String
-> (Object -> Parser AffiliateType)
-> Value
-> Parser AffiliateType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"AffiliateTypeChannel" ((Object -> Parser AffiliateType) -> Value -> Parser AffiliateType)
-> (Object -> Parser AffiliateType)
-> Value
-> Parser AffiliateType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
chat_id_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"chat_id"
        AffiliateType -> Parser AffiliateType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AffiliateType -> Parser AffiliateType)
-> AffiliateType -> Parser AffiliateType
forall a b. (a -> b) -> a -> b
$ AffiliateTypeChannel
          { chat_id :: Maybe Int
chat_id = Maybe Int
chat_id_
          }
  parseJSON Value
_ = Parser AffiliateType
forall a. Monoid a => a
mempty

instance AT.ToJSON AffiliateType where
  toJSON :: AffiliateType -> Value
toJSON AffiliateType
AffiliateTypeCurrentUser
      = [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
"affiliateTypeCurrentUser"
        ]
  toJSON AffiliateTypeBot
    { user_id :: AffiliateType -> Maybe Int
user_id = Maybe Int
user_id_
    }
      = [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
"affiliateTypeBot"
        , Key
"user_id" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
user_id_
        ]
  toJSON AffiliateTypeChannel
    { chat_id :: AffiliateType -> Maybe Int
chat_id = Maybe Int
chat_id_
    }
      = [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
"affiliateTypeChannel"
        , Key
"chat_id" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
chat_id_
        ]