module TD.Data.GameHighScore
  (GameHighScore(..)) where

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

data GameHighScore
  = GameHighScore -- ^ Contains one row of the game high score table
    { GameHighScore -> Maybe Int
position :: Maybe Int -- ^ Position in the high score table
    , GameHighScore -> Maybe Int
user_id  :: Maybe Int -- ^ User identifier
    , GameHighScore -> Maybe Int
score    :: Maybe Int -- ^ User score
    }
  deriving (GameHighScore -> GameHighScore -> Bool
(GameHighScore -> GameHighScore -> Bool)
-> (GameHighScore -> GameHighScore -> Bool) -> Eq GameHighScore
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GameHighScore -> GameHighScore -> Bool
== :: GameHighScore -> GameHighScore -> Bool
$c/= :: GameHighScore -> GameHighScore -> Bool
/= :: GameHighScore -> GameHighScore -> Bool
Eq, Int -> GameHighScore -> ShowS
[GameHighScore] -> ShowS
GameHighScore -> String
(Int -> GameHighScore -> ShowS)
-> (GameHighScore -> String)
-> ([GameHighScore] -> ShowS)
-> Show GameHighScore
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GameHighScore -> ShowS
showsPrec :: Int -> GameHighScore -> ShowS
$cshow :: GameHighScore -> String
show :: GameHighScore -> String
$cshowList :: [GameHighScore] -> ShowS
showList :: [GameHighScore] -> ShowS
Show)

instance I.ShortShow GameHighScore where
  shortShow :: GameHighScore -> String
shortShow GameHighScore
    { position :: GameHighScore -> Maybe Int
position = Maybe Int
position_
    , user_id :: GameHighScore -> Maybe Int
user_id  = Maybe Int
user_id_
    , score :: GameHighScore -> Maybe Int
score    = Maybe Int
score_
    }
      = String
"GameHighScore"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"position" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
position_
        , String
"user_id"  String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
user_id_
        , String
"score"    String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
score_
        ]

instance AT.FromJSON GameHighScore where
  parseJSON :: Value -> Parser GameHighScore
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
"gameHighScore" -> Value -> Parser GameHighScore
parseGameHighScore Value
v
      String
_               -> Parser GameHighScore
forall a. Monoid a => a
mempty
    
    where
      parseGameHighScore :: A.Value -> AT.Parser GameHighScore
      parseGameHighScore :: Value -> Parser GameHighScore
parseGameHighScore = String
-> (Object -> Parser GameHighScore)
-> Value
-> Parser GameHighScore
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"GameHighScore" ((Object -> Parser GameHighScore) -> Value -> Parser GameHighScore)
-> (Object -> Parser GameHighScore)
-> Value
-> Parser GameHighScore
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
position_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"position"
        Maybe Int
user_id_  <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"user_id"
        Maybe Int
score_    <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"score"
        GameHighScore -> Parser GameHighScore
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GameHighScore -> Parser GameHighScore)
-> GameHighScore -> Parser GameHighScore
forall a b. (a -> b) -> a -> b
$ GameHighScore
          { position :: Maybe Int
position = Maybe Int
position_
          , user_id :: Maybe Int
user_id  = Maybe Int
user_id_
          , score :: Maybe Int
score    = Maybe Int
score_
          }
  parseJSON Value
_ = Parser GameHighScore
forall a. Monoid a => a
mempty