/*
 * 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.protocol.tasks.maker;

import bisq.core.exceptions.TradePriceOutOfToleranceException;
import bisq.core.offer.Offer;
import bisq.core.support.dispute.mediation.mediator.Mediator;
import bisq.core.trade.Trade;
import bisq.core.trade.messages.InputsForDepositTxRequest;
import bisq.core.trade.protocol.TradingPeer;
import bisq.core.trade.protocol.tasks.TradeTask;
import bisq.core.user.User;

import bisq.network.p2p.NodeAddress;

import bisq.common.taskrunner.TaskRunner;

import org.bitcoinj.core.Coin;

import com.google.common.base.Charsets;

import lombok.extern.slf4j.Slf4j;

import static bisq.core.util.Validator.checkTradeId;
import static bisq.core.util.Validator.nonEmptyStringOf;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

@Slf4j
public class MakerProcessesInputsForDepositTxRequest extends TradeTask {
    public MakerProcessesInputsForDepositTxRequest(TaskRunner<Trade> taskHandler, Trade trade) {
        super(taskHandler, trade);
    }

    @Override
    protected void run() {
        try {
            runInterceptHook();
            log.debug("current trade state " + trade.getState());
            InputsForDepositTxRequest inputsForDepositTxRequest = (InputsForDepositTxRequest) processModel.getTradeMessage();
            checkNotNull(inputsForDepositTxRequest);
            checkTradeId(processModel.getOfferId(), inputsForDepositTxRequest);

            final TradingPeer tradingPeer = processModel.getTradingPeer();
            tradingPeer.setPaymentAccountPayload(checkNotNull(inputsForDepositTxRequest.getTakerPaymentAccountPayload()));
            tradingPeer.setRawTransactionInputs(checkNotNull(inputsForDepositTxRequest.getRawTransactionInputs()));
            checkArgument(inputsForDepositTxRequest.getRawTransactionInputs().size() > 0);

            tradingPeer.setChangeOutputValue(inputsForDepositTxRequest.getChangeOutputValue());
            tradingPeer.setChangeOutputAddress(inputsForDepositTxRequest.getChangeOutputAddress());

            tradingPeer.setMultiSigPubKey(checkNotNull(inputsForDepositTxRequest.getTakerMultiSigPubKey()));
            tradingPeer.setPayoutAddressString(nonEmptyStringOf(inputsForDepositTxRequest.getTakerPayoutAddressString()));
            tradingPeer.setPubKeyRing(checkNotNull(inputsForDepositTxRequest.getTakerPubKeyRing()));

            tradingPeer.setAccountId(nonEmptyStringOf(inputsForDepositTxRequest.getTakerAccountId()));
            trade.setTakerFeeTxId(nonEmptyStringOf(inputsForDepositTxRequest.getTakerFeeTxId()));

            // Taker has to sign offerId (he cannot manipulate that - so we avoid to have a challenge protocol for passing the nonce we want to get signed)
            tradingPeer.setAccountAgeWitnessNonce(trade.getId().getBytes(Charsets.UTF_8));
            tradingPeer.setAccountAgeWitnessSignature(inputsForDepositTxRequest.getAccountAgeWitnessSignatureOfOfferId());
            tradingPeer.setCurrentDate(inputsForDepositTxRequest.getCurrentDate());

            User user = checkNotNull(processModel.getUser(), "User must not be null");

            NodeAddress mediatorNodeAddress = checkNotNull(inputsForDepositTxRequest.getMediatorNodeAddress(),
                    "payDepositRequest.getMediatorNodeAddress() must not be null");
            trade.setMediatorNodeAddress(mediatorNodeAddress);
            Mediator mediator = checkNotNull(user.getAcceptedMediatorByAddress(mediatorNodeAddress),
                    "user.getAcceptedMediatorByAddress(mediatorNodeAddress) must not be null");
            trade.setMediatorPubKeyRing(checkNotNull(mediator.getPubKeyRing(),
                    "mediator.getPubKeyRing() must not be null"));

            Offer offer = checkNotNull(trade.getOffer(), "Offer must not be null");
            try {
                long takersTradePrice = inputsForDepositTxRequest.getTradePrice();
                offer.checkTradePriceTolerance(takersTradePrice);
                trade.setTradePrice(takersTradePrice);
            } catch (TradePriceOutOfToleranceException e) {
                failed(e.getMessage());
            } catch (Throwable e2) {
                failed(e2);
            }

            checkArgument(inputsForDepositTxRequest.getTradeAmount() > 0);
            trade.setTradeAmount(Coin.valueOf(inputsForDepositTxRequest.getTradeAmount()));

            trade.setTradingPeerNodeAddress(processModel.getTempTradingPeerNodeAddress());

            trade.persist();

            complete();
        } catch (Throwable t) {
            failed(t);
        }
    }
}

@@ -1830,74 +1830,8 @@
 ();%0A
-            log.debug(%22current trade state %22 + trade.getState());%0A
     
@@ -1864,35 +1864,17 @@
 Request 
-inputsForDepositTxR
+r
 equest =
@@ -1959,35 +1959,17 @@
 NotNull(
-inputsForDepositTxR
+r
 equest);
@@ -2021,35 +2021,17 @@
 erId(), 
-inputsForDepositTxR
+r
 equest);
@@ -2048,14 +2048,8 @@
     
-final 
 Trad
@@ -2163,35 +2163,17 @@
 NotNull(
-inputsForDepositTxR
+r
 equest.g
@@ -2267,35 +2267,17 @@
 NotNull(
-inputsForDepositTxR
+r
 equest.g
@@ -2330,35 +2330,17 @@
 rgument(
-inputsForDepositTxR
+r
 equest.g
@@ -2423,35 +2423,17 @@
 utValue(
-inputsForDepositTxR
+r
 equest.g
@@ -2503,35 +2503,17 @@
 Address(
-inputsForDepositTxR
+r
 equest.g
@@ -2594,35 +2594,17 @@
 NotNull(
-inputsForDepositTxR
+r
 equest.g
@@ -2694,35 +2694,17 @@
 tringOf(
-inputsForDepositTxR
+r
 equest.g
@@ -2786,35 +2786,17 @@
 NotNull(
-inputsForDepositTxR
+r
 equest.g
@@ -2873,35 +2873,17 @@
 tringOf(
-inputsForDepositTxR
+r
 equest.g
@@ -2955,35 +2955,17 @@
 tringOf(
-inputsForDepositTxR
+r
 equest.g
@@ -3099,16 +3099,31 @@
 ocol for
+%0A            //
  passing
@@ -3301,35 +3301,17 @@
 gnature(
-inputsForDepositTxR
+r
 equest.g
@@ -3391,35 +3391,17 @@
 entDate(
-inputsForDepositTxR
+r
 equest.g
@@ -3566,35 +3566,17 @@
 NotNull(
-inputsForDepositTxR
+r
 equest.g
@@ -3625,18 +3625,26 @@
    %22
-pay
+InputsFor
 Deposit
+Tx
 Requ
@@ -4241,35 +4241,17 @@
 Price = 
-inputsForDepositTxR
+r
 equest.g
@@ -4593,35 +4593,17 @@
 rgument(
-inputsForDepositTxR
+r
 equest.g
@@ -4674,27 +4674,9 @@
 eOf(
-inputsForDepositTxR
+r
 eque

rename inputsForDepositTxRequest to request Remove final Cleanups