module TD.Data.AuctionState
  (AuctionState(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified TD.Data.AuctionBid as AuctionBid
import qualified TD.Data.AuctionRound as AuctionRound
import qualified TD.Data.UserAuctionBid as UserAuctionBid
import qualified Data.Text as T

-- | Describes state of an auction
data AuctionState
  = AuctionStateActive -- ^ Contains information about an ongoing or scheduled auction
    { AuctionState -> Maybe Int
start_date             :: Maybe Int                           -- ^ Point in time (Unix timestamp) when the auction started or will start
    , AuctionState -> Maybe Int
end_date               :: Maybe Int                           -- ^ Point in time (Unix timestamp) when the auction will be ended
    , AuctionState -> Maybe Int
min_bid                :: Maybe Int                           -- ^ The minimum possible bid in the auction in Telegram Stars
    , AuctionState -> Maybe [AuctionBid]
bid_levels             :: Maybe [AuctionBid.AuctionBid]       -- ^ A sparse list of bids that were made in the auction
    , AuctionState -> Maybe [Int]
top_bidder_user_ids    :: Maybe [Int]                         -- ^ User identifiers of at most 3 users with the biggest bids
    , AuctionState -> Maybe [AuctionRound]
rounds                 :: Maybe [AuctionRound.AuctionRound]   -- ^ Rounds of the auction in which their duration or extension rules are changed
    , AuctionState -> Maybe Int
current_round_end_date :: Maybe Int                           -- ^ Point in time (Unix timestamp) when the current round will end
    , AuctionState -> Maybe Int
current_round_number   :: Maybe Int                           -- ^ 1-based number of the current round
    , AuctionState -> Maybe Int
total_round_count      :: Maybe Int                           -- ^ The total number of rounds
    , AuctionState -> Maybe Int
distributed_item_count :: Maybe Int                           -- ^ The number of items that were purchased on the auction by all users
    , AuctionState -> Maybe Int
left_item_count        :: Maybe Int                           -- ^ The number of items that have to be distributed on the auction
    , AuctionState -> Maybe Int
acquired_item_count    :: Maybe Int                           -- ^ The number of items that were purchased by the current user on the auction
    , AuctionState -> Maybe UserAuctionBid
user_bid               :: Maybe UserAuctionBid.UserAuctionBid -- ^ Bid of the current user in the auction; may be null if none
    }
  | AuctionStateFinished -- ^ Contains information about a finished auction
    { start_date                 :: Maybe Int    -- ^ Point in time (Unix timestamp) when the auction started
    , end_date                   :: Maybe Int    -- ^ Point in time (Unix timestamp) when the auction will be ended
    , AuctionState -> Maybe Int
average_price              :: Maybe Int    -- ^ Average price of bought items in Telegram Stars
    , acquired_item_count        :: Maybe Int    -- ^ The number of items that were purchased by the current user on the auction
    , AuctionState -> Maybe Int
telegram_listed_item_count :: Maybe Int    -- ^ Number of items from the auction being resold on Telegram
    , AuctionState -> Maybe Int
fragment_listed_item_count :: Maybe Int    -- ^ Number of items from the auction being resold on Fragment
    , AuctionState -> Maybe Text
fragment_url               :: Maybe T.Text -- ^ The HTTPS link to the Fragment for the resold items; may be empty if there are no such items being sold on Fragment
    }
  deriving (AuctionState -> AuctionState -> Bool
(AuctionState -> AuctionState -> Bool)
-> (AuctionState -> AuctionState -> Bool) -> Eq AuctionState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AuctionState -> AuctionState -> Bool
== :: AuctionState -> AuctionState -> Bool
$c/= :: AuctionState -> AuctionState -> Bool
/= :: AuctionState -> AuctionState -> Bool
Eq, Int -> AuctionState -> ShowS
[AuctionState] -> ShowS
AuctionState -> String
(Int -> AuctionState -> ShowS)
-> (AuctionState -> String)
-> ([AuctionState] -> ShowS)
-> Show AuctionState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AuctionState -> ShowS
showsPrec :: Int -> AuctionState -> ShowS
$cshow :: AuctionState -> String
show :: AuctionState -> String
$cshowList :: [AuctionState] -> ShowS
showList :: [AuctionState] -> ShowS
Show)

instance I.ShortShow AuctionState where
  shortShow :: AuctionState -> String
shortShow AuctionStateActive
    { start_date :: AuctionState -> Maybe Int
start_date             = Maybe Int
start_date_
    , end_date :: AuctionState -> Maybe Int
end_date               = Maybe Int
end_date_
    , min_bid :: AuctionState -> Maybe Int
min_bid                = Maybe Int
min_bid_
    , bid_levels :: AuctionState -> Maybe [AuctionBid]
bid_levels             = Maybe [AuctionBid]
bid_levels_
    , top_bidder_user_ids :: AuctionState -> Maybe [Int]
top_bidder_user_ids    = Maybe [Int]
top_bidder_user_ids_
    , rounds :: AuctionState -> Maybe [AuctionRound]
rounds                 = Maybe [AuctionRound]
rounds_
    , current_round_end_date :: AuctionState -> Maybe Int
current_round_end_date = Maybe Int
current_round_end_date_
    , current_round_number :: AuctionState -> Maybe Int
current_round_number   = Maybe Int
current_round_number_
    , total_round_count :: AuctionState -> Maybe Int
total_round_count      = Maybe Int
total_round_count_
    , distributed_item_count :: AuctionState -> Maybe Int
distributed_item_count = Maybe Int
distributed_item_count_
    , left_item_count :: AuctionState -> Maybe Int
left_item_count        = Maybe Int
left_item_count_
    , acquired_item_count :: AuctionState -> Maybe Int
acquired_item_count    = Maybe Int
acquired_item_count_
    , user_bid :: AuctionState -> Maybe UserAuctionBid
user_bid               = Maybe UserAuctionBid
user_bid_
    }
      = String
"AuctionStateActive"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"start_date"             String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
start_date_
        , String
"end_date"               String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
end_date_
        , String
"min_bid"                String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
min_bid_
        , String
"bid_levels"             String -> Maybe [AuctionBid] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [AuctionBid]
bid_levels_
        , String
"top_bidder_user_ids"    String -> Maybe [Int] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [Int]
top_bidder_user_ids_
        , String
"rounds"                 String -> Maybe [AuctionRound] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [AuctionRound]
rounds_
        , String
"current_round_end_date" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
current_round_end_date_
        , String
"current_round_number"   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
current_round_number_
        , String
"total_round_count"      String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
total_round_count_
        , String
"distributed_item_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
distributed_item_count_
        , String
"left_item_count"        String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
left_item_count_
        , String
"acquired_item_count"    String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
acquired_item_count_
        , String
"user_bid"               String -> Maybe UserAuctionBid -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe UserAuctionBid
user_bid_
        ]
  shortShow AuctionStateFinished
    { start_date :: AuctionState -> Maybe Int
start_date                 = Maybe Int
start_date_
    , end_date :: AuctionState -> Maybe Int
end_date                   = Maybe Int
end_date_
    , average_price :: AuctionState -> Maybe Int
average_price              = Maybe Int
average_price_
    , acquired_item_count :: AuctionState -> Maybe Int
acquired_item_count        = Maybe Int
acquired_item_count_
    , telegram_listed_item_count :: AuctionState -> Maybe Int
telegram_listed_item_count = Maybe Int
telegram_listed_item_count_
    , fragment_listed_item_count :: AuctionState -> Maybe Int
fragment_listed_item_count = Maybe Int
fragment_listed_item_count_
    , fragment_url :: AuctionState -> Maybe Text
fragment_url               = Maybe Text
fragment_url_
    }
      = String
"AuctionStateFinished"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"start_date"                 String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
start_date_
        , String
"end_date"                   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
end_date_
        , String
"average_price"              String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
average_price_
        , String
"acquired_item_count"        String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
acquired_item_count_
        , String
"telegram_listed_item_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
telegram_listed_item_count_
        , String
"fragment_listed_item_count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
fragment_listed_item_count_
        , String
"fragment_url"               String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
fragment_url_
        ]

