module TD.Data.StoreTransaction
  (StoreTransaction(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified Data.ByteString as BS
import qualified Data.Text as T

-- | Describes an in-store transaction
data StoreTransaction
  = StoreTransactionAppStore -- ^ A purchase through App Store
    { StoreTransaction -> Maybe ByteString
receipt :: Maybe BS.ByteString -- ^ App Store receipt
    }
  | StoreTransactionGooglePlay -- ^ A purchase through Google Play
    { StoreTransaction -> Maybe Text
package_name     :: Maybe T.Text -- ^ Application package name
    , StoreTransaction -> Maybe Text
store_product_id :: Maybe T.Text -- ^ Identifier of the purchased store product
    , StoreTransaction -> Maybe Text
purchase_token   :: Maybe T.Text -- ^ Google Play purchase token
    }
  deriving (StoreTransaction -> StoreTransaction -> Bool
(StoreTransaction -> StoreTransaction -> Bool)
-> (StoreTransaction -> StoreTransaction -> Bool)
-> Eq StoreTransaction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: StoreTransaction -> StoreTransaction -> Bool
== :: StoreTransaction -> StoreTransaction -> Bool
$c/= :: StoreTransaction -> StoreTransaction -> Bool
/= :: StoreTransaction -> StoreTransaction -> Bool
Eq, Int -> StoreTransaction -> ShowS
[StoreTransaction] -> ShowS
StoreTransaction -> String
(Int -> StoreTransaction -> ShowS)
-> (StoreTransaction -> String)
-> ([StoreTransaction] -> ShowS)
-> Show StoreTransaction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> StoreTransaction -> ShowS
showsPrec :: Int -> StoreTransaction -> ShowS
$cshow :: StoreTransaction -> String
show :: StoreTransaction -> String
$cshowList :: [StoreTransaction] -> ShowS
showList :: [StoreTransaction] -> ShowS
Show)

instance I.ShortShow StoreTransaction where
  shortShow :: StoreTransaction -> String
shortShow StoreTransactionAppStore
    { receipt :: StoreTransaction -> Maybe ByteString
receipt = Maybe ByteString
receipt_
    }
      = String
"StoreTransactionAppStore"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"receipt" String -> Maybe ByteString -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe ByteString
receipt_
        ]
  shortShow StoreTransactionGooglePlay
    { package_name :: StoreTransaction -> Maybe Text
package_name     = Maybe Text
package_name_
    , store_product_id :: StoreTransaction -> Maybe Text
store_product_id = Maybe Text
store_product_id_
    , purchase_token :: StoreTransaction -> Maybe Text
purchase_token   = Maybe Text
purchase_token_
    }
      = String
"StoreTransactionGooglePlay"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"package_name"     String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
package_name_
        , String
"store_product_id" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
store_product_id_
        , String
"purchase_token"   String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
purchase_token_
        ]

instance AT.FromJSON StoreTransaction where
  parseJSON :: Value -> Parser StoreTransaction
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
"storeTransactionAppStore"   -> Value -> Parser StoreTransaction
parseStoreTransactionAppStore Value
v
      String
"storeTransactionGooglePlay" -> Value -> Parser StoreTransaction
parseStoreTransactionGooglePlay Value
v
      String
_                            -> Parser StoreTransaction
forall a. Monoid a => a
mempty
    
    where
      parseStoreTransactionAppStore :: A.Value -> AT.Parser StoreTransaction
      parseStoreTransactionAppStore :: Value -> Parser StoreTransaction
parseStoreTransactionAppStore = String
-> (Object -> Parser StoreTransaction)
-> Value
-> Parser StoreTransaction
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StoreTransactionAppStore" ((Object -> Parser StoreTransaction)
 -> Value -> Parser StoreTransaction)
-> (Object -> Parser StoreTransaction)
-> Value
-> Parser StoreTransaction
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe ByteString
receipt_ <- (String -> ByteString) -> Maybe String -> Maybe ByteString
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> ByteString
I.readBytes (Maybe String -> Maybe ByteString)
-> Parser (Maybe String) -> Parser (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser (Maybe String)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"receipt"
        StoreTransaction -> Parser StoreTransaction
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StoreTransaction -> Parser StoreTransaction)
-> StoreTransaction -> Parser StoreTransaction
forall a b. (a -> b) -> a -> b
$ StoreTransactionAppStore
          { receipt :: Maybe ByteString
receipt = Maybe ByteString
receipt_
          }
      parseStoreTransactionGooglePlay :: A.Value -> AT.Parser StoreTransaction
      parseStoreTransactionGooglePlay :: Value -> Parser StoreTransaction
parseStoreTransactionGooglePlay = String
-> (Object -> Parser StoreTransaction)
-> Value
-> Parser StoreTransaction
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"StoreTransactionGooglePlay" ((Object -> Parser StoreTransaction)
 -> Value -> Parser StoreTransaction)
-> (Object -> Parser StoreTransaction)
-> Value
-> Parser StoreTransaction
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
package_name_     <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"package_name"
        Maybe Text
store_product_id_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"store_product_id"
        Maybe Text
purchase_token_   <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"purchase_token"
        StoreTransaction -> Parser StoreTransaction
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (StoreTransaction -> Parser StoreTransaction)
-> StoreTransaction -> Parser StoreTransaction
forall a b. (a -> b) -> a -> b
$ StoreTransactionGooglePlay
          { package_name :: Maybe Text
package_name     = Maybe Text
package_name_
          , store_product_id :: Maybe Text
store_product_id = Maybe Text
store_product_id_
          , purchase_token :: Maybe Text
purchase_token   = Maybe Text
purchase_token_
          }
  parseJSON Value
_ = Parser StoreTransaction
forall a. Monoid a => a
mempty

instance AT.ToJSON StoreTransaction where
  toJSON :: StoreTransaction -> Value
toJSON StoreTransactionAppStore
    { receipt :: StoreTransaction -> Maybe ByteString
receipt = Maybe ByteString
receipt_
    }
      = [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
"storeTransactionAppStore"
        , Key
"receipt" Key -> Maybe Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= (ByteString -> Value) -> Maybe ByteString -> Maybe Value
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Value
I.writeBytes  Maybe ByteString
receipt_
        ]
  toJSON StoreTransactionGooglePlay
    { package_name :: StoreTransaction -> Maybe Text
package_name     = Maybe Text
package_name_
    , store_product_id :: StoreTransaction -> Maybe Text
store_product_id = Maybe Text
store_product_id_
    , purchase_token :: StoreTransaction -> Maybe Text
purchase_token   = Maybe Text
purchase_token_
    }
      = [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
"storeTransactionGooglePlay"
        , Key
"package_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
package_name_
        , Key
"store_product_id" Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
store_product_id_
        , Key
"purchase_token"   Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
purchase_token_
        ]