module TD.Data.DateTimeFormattingType
  (DateTimeFormattingType(..)) 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.DateTimePartPrecision as DateTimePartPrecision

-- | Describes date and time formatting
data DateTimeFormattingType
  = DateTimeFormattingTypeRelative -- ^ The time must be shown relative to the current time ([in ] X seconds, minutes, hours, days, months, years [ago])
  | DateTimeFormattingTypeAbsolute -- ^ The date and time must be shown as absolute timestamps
    { DateTimeFormattingType -> Maybe DateTimePartPrecision
time_precision   :: Maybe DateTimePartPrecision.DateTimePartPrecision -- ^ The precision with which hours, minutes and seconds are shown
    , DateTimeFormattingType -> Maybe DateTimePartPrecision
date_precision   :: Maybe DateTimePartPrecision.DateTimePartPrecision -- ^ The precision with which the date is shown
    , DateTimeFormattingType -> Maybe Bool
show_day_of_week :: Maybe Bool                                        -- ^ True, if the day of week must be shown
    }
  deriving (DateTimeFormattingType -> DateTimeFormattingType -> Bool
(DateTimeFormattingType -> DateTimeFormattingType -> Bool)
-> (DateTimeFormattingType -> DateTimeFormattingType -> Bool)
-> Eq DateTimeFormattingType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DateTimeFormattingType -> DateTimeFormattingType -> Bool
== :: DateTimeFormattingType -> DateTimeFormattingType -> Bool
$c/= :: DateTimeFormattingType -> DateTimeFormattingType -> Bool
/= :: DateTimeFormattingType -> DateTimeFormattingType -> Bool
Eq, Int -> DateTimeFormattingType -> ShowS
[DateTimeFormattingType] -> ShowS
DateTimeFormattingType -> String
(Int -> DateTimeFormattingType -> ShowS)
-> (DateTimeFormattingType -> String)
-> ([DateTimeFormattingType] -> ShowS)
-> Show DateTimeFormattingType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DateTimeFormattingType -> ShowS
showsPrec :: Int -> DateTimeFormattingType -> ShowS
$cshow :: DateTimeFormattingType -> String
show :: DateTimeFormattingType -> String
$cshowList :: [DateTimeFormattingType] -> ShowS
showList :: [DateTimeFormattingType] -> ShowS
Show)

instance I.ShortShow DateTimeFormattingType where
  shortShow :: DateTimeFormattingType -> String
shortShow DateTimeFormattingType
DateTimeFormattingTypeRelative
      = String
"DateTimeFormattingTypeRelative"
  shortShow DateTimeFormattingTypeAbsolute
    { time_precision :: DateTimeFormattingType -> Maybe DateTimePartPrecision
time_precision   = Maybe DateTimePartPrecision
time_precision_
    , date_precision :: DateTimeFormattingType -> Maybe DateTimePartPrecision
date_precision   = Maybe DateTimePartPrecision
date_precision_
    , show_day_of_week :: DateTimeFormattingType -> Maybe Bool
show_day_of_week = Maybe Bool
show_day_of_week_
    }
      = String
"DateTimeFormattingTypeAbsolute"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"time_precision"   String -> Maybe DateTimePartPrecision -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe DateTimePartPrecision
time_precision_
        , String
"date_precision"   String -> Maybe DateTimePartPrecision -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe DateTimePartPrecision
date_precision_
        , String
"show_day_of_week" String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
show_day_of_week_
        ]

instance AT.FromJSON DateTimeFormattingType where
  parseJSON :: Value -> Parser DateTimeFormattingType
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
"dateTimeFormattingTypeRelative" -> DateTimeFormattingType -> Parser DateTimeFormattingType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DateTimeFormattingType
DateTimeFormattingTypeRelative
      String
"dateTimeFormattingTypeAbsolute" -> Value -> Parser DateTimeFormattingType
parseDateTimeFormattingTypeAbsolute Value
v
      String
_                                -> Parser DateTimeFormattingType
forall a. Monoid a => a
mempty
    
    where
      parseDateTimeFormattingTypeAbsolute :: A.Value -> AT.Parser DateTimeFormattingType
      parseDateTimeFormattingTypeAbsolute :: Value -> Parser DateTimeFormattingType
parseDateTimeFormattingTypeAbsolute = String
-> (Object -> Parser DateTimeFormattingType)
-> Value
-> Parser DateTimeFormattingType
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"DateTimeFormattingTypeAbsolute" ((Object -> Parser DateTimeFormattingType)
 -> Value -> Parser DateTimeFormattingType)
-> (Object -> Parser DateTimeFormattingType)
-> Value
-> Parser DateTimeFormattingType
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe DateTimePartPrecision
time_precision_   <- Object
o Object -> Key -> Parser (Maybe DateTimePartPrecision)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"time_precision"
        Maybe DateTimePartPrecision
date_precision_   <- Object
o Object -> Key -> Parser (Maybe DateTimePartPrecision)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"date_precision"
        Maybe Bool
show_day_of_week_ <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"show_day_of_week"
        DateTimeFormattingType -> Parser DateTimeFormattingType
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DateTimeFormattingType -> Parser DateTimeFormattingType)
-> DateTimeFormattingType -> Parser DateTimeFormattingType
forall a b. (a -> b) -> a -> b
$ DateTimeFormattingTypeAbsolute
          { time_precision :: Maybe DateTimePartPrecision
time_precision   = Maybe DateTimePartPrecision
time_precision_
          , date_precision :: Maybe DateTimePartPrecision
date_precision   = Maybe DateTimePartPrecision
date_precision_
          , show_day_of_week :: Maybe Bool
show_day_of_week = Maybe Bool
show_day_of_week_
          }
  parseJSON Value
_ = Parser DateTimeFormattingType
forall a. Monoid a => a
mempty

instance AT.ToJSON DateTimeFormattingType where
  toJSON :: DateTimeFormattingType -> Value
toJSON DateTimeFormattingType
DateTimeFormattingTypeRelative
      = [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
"dateTimeFormattingTypeRelative"
        ]
  toJSON DateTimeFormattingTypeAbsolute
    { time_precision :: DateTimeFormattingType -> Maybe DateTimePartPrecision
time_precision   = Maybe DateTimePartPrecision
time_precision_
    , date_precision :: DateTimeFormattingType -> Maybe DateTimePartPrecision
date_precision   = Maybe DateTimePartPrecision
date_precision_
    , show_day_of_week :: DateTimeFormattingType -> Maybe Bool
show_day_of_week = Maybe Bool
show_day_of_week_
    }
      = [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
"dateTimeFormattingTypeAbsolute"
        , Key
"time_precision"   Key -> Maybe DateTimePartPrecision -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe DateTimePartPrecision
time_precision_
        , Key
"date_precision"   Key -> Maybe DateTimePartPrecision -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe DateTimePartPrecision
date_precision_
        , Key
"show_day_of_week" Key -> Maybe Bool -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Bool
show_day_of_week_
        ]