/*
 * 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.api;

import bisq.core.offer.Offer;
import bisq.core.offer.takeoffer.TakeOfferModel;
import bisq.core.trade.Trade;
import bisq.core.trade.TradeManager;
import bisq.core.trade.protocol.BuyerProtocol;
import bisq.core.trade.protocol.SellerProtocol;
import bisq.core.user.User;

import javax.inject.Inject;

import java.util.function.Consumer;

import lombok.extern.slf4j.Slf4j;

import static java.lang.String.format;

@Slf4j
class CoreTradesService {

    private final TakeOfferModel takeOfferModel;
    private final TradeManager tradeManager;
    private final User user;

    @Inject
    public CoreTradesService(TakeOfferModel takeOfferModel,
                             TradeManager tradeManager,
                             User user) {
        this.takeOfferModel = takeOfferModel;
        this.tradeManager = tradeManager;
        this.user = user;
    }

    void takeOffer(Offer offer,
                   String paymentAccountId,
                   Consumer<Trade> resultHandler) {
        var paymentAccount = user.getPaymentAccount(paymentAccountId);
        if (paymentAccount == null)
            throw new IllegalArgumentException(format("payment account with id '%s' not found", paymentAccountId));

        var useSavingsWallet = true;
        takeOfferModel.initModel(offer, paymentAccount, useSavingsWallet);
        log.info("Initiating take {} offer, {}",
                offer.isBuyOffer() ? "buy" : "sell",
                takeOfferModel);
        //noinspection ConstantConditions
        tradeManager.onTakeOffer(offer.getAmount(),
                takeOfferModel.getTxFeeFromFeeService(),
                takeOfferModel.getTakerFee(),
                takeOfferModel.isCurrencyForTakerFeeBtc(),
                offer.getPrice().getValue(),
                takeOfferModel.getFundsNeededForTrade(),
                offer,
                paymentAccountId,
                useSavingsWallet,
                trade -> {
                    resultHandler.accept(trade);
                },
                errorMessage -> {
                    log.error(errorMessage);
                    throw new IllegalStateException(errorMessage);
                }
        );
    }

    void confirmPaymentStarted(String tradeId) {
        var trade = getTradeWithId(tradeId);
        if (isFollowingBuyerProtocol(trade)) {
            var tradeProtocol = tradeManager.getTradeProtocol(trade);
            ((BuyerProtocol) tradeProtocol).onPaymentStarted(
                    () -> {
                    },
                    errorMessage -> {
                        throw new IllegalStateException(errorMessage);
                    }
            );
        } else {
            throw new IllegalStateException("you are the seller and not sending payment");
        }
    }

    void confirmPaymentReceived(String tradeId) {
        var trade = getTradeWithId(tradeId);
        if (isFollowingBuyerProtocol(trade)) {
            throw new IllegalStateException("you are the buyer, and not receiving payment");
        } else {
            var tradeProtocol = tradeManager.getTradeProtocol(trade);
            ((SellerProtocol) tradeProtocol).onPaymentReceived(
                    () -> {
                    },
                    errorMessage -> {
                        throw new IllegalStateException(errorMessage);
                    }
            );
        }
    }

    Trade getTrade(String tradeId) {
        return getTradeWithId(tradeId);
    }

    private Trade getTradeWithId(String tradeId) {
        return tradeManager.getTradeById(tradeId).orElseThrow(() ->
                new IllegalArgumentException(format("trade with id '%s' not found", tradeId)));
    }

    private boolean isFollowingBuyerProtocol(Trade trade) {
        return tradeManager.getTradeProtocol(trade) instanceof BuyerProtocol;
    }
}

@@ -2953,38 +2953,32 @@
 trade = getTrade
-WithId
 (tradeId);%0A     
@@ -2964,32 +2964,32 @@
 Trade(tradeId);%0A
+
         if (isFo
@@ -3543,38 +3543,32 @@
 trade = getTrade
-WithId
 (tradeId);%0A     
@@ -4063,24 +4063,24 @@
    %7D%0A    %7D%0A%0A
+
     Trade ge
@@ -4108,106 +4108,8 @@
 ) %7B%0A
-        return getTradeWithId(tradeId);%0A    %7D%0A%0A    private Trade getTradeWithId(String tradeId) %7B%0A
     

Remove redundant getTrade(id) method