module TD.Data.AuctionRound
  (AuctionRound(..)) where

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

data AuctionRound
  = AuctionRound -- ^ Describes a round of an auction
    { AuctionRound -> Maybe Int
number           :: Maybe Int -- ^ 1-based number of the round
    , AuctionRound -> Maybe Int
duration         :: Maybe Int -- ^ Duration of the round, in seconds
    , AuctionRound -> Maybe Int
extend_time      :: Maybe Int -- ^ The number of seconds for which the round will be extended if there are changes in the top winners
    , AuctionRound -> Maybe Int
top_winner_count :: Maybe Int -- ^ The number of top winners who trigger round extension if changed
    }
  deriving (AuctionRound -> AuctionRound -> Bool
(AuctionRound -> AuctionRound -> Bool)
-> (AuctionRound -> AuctionRound -> Bool) -> Eq AuctionRound
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AuctionRound -> AuctionRound -> Bool
== :: AuctionRound -> AuctionRound -> Bool
$c/= :: AuctionRound -> AuctionRound -> Bool
/= :: AuctionRound -> AuctionRound -> Bool
Eq, Int -> AuctionRound -> ShowS
[AuctionRound] -> ShowS
AuctionRound -> String
(Int -> AuctionRound -> ShowS)
-> (AuctionRound -> String)
-> ([AuctionRound] -> ShowS)
-> Show AuctionRound
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AuctionRound -> ShowS
showsPrec :: Int -> AuctionRound -> ShowS
$cshow :: AuctionRound -> String
show :: AuctionRound -> String
$cshowList :: [AuctionRound] -> ShowS
showList :: [AuctionRound] -> ShowS
Show)

instance I.ShortShow AuctionRound where
  shortShow :: AuctionRound -> String
shortShow AuctionRound
    { number :: AuctionRound -> Maybe Int
number           = Maybe Int
number_
    , duration :: AuctionRound -> Maybe Int
duration         = Maybe Int
duration_
    , extend_time :: AuctionRound -> Maybe Int
extend_time      = Maybe Int
extend_time_
    , top_winner_count :: AuctionRound -> Maybe Int
top_winner_count = Maybe Int
top_winner_count_
    }
      = String
"AuctionRound"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"number"           String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
number_
        , String
"duration"         String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
duration_
        , String
"extend_time"      String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
extend_time_
        , String
"top_winner_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
top_winner_count_
        ]

instance AT.FromJSON AuctionRound where
  parseJSON :: Value -> Parser AuctionRound
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
"auctionRound" -> Value -> Parser AuctionRound
parseAuctionRound Value
v
      String
_              -> Parser AuctionRound
forall a. Monoid a => a
mempty
    
    where
      parseAuctionRound :: A.Value -> AT.Parser AuctionRound
      parseAuctionRound :: Value -> Parser AuctionRound
parseAuctionRound = String
-> (Object -> Parser AuctionRound) -> Value -> Parser AuctionRound
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"AuctionRound" ((Object -> Parser AuctionRound) -> Value -> Parser AuctionRound)
-> (Object -> Parser AuctionRound) -> Value -> Parser AuctionRound
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
number_           <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"number"
        Maybe Int
duration_         <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"duration"
        Maybe Int
extend_time_      <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"extend_time"
        Maybe Int
top_winner_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"top_winner_count"
        AuctionRound -> Parser AuctionRound
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AuctionRound -> Parser AuctionRound)
-> AuctionRound -> Parser AuctionRound
forall a b. (a -> b) -> a -> b
$ AuctionRound
          { number :: Maybe Int
number           = Maybe Int
number_
          , duration :: Maybe Int
duration         = Maybe Int
duration_
          , extend_time :: Maybe Int
extend_time      = Maybe Int
extend_time_
          , top_winner_count :: Maybe Int
top_winner_count = Maybe Int
top_winner_count_
          }
  parseJSON Value
_ = Parser AuctionRound
forall a. Monoid a => a
mempty