content: module P2PWallet.GUI.Widgets.TxBuilder.LoanBuilder.LenderAddressUpdates
    ( 
      lenderAddressUpdatesList
    , editLenderAddressUpdateWidget
    ) where
  
  import Monomer as M hiding (duration)
  
  import P2PWallet.Data.AppModel
  import P2PWallet.Data.Core.Wallets.LoanWallet
  import P2PWallet.Data.DeFi.CardanoLoans qualified as Loans
  import P2PWallet.GUI.Colors
  import P2PWallet.GUI.Icons
  import P2PWallet.GUI.MonomerOptics()
  import P2PWallet.GUI.Widgets.Internal.Custom
  import P2PWallet.Prelude
  
  lenderAddressUpdatesList :: [(Int,LenderAddressUpdate)] -> [AppNode]
  lenderAddressUpdatesList = map utxoRow
    where
      utxoRow :: (Int, LenderAddressUpdate) -> AppNode
      utxoRow s@(idx,LenderAddressUpdate{..}) = do
        let Loans.ActiveDatum{..} = fromMaybe def $ loanUTxOActiveDatum loanUTxO
            addressTip = unwords
              [ "Payments to"
              , newPaymentWallet ^. #alias <> ":"
              , display $ newPaymentWallet ^. #paymentAddress
              ]
        hstack
          [ vstack
              [ hstack
                  [ label "Update Loan Payment Address"
                      `styleBasic` [textSize 10, textColor customBlue]
                  , spacer_ [width 5]
                  , separatorLine `styleBasic` [fgColor darkGray, paddingT 1, paddingB 1]
                  , spacer_ [width 5]
                  , let prettyRef = display $ loanUTxO ^. #utxoRef in
                    flip styleBasic [textSize 10] $ tooltip_ prettyRef [tooltipDelay 0] $
                      box_ [alignMiddle, onClick $ CopyText prettyRef] $
                        label targetUTxOIcon
                          `styleBasic` 
                            [ bgColor black
                            , textMiddle
                            , textFont "Remix"
                            , textSize 8
                            , textColor customBlue
                            , paddingT 1
                            , paddingB 1
                            , paddingL 3
                            , paddingR 3
                            , radius 5
                            ]
                          `styleHover` [bgColor customGray1, cursorIcon CursorHand]
                  , spacer_ [width 5]
                  , flip styleBasic [textSize 10] $ tooltip_ alias [tooltipDelay 0] $
                      label userIcon
                        `styleBasic` 
                          [ textMiddle
                          , textFont "Remix"
                          , textSize 8
                          , textColor customBlue
                          ]
                  , spacer_ [width 5]
                  , flip styleBasic [textSize 10] $ tooltip_ addressTip [tooltipDelay 0] $
                      box_ [alignMiddle, onClick $ CopyText $ display $ newPaymentWallet ^. #paymentAddress] $
                        label targetAddressIcon
                          `styleBasic` 
                            [ bgColor black
                            , textMiddle
                            , textFont "Remix"
                            , textSize 8
                            , textColor customBlue
                            , paddingT 1
                            , paddingB 1
                            , paddingL 3
                            , paddingR 3
                            , radius 5
                            ]
                          `styleHover` [bgColor customGray1, cursorIcon CursorHand]
                  , filler
                  ]
              , spacer_ [width 2]
              , hstack
                  [ label "Loan ID:"
                      `styleBasic` [textSize 8, textColor lightGray]
                  , spacer_ [width 5]
                  , label (display loanId)
                      `styleBasic` [textSize 8, textColor lightGray]
                  , filler
                  ]
              ] `styleBasic` 
                  [ padding 10
                  , bgColor customGray2
                  , radius 5
                  , border 1 black
                  ]
          , spacer_ [width 3]
          , hstack
              [ box_ [alignCenter,alignMiddle] $ tooltip_ "Edit Action" [tooltipDelay 0] $
                  button editIcon 
                      (loanBuilderEvent $ EditSelectedLenderAddressUpdate $ StartAdding $ Just s)
                    `styleBasic` 
                      [ textSize 10
                      , textColor customBlue
                      , textFont "Remix"
                      , textMiddle
                      , padding 3
                      , radius 3
                      , bgColor transparent
                      , border 0 transparent
                      ]
                    `styleHover` [bgColor customGray1, cursorIcon CursorHand]
              , box_ [alignCenter,alignMiddle] $ tooltip_ "Remove Action" [tooltipDelay 0] $
                  button closeCircleIcon (loanBuilderEvent $ RemoveSelectedLenderAddressUpdate idx)
                    `styleBasic` 
                      [ textSize 10
                      , textColor customRed
                      , textFont "Remix"
                      , textMiddle
                      , padding 3
                      , radius 3
                      , bgColor transparent
                      , border 0 transparent
                      ]
                    `styleHover` [bgColor customGray1, cursorIcon CursorHand]
              ]
          ]
  
  editLenderAddressUpdateWidget :: AppModel -> AppNode
  editLenderAddressUpdateWidget AppModel{knownWallets} = do
    let maybeLens' = maybeLens (def,def) $ #txBuilderModel % #loanBuilderModel % #targetAddressUpdate
        innerDormantStyle = 
          def `styleBasic` [textSize 10, bgColor customGray3, border 1 black]
              `styleHover` [textSize 10, bgColor customGray2, border 1 black]
        innerFocusedStyle = 
          def `styleFocus` [textSize 10, bgColor customGray3, border 1 customBlue]
              `styleFocusHover` [textSize 10, bgColor customGray2, border 1 customBlue]
    centerWidget $ vstack
      [ centerWidgetH $ label "Where would you like future loan payments to go?"
      , spacer_ [width 20]
      , centerWidgetH $ hstack
          [ label "Address:"
          , spacer
          , textDropdown_ 
                (toLensVL $ maybeLens' % _2 % #newPaymentWallet) 
                (knownWallets ^. #paymentWallets) 
                (view #alias) -- The dropdown displays the wallet's alias in the menu.
                [itemBasicStyle innerDormantStyle, itemSelectedStyle innerFocusedStyle]
              `styleBasic` 
                [ bgColor customGray2
                , width 150
                , border 1 black
                , textSize 10
                ]
              `styleHover` [bgColor customGray1, cursorIcon CursorHand]
          ]
      , spacer
      , hstack 
          [ filler
          , button "Cancel" $ loanBuilderEvent $ EditSelectedLenderAddressUpdate CancelAdding
          , spacer
          , mainButton "Confirm" $ loanBuilderEvent $ EditSelectedLenderAddressUpdate ConfirmAdding
          ]
      ] `styleBasic` [radius 20, bgColor customGray3, padding 20]
  