module TD.Data.WebAppOpenParameters
  ( WebAppOpenParameters(..)    
  , defaultWebAppOpenParameters 
  ) 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.ThemeParameters as ThemeParameters
import qualified Data.Text as T
import qualified TD.Data.WebAppOpenMode as WebAppOpenMode

data WebAppOpenParameters
  = WebAppOpenParameters -- ^ Options to be used when a Web App is opened
    { WebAppOpenParameters -> Maybe ThemeParameters
theme            :: Maybe ThemeParameters.ThemeParameters -- ^ Preferred Web App theme; pass null to use the default theme
    , WebAppOpenParameters -> Maybe Text
application_name :: Maybe T.Text                          -- ^ Short name of the current application; 0-64 English letters, digits, and underscores
    , WebAppOpenParameters -> Maybe WebAppOpenMode
mode             :: Maybe WebAppOpenMode.WebAppOpenMode   -- ^ The mode in which the Web App is opened; pass null to open in webAppOpenModeFullSize
    }
  deriving (WebAppOpenParameters -> WebAppOpenParameters -> Bool
(WebAppOpenParameters -> WebAppOpenParameters -> Bool)
-> (WebAppOpenParameters -> WebAppOpenParameters -> Bool)
-> Eq WebAppOpenParameters
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: WebAppOpenParameters -> WebAppOpenParameters -> Bool
== :: WebAppOpenParameters -> WebAppOpenParameters -> Bool
$c/= :: WebAppOpenParameters -> WebAppOpenParameters -> Bool
/= :: WebAppOpenParameters -> WebAppOpenParameters -> Bool
Eq, Int -> WebAppOpenParameters -> ShowS
[WebAppOpenParameters] -> ShowS
WebAppOpenParameters -> String
(Int -> WebAppOpenParameters -> ShowS)
-> (WebAppOpenParameters -> String)
-> ([WebAppOpenParameters] -> ShowS)
-> Show WebAppOpenParameters
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WebAppOpenParameters -> ShowS
showsPrec :: Int -> WebAppOpenParameters -> ShowS
$cshow :: WebAppOpenParameters -> String
show :: WebAppOpenParameters -> String
$cshowList :: [WebAppOpenParameters] -> ShowS
showList :: [WebAppOpenParameters] -> ShowS
Show)

instance I.ShortShow WebAppOpenParameters where
  shortShow :: WebAppOpenParameters -> String
shortShow WebAppOpenParameters
    { theme :: WebAppOpenParameters -> Maybe ThemeParameters
theme            = Maybe ThemeParameters
theme_
    , application_name :: WebAppOpenParameters -> Maybe Text
application_name = Maybe Text
application_name_
    , mode :: WebAppOpenParameters -> Maybe WebAppOpenMode
mode             = Maybe WebAppOpenMode
mode_
    }
      = String
"WebAppOpenParameters"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"theme"            String -> Maybe ThemeParameters -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ThemeParameters
theme_
        , String
"application_name" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
application_name_
        , String
"mode"             String -> Maybe WebAppOpenMode -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe WebAppOpenMode
mode_
        ]

instance AT.FromJSON WebAppOpenParameters where
  parseJSON :: Value -> Parser WebAppOpenParameters
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
"webAppOpenParameters" -> Value -> Parser WebAppOpenParameters
parseWebAppOpenParameters Value
v
      String
_                      -> Parser WebAppOpenParameters
forall a. Monoid a => a
mempty
    
    where
      parseWebAppOpenParameters :: A.Value -> AT.Parser WebAppOpenParameters
      parseWebAppOpenParameters :: Value -> Parser WebAppOpenParameters
parseWebAppOpenParameters = String
-> (Object -> Parser WebAppOpenParameters)
-> Value
-> Parser WebAppOpenParameters
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"WebAppOpenParameters" ((Object -> Parser WebAppOpenParameters)
 -> Value -> Parser WebAppOpenParameters)
-> (Object -> Parser WebAppOpenParameters)
-> Value
-> Parser WebAppOpenParameters
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe ThemeParameters
theme_            <- Object
o Object -> Key -> Parser (Maybe ThemeParameters)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"theme"
        Maybe Text
application_name_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"application_name"
        Maybe WebAppOpenMode
mode_             <- Object
o Object -> Key -> Parser (Maybe WebAppOpenMode)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"mode"
        WebAppOpenParameters -> Parser WebAppOpenParameters
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (WebAppOpenParameters -> Parser WebAppOpenParameters)
-> WebAppOpenParameters -> Parser WebAppOpenParameters
forall a b. (a -> b) -> a -> b
$ WebAppOpenParameters
          { theme :: Maybe ThemeParameters
theme            = Maybe ThemeParameters
theme_
          , application_name :: Maybe Text
application_name = Maybe Text
application_name_
          , mode :: Maybe WebAppOpenMode
mode             = Maybe WebAppOpenMode
mode_
          }
  parseJSON Value
_ = Parser WebAppOpenParameters
forall a. Monoid a => a
mempty

instance AT.ToJSON WebAppOpenParameters where
  toJSON :: WebAppOpenParameters -> Value
toJSON WebAppOpenParameters
    { theme :: WebAppOpenParameters -> Maybe ThemeParameters
theme            = Maybe ThemeParameters
theme_
    , application_name :: WebAppOpenParameters -> Maybe Text
application_name = Maybe Text
application_name_
    , mode :: WebAppOpenParameters -> Maybe WebAppOpenMode
mode             = Maybe WebAppOpenMode
mode_
    }
      = [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
"webAppOpenParameters"
        , Key
"theme"            Key -> Maybe ThemeParameters -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe ThemeParameters
theme_
        , Key
"application_name" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
application_name_
        , Key
"mode"             Key -> Maybe WebAppOpenMode -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe WebAppOpenMode
mode_
        ]

defaultWebAppOpenParameters :: WebAppOpenParameters
defaultWebAppOpenParameters :: WebAppOpenParameters
defaultWebAppOpenParameters =
  WebAppOpenParameters
    { theme :: Maybe ThemeParameters
theme            = Maybe ThemeParameters
forall a. Maybe a
Nothing
    , application_name :: Maybe Text
application_name = Maybe Text
forall a. Maybe a
Nothing
    , mode :: Maybe WebAppOpenMode
mode             = Maybe WebAppOpenMode
forall a. Maybe a
Nothing
    }