module TD.Data.LanguagePackString
  ( LanguagePackString(..)    
  , defaultLanguagePackString 
  ) 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
import qualified TD.Data.LanguagePackStringValue as LanguagePackStringValue

data LanguagePackString
  = LanguagePackString -- ^ Represents one language pack string
    { LanguagePackString -> Maybe Text
key   :: Maybe T.Text                                          -- ^ String key
    , LanguagePackString -> Maybe LanguagePackStringValue
value :: Maybe LanguagePackStringValue.LanguagePackStringValue -- ^ String value; pass null if the string needs to be taken from the built-in English language pack
    }
  deriving (LanguagePackString -> LanguagePackString -> Bool
(LanguagePackString -> LanguagePackString -> Bool)
-> (LanguagePackString -> LanguagePackString -> Bool)
-> Eq LanguagePackString
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LanguagePackString -> LanguagePackString -> Bool
== :: LanguagePackString -> LanguagePackString -> Bool
$c/= :: LanguagePackString -> LanguagePackString -> Bool
/= :: LanguagePackString -> LanguagePackString -> Bool
Eq, Int -> LanguagePackString -> ShowS
[LanguagePackString] -> ShowS
LanguagePackString -> String
(Int -> LanguagePackString -> ShowS)
-> (LanguagePackString -> String)
-> ([LanguagePackString] -> ShowS)
-> Show LanguagePackString
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LanguagePackString -> ShowS
showsPrec :: Int -> LanguagePackString -> ShowS
$cshow :: LanguagePackString -> String
show :: LanguagePackString -> String
$cshowList :: [LanguagePackString] -> ShowS
showList :: [LanguagePackString] -> ShowS
Show)

instance I.ShortShow LanguagePackString where
  shortShow :: LanguagePackString -> String
shortShow LanguagePackString
    { key :: LanguagePackString -> Maybe Text
key   = Maybe Text
key_
    , value :: LanguagePackString -> Maybe LanguagePackStringValue
value = Maybe LanguagePackStringValue
value_
    }
      = String
"LanguagePackString"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"key"   String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
key_
        , String
"value" String -> Maybe LanguagePackStringValue -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe LanguagePackStringValue
value_
        ]

instance AT.FromJSON LanguagePackString where
  parseJSON :: Value -> Parser LanguagePackString
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
"languagePackString" -> Value -> Parser LanguagePackString
parseLanguagePackString Value
v
      String
_                    -> Parser LanguagePackString
forall a. Monoid a => a
mempty
    
    where
      parseLanguagePackString :: A.Value -> AT.Parser LanguagePackString
      parseLanguagePackString :: Value -> Parser LanguagePackString
parseLanguagePackString = String
-> (Object -> Parser LanguagePackString)
-> Value
-> Parser LanguagePackString
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"LanguagePackString" ((Object -> Parser LanguagePackString)
 -> Value -> Parser LanguagePackString)
-> (Object -> Parser LanguagePackString)
-> Value
-> Parser LanguagePackString
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
key_   <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"key"
        Maybe LanguagePackStringValue
value_ <- Object
o Object -> Key -> Parser (Maybe LanguagePackStringValue)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"value"
        LanguagePackString -> Parser LanguagePackString
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LanguagePackString -> Parser LanguagePackString)
-> LanguagePackString -> Parser LanguagePackString
forall a b. (a -> b) -> a -> b
$ LanguagePackString
          { key :: Maybe Text
key   = Maybe Text
key_
          , value :: Maybe LanguagePackStringValue
value = Maybe LanguagePackStringValue
value_
          }
  parseJSON Value
_ = Parser LanguagePackString
forall a. Monoid a => a
mempty

instance AT.ToJSON LanguagePackString where
  toJSON :: LanguagePackString -> Value
toJSON LanguagePackString
    { key :: LanguagePackString -> Maybe Text
key   = Maybe Text
key_
    , value :: LanguagePackString -> Maybe LanguagePackStringValue
value = Maybe LanguagePackStringValue
value_
    }
      = [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
"languagePackString"
        , Key
"key"   Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
key_
        , Key
"value" Key -> Maybe LanguagePackStringValue -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe LanguagePackStringValue
value_
        ]

defaultLanguagePackString :: LanguagePackString
defaultLanguagePackString :: LanguagePackString
defaultLanguagePackString =
  LanguagePackString
    { key :: Maybe Text
key   = Maybe Text
forall a. Maybe a
Nothing
    , value :: Maybe LanguagePackStringValue
value = Maybe LanguagePackStringValue
forall a. Maybe a
Nothing
    }