module TD.Data.ProductInfo
  (ProductInfo(..)) 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.FormattedText as FormattedText
import qualified TD.Data.Photo as Photo

data ProductInfo
  = ProductInfo -- ^ Contains information about a product that can be paid with invoice
    { ProductInfo -> Maybe Text
title       :: Maybe T.Text                      -- ^ Product title
    , ProductInfo -> Maybe FormattedText
description :: Maybe FormattedText.FormattedText -- ^ Product description
    , ProductInfo -> Maybe Photo
photo       :: Maybe Photo.Photo                 -- ^ Product photo; may be null
    }
  deriving (ProductInfo -> ProductInfo -> Bool
(ProductInfo -> ProductInfo -> Bool)
-> (ProductInfo -> ProductInfo -> Bool) -> Eq ProductInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ProductInfo -> ProductInfo -> Bool
== :: ProductInfo -> ProductInfo -> Bool
$c/= :: ProductInfo -> ProductInfo -> Bool
/= :: ProductInfo -> ProductInfo -> Bool
Eq, Int -> ProductInfo -> ShowS
[ProductInfo] -> ShowS
ProductInfo -> String
(Int -> ProductInfo -> ShowS)
-> (ProductInfo -> String)
-> ([ProductInfo] -> ShowS)
-> Show ProductInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ProductInfo -> ShowS
showsPrec :: Int -> ProductInfo -> ShowS
$cshow :: ProductInfo -> String
show :: ProductInfo -> String
$cshowList :: [ProductInfo] -> ShowS
showList :: [ProductInfo] -> ShowS
Show)

instance I.ShortShow ProductInfo where
  shortShow :: ProductInfo -> String
shortShow ProductInfo
    { title :: ProductInfo -> Maybe Text
title       = Maybe Text
title_
    , description :: ProductInfo -> Maybe FormattedText
description = Maybe FormattedText
description_
    , photo :: ProductInfo -> Maybe Photo
photo       = Maybe Photo
photo_
    }
      = String
"ProductInfo"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"title"       String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
title_
        , String
"description" String -> Maybe FormattedText -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe FormattedText
description_
        , String
"photo"       String -> Maybe Photo -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Photo
photo_
        ]

instance AT.FromJSON ProductInfo where
  parseJSON :: Value -> Parser ProductInfo
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
"productInfo" -> Value -> Parser ProductInfo
parseProductInfo Value
v
      String
_             -> Parser ProductInfo
forall a. Monoid a => a
mempty
    
    where
      parseProductInfo :: A.Value -> AT.Parser ProductInfo
      parseProductInfo :: Value -> Parser ProductInfo
parseProductInfo = String
-> (Object -> Parser ProductInfo) -> Value -> Parser ProductInfo
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"ProductInfo" ((Object -> Parser ProductInfo) -> Value -> Parser ProductInfo)
-> (Object -> Parser ProductInfo) -> Value -> Parser ProductInfo
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
title_       <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"title"
        Maybe FormattedText
description_ <- Object
o Object -> Key -> Parser (Maybe FormattedText)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"description"
        Maybe Photo
photo_       <- Object
o Object -> Key -> Parser (Maybe Photo)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"photo"
        ProductInfo -> Parser ProductInfo
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ProductInfo -> Parser ProductInfo)
-> ProductInfo -> Parser ProductInfo
forall a b. (a -> b) -> a -> b
$ ProductInfo
          { title :: Maybe Text
title       = Maybe Text
title_
          , description :: Maybe FormattedText
description = Maybe FormattedText
description_
          , photo :: Maybe Photo
photo       = Maybe Photo
photo_
          }
  parseJSON Value
_ = Parser ProductInfo
forall a. Monoid a => a
mempty