module TD.Data.Count
  (Count(..)) where

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

data Count
  = Count -- ^ Contains a counter
    { Count -> Maybe Int
count :: Maybe Int -- ^ Count
    }
  deriving (Count -> Count -> Bool
(Count -> Count -> Bool) -> (Count -> Count -> Bool) -> Eq Count
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Count -> Count -> Bool
== :: Count -> Count -> Bool
$c/= :: Count -> Count -> Bool
/= :: Count -> Count -> Bool
Eq, Int -> Count -> ShowS
[Count] -> ShowS
Count -> String
(Int -> Count -> ShowS)
-> (Count -> String) -> ([Count] -> ShowS) -> Show Count
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Count -> ShowS
showsPrec :: Int -> Count -> ShowS
$cshow :: Count -> String
show :: Count -> String
$cshowList :: [Count] -> ShowS
showList :: [Count] -> ShowS
Show)

instance I.ShortShow Count where
  shortShow :: Count -> String
shortShow Count
    { count :: Count -> Maybe Int
count = Maybe Int
count_
    }
      = String
"Count"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"count" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
count_
        ]

instance AT.FromJSON Count where
  parseJSON :: Value -> Parser Count
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
"count" -> Value -> Parser Count
parseCount Value
v
      String
_       -> Parser Count
forall a. Monoid a => a
mempty
    
    where
      parseCount :: A.Value -> AT.Parser Count
      parseCount :: Value -> Parser Count
parseCount = String -> (Object -> Parser Count) -> Value -> Parser Count
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Count" ((Object -> Parser Count) -> Value -> Parser Count)
-> (Object -> Parser Count) -> Value -> Parser Count
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
count_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"count"
        Count -> Parser Count
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Count -> Parser Count) -> Count -> Parser Count
forall a b. (a -> b) -> a -> b
$ Count
          { count :: Maybe Int
count = Maybe Int
count_
          }
  parseJSON Value
_ = Parser Count
forall a. Monoid a => a
mempty