module TD.Data.CurrentWeather
  (CurrentWeather(..)) 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 CurrentWeather
  = CurrentWeather -- ^ Describes the current weather
    { CurrentWeather -> Maybe Double
temperature :: Maybe Double -- ^ Temperature, in degree Celsius
    , CurrentWeather -> Maybe Text
emoji       :: Maybe T.Text -- ^ Emoji representing the weather
    }
  deriving (CurrentWeather -> CurrentWeather -> Bool
(CurrentWeather -> CurrentWeather -> Bool)
-> (CurrentWeather -> CurrentWeather -> Bool) -> Eq CurrentWeather
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CurrentWeather -> CurrentWeather -> Bool
== :: CurrentWeather -> CurrentWeather -> Bool
$c/= :: CurrentWeather -> CurrentWeather -> Bool
/= :: CurrentWeather -> CurrentWeather -> Bool
Eq, Int -> CurrentWeather -> ShowS
[CurrentWeather] -> ShowS
CurrentWeather -> String
(Int -> CurrentWeather -> ShowS)
-> (CurrentWeather -> String)
-> ([CurrentWeather] -> ShowS)
-> Show CurrentWeather
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CurrentWeather -> ShowS
showsPrec :: Int -> CurrentWeather -> ShowS
$cshow :: CurrentWeather -> String
show :: CurrentWeather -> String
$cshowList :: [CurrentWeather] -> ShowS
showList :: [CurrentWeather] -> ShowS
Show)

instance I.ShortShow CurrentWeather where
  shortShow :: CurrentWeather -> String
shortShow CurrentWeather
    { temperature :: CurrentWeather -> Maybe Double
temperature = Maybe Double
temperature_
    , emoji :: CurrentWeather -> Maybe Text
emoji       = Maybe Text
emoji_
    }
      = String
"CurrentWeather"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"temperature" String -> Maybe Double -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Double
temperature_
        , String
"emoji"       String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
emoji_
        ]

instance AT.FromJSON CurrentWeather where
  parseJSON :: Value -> Parser CurrentWeather
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
"currentWeather" -> Value -> Parser CurrentWeather
parseCurrentWeather Value
v
      String
_                -> Parser CurrentWeather
forall a. Monoid a => a
mempty
    
    where
      parseCurrentWeather :: A.Value -> AT.Parser CurrentWeather
      parseCurrentWeather :: Value -> Parser CurrentWeather
parseCurrentWeather = String
-> (Object -> Parser CurrentWeather)
-> Value
-> Parser CurrentWeather
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"CurrentWeather" ((Object -> Parser CurrentWeather)
 -> Value -> Parser CurrentWeather)
-> (Object -> Parser CurrentWeather)
-> Value
-> Parser CurrentWeather
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Double
temperature_ <- Object
o Object -> Key -> Parser (Maybe Double)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"temperature"
        Maybe Text
emoji_       <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"emoji"
        CurrentWeather -> Parser CurrentWeather
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CurrentWeather -> Parser CurrentWeather)
-> CurrentWeather -> Parser CurrentWeather
forall a b. (a -> b) -> a -> b
$ CurrentWeather
          { temperature :: Maybe Double
temperature = Maybe Double
temperature_
          , emoji :: Maybe Text
emoji       = Maybe Text
emoji_
          }
  parseJSON Value
_ = Parser CurrentWeather
forall a. Monoid a => a
mempty