module LndClient.Data.Payment
  ( Payment (..),
  )
where

import LndClient.Import
import qualified Proto.LndGrpc as LnGRPC
import qualified Proto.LndGrpc_Fields as LnGRPC

data Payment
  = Payment
      { paymentHash :: RHash,
        paymentPreimage :: RPreimage,
        valueMsat :: MSat,
        state :: PaymentStatus
      }
  deriving (Eq, Show)

data PaymentStatus
  = UNKNOWN
  | IN_FLIGHT
  | SUCCEEDED
  | FAILED
  deriving (Eq, Show)

instance FromGrpc Payment LnGRPC.Payment where
  fromGrpc x =
    Payment
      <$> fromGrpc (x ^. LnGRPC.paymentHash)
      <*> fromGrpc (x ^. LnGRPC.paymentPreimage)
      <*> fromGrpc (x ^. LnGRPC.valueMsat)
      <*> fromGrpc (x ^. LnGRPC.status)

instance FromGrpc PaymentStatus LnGRPC.Payment'PaymentStatus where
  fromGrpc x =
    case x of
      LnGRPC.Payment'UNKNOWN -> Right UNKNOWN
      LnGRPC.Payment'IN_FLIGHT -> Right IN_FLIGHT
      LnGRPC.Payment'SUCCEEDED -> Right SUCCEEDED
      LnGRPC.Payment'FAILED -> Right FAILED
      LnGRPC.Payment'PaymentStatus'Unrecognized v -> Left $ FromGrpcError ("Cannot parse PaymentStatus, value:" <> show v)