module TD.Data.Birthdate
  ( Birthdate(..)    
  , defaultBirthdate 
  ) where

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

data Birthdate
  = Birthdate -- ^ Represents a birthdate of a user
    { Birthdate -> Maybe Int
day   :: Maybe Int -- ^ Day of the month; 1-31
    , Birthdate -> Maybe Int
month :: Maybe Int -- ^ Month of the year; 1-12
    , Birthdate -> Maybe Int
year  :: Maybe Int -- ^ Birth year; 0 if unknown
    }
  deriving (Birthdate -> Birthdate -> Bool
(Birthdate -> Birthdate -> Bool)
-> (Birthdate -> Birthdate -> Bool) -> Eq Birthdate
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Birthdate -> Birthdate -> Bool
== :: Birthdate -> Birthdate -> Bool
$c/= :: Birthdate -> Birthdate -> Bool
/= :: Birthdate -> Birthdate -> Bool
Eq, Int -> Birthdate -> ShowS
[Birthdate] -> ShowS
Birthdate -> String
(Int -> Birthdate -> ShowS)
-> (Birthdate -> String)
-> ([Birthdate] -> ShowS)
-> Show Birthdate
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Birthdate -> ShowS
showsPrec :: Int -> Birthdate -> ShowS
$cshow :: Birthdate -> String
show :: Birthdate -> String
$cshowList :: [Birthdate] -> ShowS
showList :: [Birthdate] -> ShowS
Show)

instance I.ShortShow Birthdate where
  shortShow :: Birthdate -> String
shortShow Birthdate
    { day :: Birthdate -> Maybe Int
day   = Maybe Int
day_
    , month :: Birthdate -> Maybe Int
month = Maybe Int
month_
    , year :: Birthdate -> Maybe Int
year  = Maybe Int
year_
    }
      = String
"Birthdate"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"day"   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
day_
        , String
"month" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
month_
        , String
"year"  String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
year_
        ]

instance AT.FromJSON Birthdate where
  parseJSON :: Value -> Parser Birthdate
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
"birthdate" -> Value -> Parser Birthdate
parseBirthdate Value
v
      String
_           -> Parser Birthdate
forall a. Monoid a => a
mempty
    
    where
      parseBirthdate :: A.Value -> AT.Parser Birthdate
      parseBirthdate :: Value -> Parser Birthdate
parseBirthdate = String -> (Object -> Parser Birthdate) -> Value -> Parser Birthdate
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"Birthdate" ((Object -> Parser Birthdate) -> Value -> Parser Birthdate)
-> (Object -> Parser Birthdate) -> Value -> Parser Birthdate
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Int
day_   <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"day"
        Maybe Int
month_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"month"
        Maybe Int
year_  <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"year"
        Birthdate -> Parser Birthdate
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Birthdate -> Parser Birthdate) -> Birthdate -> Parser Birthdate
forall a b. (a -> b) -> a -> b
$ Birthdate
          { day :: Maybe Int
day   = Maybe Int
day_
          , month :: Maybe Int
month = Maybe Int
month_
          , year :: Maybe Int
year  = Maybe Int
year_
          }
  parseJSON Value
_ = Parser Birthdate
forall a. Monoid a => a
mempty

instance AT.ToJSON Birthdate where
  toJSON :: Birthdate -> Value
toJSON Birthdate
    { day :: Birthdate -> Maybe Int
day   = Maybe Int
day_
    , month :: Birthdate -> Maybe Int
month = Maybe Int
month_
    , year :: Birthdate -> Maybe Int
year  = Maybe Int
year_
    }
      = [Pair] -> Value
A.object
        [ Key
"@type" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"birthdate"
        , Key
"day"   Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
day_
        , Key
"month" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
month_
        , Key
"year"  Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
year_
        ]

defaultBirthdate :: Birthdate
defaultBirthdate :: Birthdate
defaultBirthdate =
  Birthdate
    { day :: Maybe Int
day   = Maybe Int
forall a. Maybe a
Nothing
    , month :: Maybe Int
month = Maybe Int
forall a. Maybe a
Nothing
    , year :: Maybe Int
year  = Maybe Int
forall a. Maybe a
Nothing
    }