module TD.Data.GiveawayPrize
  (GiveawayPrize(..)) where

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

-- | Contains information about a giveaway prize
data GiveawayPrize
  = GiveawayPrizePremium -- ^ The giveaway sends Telegram Premium subscriptions to the winners
    { GiveawayPrize -> Maybe Int
month_count :: Maybe Int -- ^ Number of months the Telegram Premium subscription will be active after code activation
    }
  | GiveawayPrizeStars -- ^ The giveaway sends Telegram Stars to the winners
    { GiveawayPrize -> Maybe Int
star_count :: Maybe Int -- ^ Number of Telegram Stars that will be shared by all winners
    }
  deriving (GiveawayPrize -> GiveawayPrize -> Bool
(GiveawayPrize -> GiveawayPrize -> Bool)
-> (GiveawayPrize -> GiveawayPrize -> Bool) -> Eq GiveawayPrize
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GiveawayPrize -> GiveawayPrize -> Bool
== :: GiveawayPrize -> GiveawayPrize -> Bool
$c/= :: GiveawayPrize -> GiveawayPrize -> Bool
/= :: GiveawayPrize -> GiveawayPrize -> Bool
Eq, Int -> GiveawayPrize -> ShowS
[GiveawayPrize] -> ShowS
GiveawayPrize -> String
(Int -> GiveawayPrize -> ShowS)
-> (GiveawayPrize -> String)
-> ([GiveawayPrize] -> ShowS)
-> Show GiveawayPrize
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GiveawayPrize -> ShowS
showsPrec :: Int -> GiveawayPrize -> ShowS
$cshow :: GiveawayPrize -> String
show :: GiveawayPrize -> String
$cshowList :: [GiveawayPrize] -> ShowS
showList :: [GiveawayPrize] -> ShowS
Show)

instance I.ShortShow GiveawayPrize where
  shortShow :: GiveawayPrize -> String
shortShow GiveawayPrizePremium
    { month_count :: GiveawayPrize -> Maybe Int
month_count = Maybe Int
month_count_
    }
      = String
"GiveawayPrizePremium"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"month_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
month_count_
        ]
  shortShow GiveawayPrizeStars
    { star_count :: GiveawayPrize -> Maybe Int
star_count = Maybe Int
star_count_
    }
      = String
"GiveawayPrizeStars"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"star_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
star_count_
        ]

instance AT.FromJSON GiveawayPrize where
  parseJSON :: Value -> Parser GiveawayPrize
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
"giveawayPrizePremium" -> Value -> Parser GiveawayPrize
parseGiveawayPrizePremium Value
v
      String
"giveawayPrizeStars"   -> Value -> Parser GiveawayPrize
parseGiveawayPrizeStars Value
v
      String
_                      -> Parser GiveawayPrize
forall a. Monoid a => a
mempty
    
    where
      parseGiveawayPrizePremium :: A.Value -> AT.Parser GiveawayPrize
      parseGiveawayPrizePremium :: Value -> Parser GiveawayPrize
parseGiveawayPrizePremium = String
-> (Object -> Parser GiveawayPrize)
-> Value
-> Parser GiveawayPrize
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"GiveawayPrizePremium" ((Object -> Parser GiveawayPrize) -> Value -> Parser GiveawayPrize)
-> (Object -> Parser GiveawayPrize)
-> Value
-> Parser GiveawayPrize
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
month_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"month_count"
        GiveawayPrize -> Parser GiveawayPrize
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GiveawayPrize -> Parser GiveawayPrize)
-> GiveawayPrize -> Parser GiveawayPrize
forall a b. (a -> b) -> a -> b
$ GiveawayPrizePremium
          { month_count :: Maybe Int
month_count = Maybe Int
month_count_
          }
      parseGiveawayPrizeStars :: A.Value -> AT.Parser GiveawayPrize
      parseGiveawayPrizeStars :: Value -> Parser GiveawayPrize
parseGiveawayPrizeStars = String
-> (Object -> Parser GiveawayPrize)
-> Value
-> Parser GiveawayPrize
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"GiveawayPrizeStars" ((Object -> Parser GiveawayPrize) -> Value -> Parser GiveawayPrize)
-> (Object -> Parser GiveawayPrize)
-> Value
-> Parser GiveawayPrize
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
star_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"star_count"
        GiveawayPrize -> Parser GiveawayPrize
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GiveawayPrize -> Parser GiveawayPrize)
-> GiveawayPrize -> Parser GiveawayPrize
forall a b. (a -> b) -> a -> b
$ GiveawayPrizeStars
          { star_count :: Maybe Int
star_count = Maybe Int
star_count_
          }
  parseJSON Value
_ = Parser GiveawayPrize
forall a. Monoid a => a
mempty