instance AT.FromJSON AuctionState where
  parseJSON :: Value -> Parser AuctionState
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
"auctionStateActive"   -> Value -> Parser AuctionState
parseAuctionStateActive Value
v
      String
"auctionStateFinished" -> Value -> Parser AuctionState
parseAuctionStateFinished Value
v
      String
_                      -> Parser AuctionState
forall a. Monoid a => a
mempty
    
    where
      parseAuctionStateActive :: A.Value -> AT.Parser AuctionState
      parseAuctionStateActive :: Value -> Parser AuctionState
parseAuctionStateActive = String
-> (Object -> Parser AuctionState) -> Value -> Parser AuctionState
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"AuctionStateActive" ((Object -> Parser AuctionState) -> Value -> Parser AuctionState)
-> (Object -> Parser AuctionState) -> Value -> Parser AuctionState
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
start_date_             <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"start_date"
        Maybe Int
end_date_               <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"end_date"
        Maybe Int
min_bid_                <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"min_bid"
        Maybe [AuctionBid]
bid_levels_             <- Object
o Object -> Key -> Parser (Maybe [AuctionBid])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"bid_levels"
        Maybe [Int]
top_bidder_user_ids_    <- Object
o Object -> Key -> Parser (Maybe [Int])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"top_bidder_user_ids"
        Maybe [AuctionRound]
