module TD.Data.GameHighScores
  (GameHighScores(..)) 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.GameHighScore as GameHighScore

data GameHighScores
  = GameHighScores -- ^ Contains a list of game high scores
    { GameHighScores -> Maybe [GameHighScore]
scores :: Maybe [GameHighScore.GameHighScore] -- ^ A list of game high scores
    }
  deriving (GameHighScores -> GameHighScores -> Bool
(GameHighScores -> GameHighScores -> Bool)
-> (GameHighScores -> GameHighScores -> Bool) -> Eq GameHighScores
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GameHighScores -> GameHighScores -> Bool
== :: GameHighScores -> GameHighScores -> Bool
$c/= :: GameHighScores -> GameHighScores -> Bool
/= :: GameHighScores -> GameHighScores -> Bool
Eq, Int -> GameHighScores -> ShowS
[GameHighScores] -> ShowS
GameHighScores -> String
(Int -> GameHighScores -> ShowS)
-> (GameHighScores -> String)
-> ([GameHighScores] -> ShowS)
-> Show GameHighScores
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> GameHighScores -> ShowS
showsPrec :: Int -> GameHighScores -> ShowS
$cshow :: GameHighScores -> String
show :: GameHighScores -> String
$cshowList :: [GameHighScores] -> ShowS
showList :: [GameHighScores] -> ShowS
Show)

instance I.ShortShow GameHighScores where
  shortShow :: GameHighScores -> String
shortShow GameHighScores
    { scores :: GameHighScores -> Maybe [GameHighScore]
scores = Maybe [GameHighScore]
scores_
    }
      = String
"GameHighScores"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"scores" String -> Maybe [GameHighScore] -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe [GameHighScore]
scores_
        ]

instance AT.FromJSON GameHighScores where
  parseJSON :: Value -> Parser GameHighScores
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
"gameHighScores" -> Value -> Parser GameHighScores
parseGameHighScores Value
v
      String
_                -> Parser GameHighScores
forall a. Monoid a => a
mempty
    
    where
      parseGameHighScores :: A.Value -> AT.Parser GameHighScores
      parseGameHighScores :: Value -> Parser GameHighScores
parseGameHighScores = String
-> (Object -> Parser GameHighScores)
-> Value
-> Parser GameHighScores
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"GameHighScores" ((Object -> Parser GameHighScores)
 -> Value -> Parser GameHighScores)
-> (Object -> Parser GameHighScores)
-> Value
-> Parser GameHighScores
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe [GameHighScore]
scores_ <- Object
o Object -> Key -> Parser (Maybe [GameHighScore])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"scores"
        GameHighScores -> Parser GameHighScores
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GameHighScores -> Parser GameHighScores)
-> GameHighScores -> Parser GameHighScores
forall a b. (a -> b) -> a -> b
$ GameHighScores
          { scores :: Maybe [GameHighScore]
scores = Maybe [GameHighScore]
scores_
          }
  parseJSON Value
_ = Parser GameHighScores
forall a. Monoid a => a
mempty