module TD.Data.DateTimePartPrecision
  (DateTimePartPrecision(..)) where

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

-- | Describes precision with which to show a date or a time
data DateTimePartPrecision
  = DateTimePartPrecisionNone -- ^ Don't show the date or time
  | DateTimePartPrecisionShort -- ^ Show the date or time in a short way (17.03.22 or 22:45)
  | DateTimePartPrecisionLong -- ^ Show the date or time in a long way (March 17, 2022 or 22:45:00)
  deriving (DateTimePartPrecision -> DateTimePartPrecision -> Bool
(DateTimePartPrecision -> DateTimePartPrecision -> Bool)
-> (DateTimePartPrecision -> DateTimePartPrecision -> Bool)
-> Eq DateTimePartPrecision
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DateTimePartPrecision -> DateTimePartPrecision -> Bool
== :: DateTimePartPrecision -> DateTimePartPrecision -> Bool
$c/= :: DateTimePartPrecision -> DateTimePartPrecision -> Bool
/= :: DateTimePartPrecision -> DateTimePartPrecision -> Bool
Eq, Int -> DateTimePartPrecision -> ShowS
[DateTimePartPrecision] -> ShowS
DateTimePartPrecision -> String
(Int -> DateTimePartPrecision -> ShowS)
-> (DateTimePartPrecision -> String)
-> ([DateTimePartPrecision] -> ShowS)
-> Show DateTimePartPrecision
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DateTimePartPrecision -> ShowS
showsPrec :: Int -> DateTimePartPrecision -> ShowS
$cshow :: DateTimePartPrecision -> String
show :: DateTimePartPrecision -> String
$cshowList :: [DateTimePartPrecision] -> ShowS
showList :: [DateTimePartPrecision] -> ShowS
Show)

instance I.ShortShow DateTimePartPrecision where
  shortShow :: DateTimePartPrecision -> String
shortShow DateTimePartPrecision
DateTimePartPrecisionNone
      = String
"DateTimePartPrecisionNone"
  shortShow DateTimePartPrecision
DateTimePartPrecisionShort
      = String
"DateTimePartPrecisionShort"
  shortShow DateTimePartPrecision
DateTimePartPrecisionLong
      = String
"DateTimePartPrecisionLong"

instance AT.FromJSON DateTimePartPrecision where
  parseJSON :: Value -> Parser DateTimePartPrecision
parseJSON (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
"dateTimePartPrecisionNone"  -> DateTimePartPrecision -> Parser DateTimePartPrecision
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DateTimePartPrecision
DateTimePartPrecisionNone
      String
"dateTimePartPrecisionShort" -> DateTimePartPrecision -> Parser DateTimePartPrecision
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DateTimePartPrecision
DateTimePartPrecisionShort
      String
"dateTimePartPrecisionLong"  -> DateTimePartPrecision -> Parser DateTimePartPrecision
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure DateTimePartPrecision
DateTimePartPrecisionLong
      String
_                            -> Parser DateTimePartPrecision
forall a. Monoid a => a
mempty
    
  parseJSON Value
_ = Parser DateTimePartPrecision
forall a. Monoid a => a
mempty

instance AT.ToJSON DateTimePartPrecision where
  toJSON :: DateTimePartPrecision -> Value
toJSON DateTimePartPrecision
DateTimePartPrecisionNone
      = [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
"dateTimePartPrecisionNone"
        ]
  toJSON DateTimePartPrecision
DateTimePartPrecisionShort
      = [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
"dateTimePartPrecisionShort"
        ]
  toJSON DateTimePartPrecision
DateTimePartPrecisionLong
      = [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
"dateTimePartPrecisionLong"
        ]