rounds_                 <- Object
o Object -> Key -> Parser (Maybe [AuctionRound])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"rounds"
        Maybe Int
current_round_end_date_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"current_round_end_date"
        Maybe Int
current_round_number_   <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"current_round_number"
        Maybe Int
total_round_count_      <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"total_round_count"
        Maybe Int
distributed_item_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"distributed_item_count"
        Maybe Int
left_item_count_        <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"left_item_count"
        Maybe Int
acquired_item_count_    <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"acquired_item_count"
        Maybe UserAuctionBid
user_bid_               <- Object
o Object -> Key -> Parser (Maybe UserAuctionBid)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"user_bid"
        AuctionState -> Parser AuctionState
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AuctionState -> Parser AuctionState)
-> AuctionState -> Parser AuctionState
forall a b. (a -> b) -> a -> b
$ AuctionStateActive
          { start_date :: Maybe Int
start_date             = Maybe Int
start_date_
          , end_date :: Maybe Int
end_date               = Maybe Int
end_date_
          , min_bid :: Maybe Int
min_bid                = Maybe Int
min_bid_
          , bid_levels :: Maybe [AuctionBid]
bid_levels             = Maybe [AuctionBid]
bid_levels_
          , top_bidder_user_ids :: Maybe [Int]
top_bidder_user_ids    = Maybe [Int]
top_bidder_user_ids_
          , rounds :: Maybe [AuctionRound]
rounds                 = Maybe [AuctionRound]
rounds_
          , current_round_end_date :: Maybe Int
current_round_end_date = Maybe Int
current_round_end_date_
          , current_round_number :: Maybe Int
current_round_number   = Maybe Int
current_round_number_
          , total_round_count :: Maybe Int
total_round_count      = Maybe Int
total_round_count_
          , distributed_item_count :: Maybe Int
distributed_item_count = Maybe Int
distributed_item_count_
          , left_item_count :: Maybe Int
left_item_count        = Maybe Int
left_item_count_
          , acquired_item_count :: Maybe Int
acquired_item_count    = Maybe Int
acquired_item_count_
          , user_bid :: Maybe UserAuctionBid
user_bid               = Maybe UserAuctionBid
user_bid_
          }
      parseAuctionStateFinished :: A.Value -> AT.Parser AuctionState
      parseAuctionStateFinished :: Value -> Parser AuctionState
parseAuctionStateFinished = String
-> (Object -> Parser AuctionState) -> Value -> Parser AuctionState
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"AuctionStateFinished" ((Object -> Parser AuctionState) -> Value -> Parser AuctionState)
-> (Object -> Parser AuctionState) -> Value -> Parser AuctionState
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
start_date_                 <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"start_date"
        Maybe Int
end_date_                   <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"end_date"
        Maybe Int
average_price_              <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"average_price"
        Maybe Int
acquired_item_count_        <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"acquired_item_count"
        Maybe Int
telegram_listed_item_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"telegram_listed_item_count"
        Maybe Int
fragment_listed_item_count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"fragment_listed_item_count"
        Maybe Text
fragment_url_               <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"fragment_url"
        AuctionState -> Parser AuctionState
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AuctionState -> Parser AuctionState)
-> AuctionState -> Parser AuctionState
forall a b. (a -> b) -> a -> b
$ AuctionStateFinished
          { start_date :: Maybe Int
start_date                 = Maybe Int
start_date_
          , end_date :: Maybe Int
end_date                   = Maybe Int
end_date_
          , average_price :: Maybe Int
average_price              = Maybe Int
average_price_
          , acquired_item_count :: Maybe Int
acquired_item_count        = Maybe Int
acquired_item_count_
          , telegram_listed_item_count :: Maybe Int
telegram_listed_item_count = Maybe Int
telegram_listed_item_count_
          , fragment_listed_item_count :: Maybe Int
fragment_listed_item_count = Maybe Int
fragment_listed_item_count_
          , fragment_url :: Maybe Text
fragment_url               = Maybe Text
fragment_url_
          }
  parseJSON Value
_ = Parser AuctionState
forall a. Monoid a => a
mempty