module TD.Data.LabeledPricePart
  ( LabeledPricePart(..)    
  , defaultLabeledPricePart 
  ) 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 LabeledPricePart
  = LabeledPricePart -- ^ Portion of the price of a product (e.g., "delivery cost", "tax amount")
    { LabeledPricePart -> Maybe Text
label  :: Maybe T.Text -- ^ Label for this portion of the product price
    , LabeledPricePart -> Maybe Int
amount :: Maybe Int    -- ^ Currency amount in the smallest units of the currency
    }
  deriving (LabeledPricePart -> LabeledPricePart -> Bool
(LabeledPricePart -> LabeledPricePart -> Bool)
-> (LabeledPricePart -> LabeledPricePart -> Bool)
-> Eq LabeledPricePart
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LabeledPricePart -> LabeledPricePart -> Bool
== :: LabeledPricePart -> LabeledPricePart -> Bool
$c/= :: LabeledPricePart -> LabeledPricePart -> Bool
/= :: LabeledPricePart -> LabeledPricePart -> Bool
Eq, Int -> LabeledPricePart -> ShowS
[LabeledPricePart] -> ShowS
LabeledPricePart -> String
(Int -> LabeledPricePart -> ShowS)
-> (LabeledPricePart -> String)
-> ([LabeledPricePart] -> ShowS)
-> Show LabeledPricePart
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LabeledPricePart -> ShowS
showsPrec :: Int -> LabeledPricePart -> ShowS
$cshow :: LabeledPricePart -> String
show :: LabeledPricePart -> String
$cshowList :: [LabeledPricePart] -> ShowS
showList :: [LabeledPricePart] -> ShowS
Show)

instance I.ShortShow LabeledPricePart where
  shortShow :: LabeledPricePart -> String
shortShow LabeledPricePart
    { label :: LabeledPricePart -> Maybe Text
label  = Maybe Text
label_
    , amount :: LabeledPricePart -> Maybe Int
amount = Maybe Int
amount_
    }
      = String
"LabeledPricePart"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"label"  String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
label_
        , String
"amount" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
amount_
        ]

instance AT.FromJSON LabeledPricePart where
  parseJSON :: Value -> Parser LabeledPricePart
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
"labeledPricePart" -> Value -> Parser LabeledPricePart
parseLabeledPricePart Value
v
      String
_                  -> Parser LabeledPricePart
forall a. Monoid a => a
mempty
    
    where
      parseLabeledPricePart :: A.Value -> AT.Parser LabeledPricePart
      parseLabeledPricePart :: Value -> Parser LabeledPricePart
parseLabeledPricePart = String
-> (Object -> Parser LabeledPricePart)
-> Value
-> Parser LabeledPricePart
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"LabeledPricePart" ((Object -> Parser LabeledPricePart)
 -> Value -> Parser LabeledPricePart)
-> (Object -> Parser LabeledPricePart)
-> Value
-> Parser LabeledPricePart
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Text
label_  <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"label"
        Maybe Int
amount_ <- Object
o Object -> Key -> Parser (Maybe Int)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"amount"
        LabeledPricePart -> Parser LabeledPricePart
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LabeledPricePart -> Parser LabeledPricePart)
-> LabeledPricePart -> Parser LabeledPricePart
forall a b. (a -> b) -> a -> b
$ LabeledPricePart
          { label :: Maybe Text
label  = Maybe Text
label_
          , amount :: Maybe Int
amount = Maybe Int
amount_
          }
  parseJSON Value
_ = Parser LabeledPricePart
forall a. Monoid a => a
mempty

instance AT.ToJSON LabeledPricePart where
  toJSON :: LabeledPricePart -> Value
toJSON LabeledPricePart
    { label :: LabeledPricePart -> Maybe Text
label  = Maybe Text
label_
    , amount :: LabeledPricePart -> Maybe Int
amount = Maybe Int
amount_
    }
      = [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
"labeledPricePart"
        , Key
"label"  Key -> Maybe Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Text
label_
        , Key
"amount" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
amount_
        ]

defaultLabeledPricePart :: LabeledPricePart
defaultLabeledPricePart :: LabeledPricePart
defaultLabeledPricePart =
  LabeledPricePart
    { label :: Maybe Text
label  = Maybe Text
forall a. Maybe a
Nothing
    , amount :: Maybe Int
amount = Maybe Int
forall a. Maybe a
Nothing
    }