/*
 * 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.trade.Trade;
import bisq.core.trade.messages.InputsForDepositTxResponse;
import bisq.core.trade.protocol.TradingPeer;
import bisq.core.trade.protocol.tasks.TradeTask;

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 {
    @SuppressWarnings({"unused"})
    public TakerProcessesInputsForDepositTxResponse(TaskRunner 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();
            //todo for dev testing deactivated
            //checkArgument(lockTime >= processModel.getBtcWalletService().getBestChainHeight() + 144 * 20);
            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);
        }
    }
}

@@ -718,24 +718,66 @@
 sks.taker;%0A%0A
+import bisq.core.btc.wallet.Restrictions;%0A
 import bisq.
@@ -949,16 +949,50 @@
 eTask;%0A%0A
+import bisq.common.config.Config;%0A
 import b
@@ -3098,80 +3098,87 @@
     
-//todo for dev testing deactivated%0A            //checkArgument(l
+if (Config.baseCurrencyNetwork().isMainnet()) %7B%0A                int myL
 ockTime 
 %3E= p
@@ -3173,17 +3173,16 @@
 ockTime 
-%3E
 = proces
@@ -3236,19 +3236,569 @@
 () +
- 144 * 20);
+%0A                        Restrictions.getLockTime(processModel.getOffer().getPaymentMethod().isAsset());%0A                // We allow a tolerance of 3 blocks as BestChainHeight might be a bit different on maker and taker in case a new%0A                // block was just found%0A                checkArgument(Math.abs(lockTime - myLockTime) %3C= 3,%0A                        %22Lock time of maker is more than 3 blocks different to the locktime I %22 +%0A                                %22calculated. Makers lockTime= %22 + lockTime + %22, myLockTime=%22 + myLockTime);%0A            %7D
 %0A   

Enable check for lock time on mainnet and add a 3 block tolerance.