module TD.Data.Game
  (Game(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified Data.Text as T
import qualified TD.Data.FormattedText as FormattedText
import qualified TD.Data.Photo as Photo
import qualified TD.Data.Animation as Animation

data Game
  = Game -- ^ Describes a game. Use getInternalLink with internalLinkTypeGame to share the game
    { Game -> Maybe Int
_id         :: Maybe Int                         -- ^ Unique game identifier
    , Game -> Maybe Text
short_name  :: Maybe T.Text                      -- ^ Game short name
    , Game -> Maybe Text
title       :: Maybe T.Text                      -- ^ Game title
    , Game -> Maybe FormattedText
text        :: Maybe FormattedText.FormattedText -- ^ Game text, usually containing scoreboards for a game
    , Game -> Maybe Text
description :: Maybe T.Text                      -- ^ Game description
    , Game -> Maybe Photo
photo       :: Maybe Photo.Photo                 -- ^ Game photo
    , Game -> Maybe Animation
animation   :: Maybe Animation.Animation         -- ^ Game animation; may be null
    }
  deriving (Game -> Game -> Bool
(Game -> Game -> Bool) -> (Game -> Game -> Bool) -> Eq Game
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Game -> Game -> Bool
== :: Game -> Game -> Bool
$c/= :: Game -> Game -> Bool
/= :: Game -> Game -> Bool
Eq, Int -> Game -> ShowS
[Game] -> ShowS
Game -> String
(Int -> Game -> ShowS)
-> (Game -> String) -> ([Game] -> ShowS) -> Show Game
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Game -> ShowS
showsPrec :: Int -> Game -> ShowS
$cshow :: Game -> String
show :: Game -> String
$cshowList :: [Game] -> ShowS
showList :: [Game] -> ShowS
Show)

instance I.ShortShow Game where
  shortShow :: Game -> String
shortShow Game
    { _id :: Game -> Maybe Int
_id         = Maybe Int
_id_
    , short_name :: Game -> Maybe Text
short_name  = Maybe Text
short_name_
    , title :: Game -> Maybe Text
title       = Maybe Text
title_
    , text :: Game -> Maybe FormattedText
text        = Maybe FormattedText
text_
    , description :: Game -> Maybe Text
description = Maybe Text
description_
    , photo :: Game -> Maybe Photo
photo       = Maybe Photo
photo_
    , animation :: Game -> Maybe Animation
animation   = Maybe Animation
animation_
    }
      = String
"Game"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_id"         String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
_id_
        , String
"short_name"  String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
short_name_
        , String
"title"       String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
title_
        , String
"text"        String -> Maybe FormattedText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe FormattedText
text_
        , String
"description" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
description_
        , String
"photo"       String -> Maybe Photo -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Photo
photo_
        , String
"animation"   String -> Maybe Animation -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Animation
animation_
        ]

instance AT.FromJSON Game where
  parseJSON :: Value -> Parser Game
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
"game" -> Value -> Parser Game
parseGame Value
v
      String
_      -> Parser Game
forall a. Monoid a => a
mempty
    
    where
      parseGame :: A.Value -> AT.Parser Game
      parseGame :: Value -> Parser Game
parseGame = String -> (Object -> Parser Game) -> Value -> Parser Game
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Game" ((Object -> Parser Game) -> Value -> Parser Game)
-> (Object -> Parser Game) -> Value -> Parser Game
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
_id_         <- (String -> Int) -> Maybe String -> Maybe Int
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Int
I.readInt64 (Maybe String -> Maybe Int)
-> Parser (Maybe String) -> Parser (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        Maybe Text
short_name_  <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"short_name"
        Maybe Text
title_       <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"title"
        Maybe FormattedText
text_        <- Object
o Object -> Key -> Parser (Maybe FormattedText)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"text"
        Maybe Text
description_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"description"
        Maybe Photo
photo_       <- Object
o Object -> Key -> Parser (Maybe Photo)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"photo"
        Maybe Animation
animation_   <- Object
o Object -> Key -> Parser (Maybe Animation)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?                       Key
"animation"
        Game -> Parser Game
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Game -> Parser Game) -> Game -> Parser Game
forall a b. (a -> b) -> a -> b
$ Game
          { _id :: Maybe Int
_id         = Maybe Int
_id_
          , short_name :: Maybe Text
short_name  = Maybe Text
short_name_
          , title :: Maybe Text
title       = Maybe Text
title_
          , text :: Maybe FormattedText
text        = Maybe FormattedText
text_
          , description :: Maybe Text
description = Maybe Text
description_
          , photo :: Maybe Photo
photo       = Maybe Photo
photo_
          , animation :: Maybe Animation
animation   = Maybe Animation
animation_
          }
  parseJSON Value
_ = Parser Game
forall a. Monoid a => a
mempty