module TD.Data.GiftResalePrice
  (GiftResalePrice(..)) where

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

-- | Describes price of a resold gift
data GiftResalePrice
  = GiftResalePriceStar -- ^ Describes price of a resold gift in Telegram Stars
    { GiftResalePrice -> Maybe Int
star_count :: Maybe Int -- ^ The Telegram Star amount expected to be paid for the gift. Must be in the range getOption("gift_resale_star_count_min")-getOption("gift_resale_star_count_max") for gifts put for resale
    }
  | GiftResalePriceTon -- ^ Describes price of a resold gift in Toncoins
    { GiftResalePrice -> Maybe Int
toncoin_cent_count :: Maybe Int -- ^ The amount of 1/100 of Toncoin expected to be paid for the gift. Must be in the range getOption("gift_resale_toncoin_cent_count_min")-getOption("gift_resale_toncoin_cent_count_max")
    }
  deriving (GiftResalePrice -> GiftResalePrice -> Bool
(GiftResalePrice -> GiftResalePrice -> Bool)
-> (GiftResalePrice -> GiftResalePrice -> Bool)
-> Eq GiftResalePrice
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GiftResalePrice -> GiftResalePrice -> Bool
== :: GiftResalePrice -> GiftResalePrice -> Bool
$c/= :: GiftResalePrice -> GiftResalePrice -> Bool
/= :: GiftResalePrice -> GiftResalePrice -> Bool
Eq, Int -> GiftResalePrice -> ShowS
[GiftResalePrice] -> ShowS
GiftResalePrice -> String
(Int -> GiftResalePrice -> ShowS)
-> (GiftResalePrice -> String)
-> ([GiftResalePrice] -> ShowS)
-> Show GiftResalePrice
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GiftResalePrice -> ShowS
showsPrec :: Int -> GiftResalePrice -> ShowS
$cshow :: GiftResalePrice -> String
show :: GiftResalePrice -> String
$cshowList :: [GiftResalePrice] -> ShowS
showList :: [GiftResalePrice] -> ShowS
Show)

instance I.ShortShow GiftResalePrice where
  shortShow :: GiftResalePrice -> String
shortShow GiftResalePriceStar
    { star_count :: GiftResalePrice -> Maybe Int
star_count = Maybe Int
star_count_
    }
      = String
"GiftResalePriceStar"
        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_
        ]
  shortShow GiftResalePriceTon
    { toncoin_cent_count :: GiftResalePrice -> Maybe Int
toncoin_cent_count = Maybe Int
toncoin_cent_count_
    }
      = String
"GiftResalePriceTon"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"toncoin_cent_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
toncoin_cent_count_
        ]

instance AT.FromJSON GiftResalePrice where
  parseJSON :: Value -> Parser GiftResalePrice
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
"giftResalePriceStar" -> Value -> Parser GiftResalePrice
parseGiftResalePriceStar Value
v
      String
"giftResalePriceTon"  -> Value -> Parser GiftResalePrice
parseGiftResalePriceTon Value
v
      String
_                     -> Parser GiftResalePrice
forall a. Monoid a => a
mempty
    
    where
      parseGiftResalePriceStar :: A.Value -> AT.Parser GiftResalePrice
      parseGiftResalePriceStar :: Value -> Parser GiftResalePrice
parseGiftResalePriceStar = String
-> (Object -> Parser GiftResalePrice)
-> Value
-> Parser GiftResalePrice
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"GiftResalePriceStar" ((Object -> Parser GiftResalePrice)
 -> Value -> Parser GiftResalePrice)
-> (Object -> Parser GiftResalePrice)
-> Value
-> Parser GiftResalePrice
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"
        GiftResalePrice -> Parser GiftResalePrice
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GiftResalePrice -> Parser GiftResalePrice)
-> GiftResalePrice -> Parser GiftResalePrice
forall a b. (a -> b) -> a -> b
$ GiftResalePriceStar
          { star_count :: Maybe Int
star_count = Maybe Int
star_count_
          }
      parseGiftResalePriceTon :: A.Value -> AT.Parser GiftResalePrice
      parseGiftResalePriceTon :: Value -> Parser GiftResalePrice
parseGiftResalePriceTon = String
-> (Object -> Parser GiftResalePrice)
-> Value
-> Parser GiftResalePrice
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"GiftResalePriceTon" ((Object -> Parser GiftResalePrice)
 -> Value -> Parser GiftResalePrice)
-> (Object -> Parser GiftResalePrice)
-> Value
-> Parser GiftResalePrice
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
toncoin_cent_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"toncoin_cent_count"
        GiftResalePrice -> Parser GiftResalePrice
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GiftResalePrice -> Parser GiftResalePrice)
-> GiftResalePrice -> Parser GiftResalePrice
forall a b. (a -> b) -> a -> b
$ GiftResalePriceTon
          { toncoin_cent_count :: Maybe Int
toncoin_cent_count = Maybe Int
toncoin_cent_count_
          }
  parseJSON Value
_ = Parser GiftResalePrice
forall a. Monoid a => a
mempty

instance AT.ToJSON GiftResalePrice where
  toJSON :: GiftResalePrice -> Value
toJSON GiftResalePriceStar
    { star_count :: GiftResalePrice -> Maybe Int
star_count = Maybe Int
star_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
"giftResalePriceStar"
        , Key
"star_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
star_count_
        ]
  toJSON GiftResalePriceTon
    { toncoin_cent_count :: GiftResalePrice -> Maybe Int
toncoin_cent_count = Maybe Int
toncoin_cent_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
"giftResalePriceTon"
        , Key
"toncoin_cent_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
toncoin_cent_count_
        ]