/*
 * This file is part of Bisq.
 *
 * Bisq is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bisq is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bisq. If not, see <http://www.gnu.org/licenses/>.
 */

package bisq.core.trade.bsq_swap;

import bisq.core.offer.Offer;
import bisq.core.provider.price.PriceFeedService;
import bisq.core.trade.model.TradableList;
import bisq.core.trade.model.bsq_swap.BsqSwapTrade;

import bisq.common.crypto.KeyRing;
import bisq.common.persistence.PersistenceManager;
import bisq.common.proto.persistable.PersistedDataHost;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.google.common.collect.ImmutableList;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;

import javafx.collections.ObservableList;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Singleton
public class BsqSwapTradeManager implements PersistedDataHost {
    private final PersistenceManager<TradableList<BsqSwapTrade>> persistenceManager;
    private final TradableList<BsqSwapTrade> bsqSwapTrades = new TradableList<>();
    private final KeyRing keyRing;
    private final PriceFeedService priceFeedService;

    // Used for listening for notifications in the UI
    @Getter
    private final ObjectProperty<BsqSwapTrade> completedBsqSwapTrade = new SimpleObjectProperty<>();

    @Inject
    public BsqSwapTradeManager(KeyRing keyRing,
                               PriceFeedService priceFeedService,
                               PersistenceManager<TradableList<BsqSwapTrade>> persistenceManager) {
        this.keyRing = keyRing;
        this.priceFeedService = priceFeedService;
        this.persistenceManager = persistenceManager;

        this.persistenceManager.initialize(bsqSwapTrades, "BsqSwapTrades", PersistenceManager.Source.PRIVATE);
    }

    @Override
    public void readPersisted(Runnable completeHandler) {
        persistenceManager.readPersisted(persisted -> {
                    bsqSwapTrades.setAll(persisted.getList());
                    bsqSwapTrades.stream()
                            .filter(bsqSwapTrade -> bsqSwapTrade.getOffer() != null)
                            .forEach(bsqSwapTrade -> bsqSwapTrade.getOffer().setPriceFeedService(priceFeedService));
                    completeHandler.run();
                },
                completeHandler);
    }

    public void onAllServicesInitialized() {
    }

    public void onTradeCompleted(BsqSwapTrade bsqSwapTrade) {
        if (findBsqSwapTradeById(bsqSwapTrade.getId()).isPresent()) {
            return;
        }

        if (bsqSwapTrades.add(bsqSwapTrade)) {
            requestPersistence();

            completedBsqSwapTrade.set(bsqSwapTrade);
        }
    }

    public void resetCompletedBsqSwapTrade() {
        completedBsqSwapTrade.set(null);
    }

    public boolean wasMyOffer(Offer offer) {
        return offer.isMyOffer(keyRing);
    }

    public ObservableList<BsqSwapTrade> getObservableList() {
        return bsqSwapTrades.getObservableList();
    }

    public List<BsqSwapTrade> getBsqSwapTrades() {
        return ImmutableList.copyOf(new ArrayList<>(getObservableList()));
    }

    public Optional<BsqSwapTrade> findBsqSwapTradeById(String id) {
        return bsqSwapTrades.stream().filter(e -> e.getId().equals(id)).findFirst();
    }

    private void requestPersistence() {
        persistenceManager.requestPersistence();
    }
}

@@ -710,16 +710,62 @@
 _swap;%0A%0A
+import bisq.core.btc.wallet.BsqWalletService;%0A
 import b
@@ -782,24 +782,24 @@
 ffer.Offer;%0A
-
 import bisq.
@@ -1071,24 +1071,73 @@
 dDataHost;%0A%0A
+import org.bitcoinj.core.TransactionConfidence;%0A%0A
 import com.g
@@ -1460,16 +1460,48 @@
 ptional;
+%0Aimport java.util.stream.Stream;
 %0A%0Aimport
@@ -1633,16 +1633,69 @@
 aHost %7B%0A
+    private final BsqWalletService bsqWalletService;%0A
     priv
@@ -2237,16 +2237,82 @@
 ervice,%0A
+                               BsqWalletService bsqWalletService,%0A
         
@@ -2481,24 +2481,74 @@
 eedService;%0A
+        this.bsqWalletService = bsqWalletService;%0A
         this
@@ -4118,24 +4118,24 @@
 tring id) %7B%0A
-
         retu
@@ -4210,24 +4210,986 @@
 t();%0A    %7D%0A%0A
+    public Stream%3CBsqSwapTrade%3E getUnconfirmedBsqSwapTrades() %7B%0A        return getObservableList().stream().filter(this::isUnconfirmedTx);%0A    %7D%0A%0A    public Stream%3CBsqSwapTrade%3E getConfirmedBsqSwapTrades() %7B%0A        return getObservableList().stream().filter(this::isConfirmedTx);%0A    %7D%0A%0A    private boolean isUnconfirmedTx(BsqSwapTrade bsqSwapTrade) %7B%0A        return isConfirmedTx(bsqSwapTrade, TransactionConfidence.ConfidenceType.PENDING);%0A    %7D%0A%0A    private boolean isConfirmedTx(BsqSwapTrade bsqSwapTrade) %7B%0A        return isConfirmedTx(bsqSwapTrade, TransactionConfidence.ConfidenceType.BUILDING);%0A    %7D%0A%0A    private boolean isConfirmedTx(BsqSwapTrade bsqSwapTrade, TransactionConfidence.ConfidenceType confidenceTyp) %7B%0A        TransactionConfidence confidenceForTxId = bsqWalletService.getConfidenceForTxId(bsqSwapTrade.getTxId());%0A        return confidenceForTxId != null &&%0A                confidenceForTxId.getConfidenceType() == confidenceTyp;%0A    %7D%0A%0A
     private 

Add methods for confirmed and unconfirmed trades