module TD.Data.TimeZone
  (TimeZone(..)) 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

data TimeZone
  = TimeZone -- ^ Describes a time zone
    { TimeZone -> Maybe Text
_id             :: Maybe T.Text -- ^ Unique time zone identifier
    , TimeZone -> Maybe Text
name            :: Maybe T.Text -- ^ Time zone name
    , TimeZone -> Maybe Int
utc_time_offset :: Maybe Int    -- ^ Current UTC time offset for the time zone
    }
  deriving (TimeZone -> TimeZone -> Bool
(TimeZone -> TimeZone -> Bool)
-> (TimeZone -> TimeZone -> Bool) -> Eq TimeZone
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TimeZone -> TimeZone -> Bool
== :: TimeZone -> TimeZone -> Bool
$c/= :: TimeZone -> TimeZone -> Bool
/= :: TimeZone -> TimeZone -> Bool
Eq, Int -> TimeZone -> ShowS
[TimeZone] -> ShowS
TimeZone -> String
(Int -> TimeZone -> ShowS)
-> (TimeZone -> String) -> ([TimeZone] -> ShowS) -> Show TimeZone
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TimeZone -> ShowS
showsPrec :: Int -> TimeZone -> ShowS
$cshow :: TimeZone -> String
show :: TimeZone -> String
$cshowList :: [TimeZone] -> ShowS
showList :: [TimeZone] -> ShowS
Show)

instance I.ShortShow TimeZone where
  shortShow :: TimeZone -> String
shortShow TimeZone
    { _id :: TimeZone -> Maybe Text
_id             = Maybe Text
_id_
    , name :: TimeZone -> Maybe Text
name            = Maybe Text
name_
    , utc_time_offset :: TimeZone -> Maybe Int
utc_time_offset = Maybe Int
utc_time_offset_
    }
      = String
"TimeZone"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"_id"             String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
_id_
        , String
"name"            String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
name_
        , String
"utc_time_offset" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
utc_time_offset_
        ]

instance AT.FromJSON TimeZone where
  parseJSON :: Value -> Parser TimeZone
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
"timeZone" -> Value -> Parser TimeZone
parseTimeZone Value
v
      String
_          -> Parser TimeZone
forall a. Monoid a => a
mempty
    
    where
      parseTimeZone :: A.Value -> AT.Parser TimeZone
      parseTimeZone :: Value -> Parser TimeZone
parseTimeZone = String -> (Object -> Parser TimeZone) -> Value -> Parser TimeZone
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"TimeZone" ((Object -> Parser TimeZone) -> Value -> Parser TimeZone)
-> (Object -> Parser TimeZone) -> Value -> Parser TimeZone
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
_id_             <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"id"
        Maybe Text
name_            <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"name"
        Maybe Int
utc_time_offset_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"utc_time_offset"
        TimeZone -> Parser TimeZone
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TimeZone -> Parser TimeZone) -> TimeZone -> Parser TimeZone
forall a b. (a -> b) -> a -> b
$ TimeZone
          { _id :: Maybe Text
_id             = Maybe Text
_id_
          , name :: Maybe Text
name            = Maybe Text
name_
          , utc_time_offset :: Maybe Int
utc_time_offset = Maybe Int
utc_time_offset_
          }
  parseJSON Value
_ = Parser TimeZone
forall a. Monoid a => a
mempty