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

import bisq.core.btc.wallet.Restrictions;
import bisq.core.trade.Trade;
import bisq.core.trade.messages.InputsForDepositTxResponse;
import bisq.core.trade.protocol.TradingPeer;
import bisq.core.trade.protocol.tasks.TradeTask;

import bisq.common.config.Config;
import bisq.common.taskrunner.TaskRunner;

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 TakerProcessesInputsForDepositTxResponse extends TradeTask {
    public TakerProcessesInputsForDepositTxResponse(TaskRunner<Trade> taskHandler, Trade trade) {
        super(taskHandler, trade);
    }

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

            TradingPeer tradingPeer = processModel.getTradingPeer();
            tradingPeer.setPaymentAccountPayload(checkNotNull(inputsForDepositTxResponse.getMakerPaymentAccountPayload()));
            tradingPeer.setAccountId(nonEmptyStringOf(inputsForDepositTxResponse.getMakerAccountId()));
            tradingPeer.setMultiSigPubKey(checkNotNull(inputsForDepositTxResponse.getMakerMultiSigPubKey()));
            tradingPeer.setContractAsJson(nonEmptyStringOf(inputsForDepositTxResponse.getMakerContractAsJson()));
            tradingPeer.setContractSignature(nonEmptyStringOf(inputsForDepositTxResponse.getMakerContractSignature()));
            tradingPeer.setPayoutAddressString(nonEmptyStringOf(inputsForDepositTxResponse.getMakerPayoutAddressString()));
            tradingPeer.setRawTransactionInputs(checkNotNull(inputsForDepositTxResponse.getMakerInputs()));
            byte[] preparedDepositTx = inputsForDepositTxResponse.getPreparedDepositTx();
            processModel.setPreparedDepositTx(checkNotNull(preparedDepositTx));
            long lockTime = inputsForDepositTxResponse.getLockTime();
            if (Config.baseCurrencyNetwork().isMainnet()) {
                int myLockTime = processModel.getBtcWalletService().getBestChainHeight() +
                        Restrictions.getLockTime(processModel.getOffer().getPaymentMethod().isAsset());
                // We allow a tolerance of 3 blocks as BestChainHeight might be a bit different on maker and taker in case a new
                // block was just found
                checkArgument(Math.abs(lockTime - myLockTime) <= 3,
                        "Lock time of maker is more than 3 blocks different to the locktime I " +
                                "calculated. Makers lockTime= " + lockTime + ", myLockTime=" + myLockTime);
            }
            trade.setLockTime(lockTime);
            log.info("lockTime={}, delay={}", lockTime, (processModel.getBtcWalletService().getBestChainHeight() - lockTime));

            // Maker has to sign preparedDepositTx. He cannot manipulate the preparedDepositTx - so we avoid to have a
            // challenge protocol for passing the nonce we want to get signed.
            tradingPeer.setAccountAgeWitnessNonce(inputsForDepositTxResponse.getPreparedDepositTx());
            tradingPeer.setAccountAgeWitnessSignature(inputsForDepositTxResponse.getAccountAgeWitnessSignatureOfPreparedDepositTx());

            tradingPeer.setCurrentDate(inputsForDepositTxResponse.getCurrentDate());

            checkArgument(inputsForDepositTxResponse.getMakerInputs().size() > 0);

            // update to the latest peer address of our peer if the message is correct
            trade.setTradingPeerNodeAddress(processModel.getTempTradingPeerNodeAddress());
            trade.setState(Trade.State.TAKER_RECEIVED_PUBLISH_DEPOSIT_TX_REQUEST);

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

@@ -1615,74 +1615,8 @@
 ();%0A
-            log.debug(%22current trade state %22 + trade.getState());%0A
     
@@ -1650,35 +1650,17 @@
 esponse 
-inputsForDepositTxR
+r
 esponse 
@@ -1774,35 +1774,17 @@
 erId(), 
-inputsForDepositTxR
+r
 esponse)
@@ -1810,35 +1810,17 @@
 NotNull(
-inputsForDepositTxR
+r
 esponse)
@@ -1953,35 +1953,17 @@
 NotNull(
-inputsForDepositTxR
+r
 esponse.
@@ -2051,35 +2051,17 @@
 tringOf(
-inputsForDepositTxR
+r
 esponse.
@@ -2138,35 +2138,17 @@
 NotNull(
-inputsForDepositTxR
+r
 esponse.
@@ -2234,35 +2234,17 @@
 tringOf(
-inputsForDepositTxR
+r
 esponse.
@@ -2333,35 +2333,17 @@
 tringOf(
-inputsForDepositTxR
+r
 esponse.
@@ -2437,35 +2437,17 @@
 tringOf(
-inputsForDepositTxR
+r
 esponse.
@@ -2540,35 +2540,17 @@
 NotNull(
-inputsForDepositTxR
+r
 esponse.
@@ -2573,98 +2573,8 @@
 ));%0A
-            byte%5B%5D preparedDepositTx = inputsForDepositTxResponse.getPreparedDepositTx();%0A
     
@@ -2628,17 +2628,29 @@
 NotNull(
-p
+response.getP
 reparedD
@@ -2657,16 +2657,18 @@
 epositTx
+()
 ));%0A    
@@ -2691,35 +2691,17 @@
 kTime = 
-inputsForDepositTxR
+r
 esponse.
@@ -3294,17 +3294,17 @@
 the lock
-t
+T
 ime I %22 
@@ -3486,51 +3486,19 @@
   lo
-g.info(%22lockTime=%7B%7D, delay=%7B%7D%22, lockTime, (
+ng delay = 
 proc
@@ -3559,17 +3559,79 @@
 lockTime
-)
+;%0A            log.info(%22lockTime=%7B%7D, delay=%7B%7D%22, lockTime, delay
 );%0A%0A    
@@ -3874,35 +3874,30 @@
 ssNonce(
-inputsForDepositTxR
+checkNotNull(r
 esponse.
@@ -3919,16 +3919,17 @@
 sitTx())
+)
 ;%0A      
@@ -3976,35 +3976,30 @@
 gnature(
-inputsForDepositTxR
+checkNotNull(r
 esponse.
@@ -4049,16 +4049,17 @@
 sitTx())
+)
 ;%0A%0A     
@@ -4092,35 +4092,17 @@
 entDate(
-inputsForDepositTxR
+r
 esponse.
@@ -4151,27 +4151,9 @@
 ent(
-inputsForDepositTxR
+r
 espo

rename inputsForDepositTxResponse , add more checkNotNull checks