/*
 * 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.desktop.main.portfolio.bsqswaps;

import bisq.desktop.common.model.ActivatableWithDataModel;
import bisq.desktop.common.model.ViewModel;
import bisq.desktop.util.DisplayUtils;

import bisq.core.account.witness.AccountAgeWitnessService;
import bisq.core.locale.CurrencyUtil;
import bisq.core.trade.model.bsq_swap.BsqSwapTrade;
import bisq.core.util.FormattingUtils;
import bisq.core.util.VolumeUtil;
import bisq.core.util.coin.BsqFormatter;
import bisq.core.util.coin.CoinFormatter;

import org.bitcoinj.core.Coin;

import com.google.inject.Inject;

import javax.inject.Named;

import javafx.collections.ObservableList;

import java.util.stream.Collectors;

class UnconfirmedBsqSwapsViewModel extends ActivatableWithDataModel<UnconfirmedBsqSwapsDataModel> implements ViewModel {
    private final BsqFormatter bsqFormatter;
    private final CoinFormatter btcFormatter;
    final AccountAgeWitnessService accountAgeWitnessService;

    @Inject
    public UnconfirmedBsqSwapsViewModel(UnconfirmedBsqSwapsDataModel dataModel,
                                        AccountAgeWitnessService accountAgeWitnessService,
                                        BsqFormatter bsqFormatter,
                                        @Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter btcFormatter) {
        super(dataModel);
        this.accountAgeWitnessService = accountAgeWitnessService;
        this.bsqFormatter = bsqFormatter;
        this.btcFormatter = btcFormatter;
    }

    public ObservableList<UnconfirmedBsqSwapsListItem> getList() {
        return dataModel.getList();
    }

    String getTradeId(UnconfirmedBsqSwapsListItem item) {
        return item.getBsqSwapTrade().getShortId();
    }

    String getAmount(UnconfirmedBsqSwapsListItem item) {
        if (item == null)
            return "";

        return btcFormatter.formatCoin(item.getBsqSwapTrade().getAmount());
    }

    String getPrice(UnconfirmedBsqSwapsListItem item) {
        if (item == null)
            return "";

        return FormattingUtils.formatPrice(item.getBsqSwapTrade().getPrice());
    }

    String getVolume(UnconfirmedBsqSwapsListItem item) {
        if (item == null)
            return "";

        return VolumeUtil.formatVolumeWithCode(item.getBsqSwapTrade().getVolume());
    }

    String getTxFee(UnconfirmedBsqSwapsListItem item) {
        if (item == null)
            return "";

        return btcFormatter.formatCoinWithCode(Coin.valueOf(item.getBsqSwapTrade().getBsqSwapProtocolModel().getTxFee()));
    }

    String getTradeFee(UnconfirmedBsqSwapsListItem item) {
        if (item == null)
            return "";

        if (wasMyOffer(item.getBsqSwapTrade())) {
            return bsqFormatter.formatCoinWithCode(item.getBsqSwapTrade().getMakerFeeAsLong());
        } else {
            return bsqFormatter.formatCoinWithCode(item.getBsqSwapTrade().getTakerFeeAsLong());
        }
    }

    String getDirectionLabel(UnconfirmedBsqSwapsListItem item) {
        if (item == null)
            return "";

        return DisplayUtils.getDirectionWithCode(dataModel.getDirection(item.getBsqSwapTrade().getOffer()),
                item.getBsqSwapTrade().getOffer().getCurrencyCode());
    }

    String getDate(UnconfirmedBsqSwapsListItem item) {
        return DisplayUtils.formatDateTime(item.getBsqSwapTrade().getDate());
    }

    String getMarketLabel(UnconfirmedBsqSwapsListItem item) {
        if ((item == null))
            return "";

        return CurrencyUtil.getCurrencyPair(item.getBsqSwapTrade().getOffer().getCurrencyCode());
    }

    int getConfidence(UnconfirmedBsqSwapsListItem item) {
        if ((item == null))
            return 0;
        return item.getConfirmations();
    }

    int getNumPastTrades(BsqSwapTrade bsqSwapTrade) {
        // TODO(sq): include closed trades in count
        return dataModel.bsqSwapTradeManager.getObservableList().stream()
                .filter(e -> {
                    var candidate = e.getTradingPeerNodeAddress();
                    var current = bsqSwapTrade.getTradingPeerNodeAddress();
                    return candidate != null &&
                            current != null &&
                            candidate.getFullAddress().equals(current.getFullAddress());
                })
                .collect(Collectors.toSet())
                .size();
    }

    boolean wasMyOffer(BsqSwapTrade bsqSwapTrade) {
        return dataModel.bsqSwapTradeManager.wasMyOffer(bsqSwapTrade.getOffer());
    }
}

@@ -958,24 +958,64 @@
 rrencyUtil;%0A
+import bisq.core.trade.ClosedTradeUtil;%0A
 import bisq.
@@ -1054,16 +1054,16 @@
 pTrade;%0A
-
 import b
@@ -1352,45 +1352,8 @@
 t;%0A%0A
-import java.util.stream.Collectors;%0A%0A
 clas
@@ -1469,16 +1469,67 @@
 Model %7B%0A
+    private final ClosedTradeUtil closedTradeUtil;%0A
     priv
@@ -1856,16 +1856,89 @@
 ervice,%0A
+                                        ClosedTradeUtil closedTradeUtil,%0A
         
@@ -2134,16 +2134,16 @@
 Model);%0A
-
         
@@ -2200,16 +2200,64 @@
 ervice;%0A
+        this.closedTradeUtil = closedTradeUtil;%0A
         
@@ -4272,33 +4272,32 @@
 m) %7B%0A        if 
-(
 (item == null))%0A
@@ -4286,33 +4286,32 @@
 f (item == null)
-)
 %0A            ret
@@ -4645,570 +4645,60 @@
     
-// TODO(sq): include closed trades in count%0A        return dataModel.bsqSwapTradeManager.getObservableList().stream()%0A                .filter(e -%3E %7B%0A                    var candidate = e.getTradingPeerNodeAddress();%0A                    var current = bsqSwapTrade.getTradingPeerNodeAddress();%0A                    return candidate != null &&%0A                            current != null &&%0A                            candidate.getFullAddress().equals(current.getFullAddress());%0A                %7D)%0A                .collect(Collectors.toSet())%0A                .size(
+return closedTradeUtil.getNumPastTrades(bsqSwapTrade
 );%0A 

Fix getNumPastTrades method