module TD.Data.SuggestedPostPrice
  (SuggestedPostPrice(..)) 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 suggested post
data SuggestedPostPrice
  = SuggestedPostPriceStar -- ^ Describes price of a suggested post in Telegram Stars
    { SuggestedPostPrice -> Maybe Int
star_count :: Maybe Int -- ^ The Telegram Star amount expected to be paid for the post; getOption("suggested_post_star_count_min")-getOption("suggested_post_star_count_max")
    }
  | SuggestedPostPriceTon -- ^ Describes price of a suggested post in Toncoins
    { SuggestedPostPrice -> Maybe Int
toncoin_cent_count :: Maybe Int -- ^ The amount of 1/100 of Toncoin expected to be paid for the post; getOption("suggested_post_toncoin_cent_count_min")-getOption("suggested_post_toncoin_cent_count_max")
    }
  deriving (SuggestedPostPrice -> SuggestedPostPrice -> Bool
(SuggestedPostPrice -> SuggestedPostPrice -> Bool)
-> (SuggestedPostPrice -> SuggestedPostPrice -> Bool)
-> Eq SuggestedPostPrice
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SuggestedPostPrice -> SuggestedPostPrice -> Bool
== :: SuggestedPostPrice -> SuggestedPostPrice -> Bool
$c/= :: SuggestedPostPrice -> SuggestedPostPrice -> Bool
/= :: SuggestedPostPrice -> SuggestedPostPrice -> Bool
Eq, Int -> SuggestedPostPrice -> ShowS
[SuggestedPostPrice] -> ShowS
SuggestedPostPrice -> String
(Int -> SuggestedPostPrice -> ShowS)
-> (SuggestedPostPrice -> String)
-> ([SuggestedPostPrice] -> ShowS)
-> Show SuggestedPostPrice
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SuggestedPostPrice -> ShowS
showsPrec :: Int -> SuggestedPostPrice -> ShowS
$cshow :: SuggestedPostPrice -> String
show :: SuggestedPostPrice -> String
$cshowList :: [SuggestedPostPrice] -> ShowS
showList :: [SuggestedPostPrice] -> ShowS
Show)

instance I.ShortShow SuggestedPostPrice where
  shortShow :: SuggestedPostPrice -> String
shortShow SuggestedPostPriceStar
    { star_count :: SuggestedPostPrice -> Maybe Int
star_count = Maybe Int
star_count_
    }
      = String
"SuggestedPostPriceStar"
        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 SuggestedPostPriceTon
    { toncoin_cent_count :: SuggestedPostPrice -> Maybe Int
toncoin_cent_count = Maybe Int
toncoin_cent_count_
    }
      = String
"SuggestedPostPriceTon"
        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 SuggestedPostPrice where
  parseJSON :: Value -> Parser SuggestedPostPrice
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
"suggestedPostPriceStar" -> Value -> Parser SuggestedPostPrice
parseSuggestedPostPriceStar Value
v
      String
"suggestedPostPriceTon"  -> Value -> Parser SuggestedPostPrice
parseSuggestedPostPriceTon Value
v
      String
_                        -> Parser SuggestedPostPrice
forall a. Monoid a => a
mempty
    
    where
      parseSuggestedPostPriceStar :: A.Value -> AT.Parser SuggestedPostPrice
      parseSuggestedPostPriceStar :: Value -> Parser SuggestedPostPrice
parseSuggestedPostPriceStar = String
-> (Object -> Parser SuggestedPostPrice)
-> Value
-> Parser SuggestedPostPrice
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"SuggestedPostPriceStar" ((Object -> Parser SuggestedPostPrice)
 -> Value -> Parser SuggestedPostPrice)
-> (Object -> Parser SuggestedPostPrice)
-> Value
-> Parser SuggestedPostPrice
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"
        SuggestedPostPrice -> Parser SuggestedPostPrice
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SuggestedPostPrice -> Parser SuggestedPostPrice)
-> SuggestedPostPrice -> Parser SuggestedPostPrice
forall a b. (a -> b) -> a -> b
$ SuggestedPostPriceStar
          { star_count :: Maybe Int
star_count = Maybe Int
star_count_
          }
      parseSuggestedPostPriceTon :: A.Value -> AT.Parser SuggestedPostPrice
      parseSuggestedPostPriceTon :: Value -> Parser SuggestedPostPrice
parseSuggestedPostPriceTon = String
-> (Object -> Parser SuggestedPostPrice)
-> Value
-> Parser SuggestedPostPrice
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"SuggestedPostPriceTon" ((Object -> Parser SuggestedPostPrice)
 -> Value -> Parser SuggestedPostPrice)
-> (Object -> Parser SuggestedPostPrice)
-> Value
-> Parser SuggestedPostPrice
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"
        SuggestedPostPrice -> Parser SuggestedPostPrice
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SuggestedPostPrice -> Parser SuggestedPostPrice)
-> SuggestedPostPrice -> Parser SuggestedPostPrice
forall a b. (a -> b) -> a -> b
$ SuggestedPostPriceTon
          { toncoin_cent_count :: Maybe Int
toncoin_cent_count = Maybe Int
toncoin_cent_count_
          }
  parseJSON Value
_ = Parser SuggestedPostPrice
forall a. Monoid a => a
mempty

instance AT.ToJSON SuggestedPostPrice where
  toJSON :: SuggestedPostPrice -> Value
toJSON SuggestedPostPriceStar
    { star_count :: SuggestedPostPrice -> 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
"suggestedPostPriceStar"
        , 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 SuggestedPostPriceTon
    { toncoin_cent_count :: SuggestedPostPrice -> 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
"suggestedPostPriceTon"
        , 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_
        ]