<commit_before>/*
 * This file is part of Bitsquare.
 *
 * Bitsquare 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.
 *
 * Bitsquare 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 Bitsquare. If not, see <http://www.gnu.org/licenses/>.
 */

package io.bitsquare.gui.main.funds.reserved;

import io.bitsquare.btc.AddressEntry;
import io.bitsquare.btc.WalletService;
import io.bitsquare.btc.listeners.AddressConfidenceListener;
import io.bitsquare.btc.listeners.BalanceListener;
import io.bitsquare.gui.components.confidence.ConfidenceProgressIndicator;
import io.bitsquare.gui.util.BSFormatter;
import io.bitsquare.trade.Tradable;
import io.bitsquare.trade.Trade;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import org.bitcoinj.core.Address;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.TransactionConfidence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReservedListItem {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private final BalanceListener balanceListener;

    private final Label balanceLabel;

    private final Tradable tradable;
    private final AddressEntry addressEntry;

    private final WalletService walletService;
    private final BSFormatter formatter;
    private final AddressConfidenceListener confidenceListener;

    private final ConfidenceProgressIndicator progressIndicator;

    private final Tooltip tooltip;
    private final String addressString;
    private Coin balance;

    public ReservedListItem(Tradable tradable, AddressEntry addressEntry, WalletService walletService, BSFormatter formatter) {
        this.tradable = tradable;
        this.addressEntry = addressEntry;
        this.walletService = walletService;
        this.formatter = formatter;
        addressString = addressEntry.getAddressString();

        // confidence
        progressIndicator = new ConfidenceProgressIndicator();
        progressIndicator.setId("funds-confidence");
        tooltip = new Tooltip("Not used yet");
        progressIndicator.setProgress(0);
        progressIndicator.setPrefSize(24, 24);
        Tooltip.install(progressIndicator, tooltip);

        confidenceListener = walletService.addAddressConfidenceListener(new AddressConfidenceListener(getAddress()) {
            @Override
            public void onTransactionConfidenceChanged(TransactionConfidence confidence) {
                updateConfidence(confidence);
            }
        });

        updateConfidence(walletService.getConfidenceForAddress(getAddress()));


        // balance
        balanceLabel = new Label();
        balanceListener = walletService.addBalanceListener(new BalanceListener(getAddress()) {
            @Override
            public void onBalanceChanged(Coin balance) {
                updateBalance(balance);
            }
        });

        updateBalance(walletService.getBalanceForAddress(getAddress()));
    }

    public void cleanup() {
        walletService.removeAddressConfidenceListener(confidenceListener);
        walletService.removeBalanceListener(balanceListener);
    }

    private void updateBalance(Coin balance) {
        this.balance = balance;
        if (balance != null) {
            if (tradable instanceof Trade) {
                Trade trade = (Trade) tradable;
                Trade.Phase phase = trade.getState().getPhase();
                switch (phase) {
                    case PREPARATION:
                    case TAKER_FEE_PAID:
                        balanceLabel.setText(formatter.formatCoinWithCode(balance) + " locked in deposit");
                        break;
                    case DEPOSIT_PAID:
                    case FIAT_SENT:
                    case FIAT_RECEIVED:
                        // TODO show amount locked
                        balanceLabel.setText("Locked in deposit");
                        break;
                    case PAYOUT_PAID:
                        balanceLabel.setText(formatter.formatCoinWithCode(balance) + " in wallet");
                        break;
                    case WITHDRAWN:
                        log.error("Invalid state at updateBalance (WITHDRAWN)");
                        balanceLabel.setText(formatter.formatCoinWithCode(balance) + " already withdrawn");
                        break;
                    case DISPUTE:
                        balanceLabel.setText(formatter.formatCoinWithCode(balance) + " locked because of open ticket");
                        break;
                }

            } else
                balanceLabel.setText(formatter.formatCoin(balance));
        }
    }

    private void updateConfidence(TransactionConfidence confidence) {
        if (confidence != null) {
            //log.debug("Type numBroadcastPeers getDepthInBlocks " + confidence.getConfidenceType() + " / " +
            // confidence.numBroadcastPeers() + " / " + confidence.getDepthInBlocks());<commit_msg>Show balance of deposit tx<commit_after>/*
 * This file is part of Bitsquare.
 *
 * Bitsquare 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.
 *
 * Bitsquare 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 Bitsquare. If not, see <http://www.gnu.org/licenses/>.
 */

package io.bitsquare.gui.main.funds.reserved;

import io.bitsquare.btc.AddressEntry;
import io.bitsquare.btc.FeePolicy;
import io.bitsquare.btc.WalletService;
import io.bitsquare.btc.listeners.AddressConfidenceListener;
import io.bitsquare.btc.listeners.BalanceListener;
import io.bitsquare.gui.components.confidence.ConfidenceProgressIndicator;
import io.bitsquare.gui.util.BSFormatter;
import io.bitsquare.trade.Tradable;
import io.bitsquare.trade.Trade;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import org.bitcoinj.core.Address;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.TransactionConfidence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReservedListItem {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private final BalanceListener balanceListener;

    private final Label balanceLabel;

    private final Tradable tradable;
    private final AddressEntry addressEntry;

    private final WalletService walletService;
    private final BSFormatter formatter;
    private final AddressConfidenceListener confidenceListener;

    private final ConfidenceProgressIndicator progressIndicator;

    private final Tooltip tooltip;
    private final String addressString;
    private Coin balance;

    public ReservedListItem(Tradable tradable, AddressEntry addressEntry, WalletService walletService, BSFormatter formatter) {
        this.tradable = tradable;
        this.addressEntry = addressEntry;
        this.walletService = walletService;
        this.formatter = formatter;
        addressString = addressEntry.getAddressString();

        // confidence
        progressIndicator = new ConfidenceProgressIndicator();
        progressIndicator.setId("funds-confidence");
        tooltip = new Tooltip("Not used yet");
        progressIndicator.setProgress(0);
        progressIndicator.setPrefSize(24, 24);
        Tooltip.install(progressIndicator, tooltip);

        confidenceListener = walletService.addAddressConfidenceListener(new AddressConfidenceListener(getAddress()) {
            @Override
            public void onTransactionConfidenceChanged(TransactionConfidence confidence) {
                updateConfidence(confidence);
            }
        });

        updateConfidence(walletService.getConfidenceForAddress(getAddress()));


        // balance
        balanceLabel = new Label();
        balanceListener = walletService.addBalanceListener(new BalanceListener(getAddress()) {
            @Override
            public void onBalanceChanged(Coin balance) {
                updateBalance(balance);
            }
        });

        updateBalance(walletService.getBalanceForAddress(getAddress()));
    }

    public void cleanup() {
        walletService.removeAddressConfidenceListener(confidenceListener);
        walletService.removeBalanceListener(balanceListener);
    }

    private void updateBalance(Coin balance) {
        this.balance = balance;
        if (balance != null) {
            if (tradable instanceof Trade) {
                Trade trade = (Trade) tradable;
                Trade.Phase phase = trade.getState().getPhase();
                switch (phase) {
                    case PREPARATION:
                    case TAKER_FEE_PAID:
                        balanceLabel.setText(formatter.formatCoinWithCode(balance) + " locked in deposit");
                        break;
                    case DEPOSIT_REQUESTED:
                    case DEPOSIT_PAID:
                    case FIAT_SENT:
                    case FIAT_RECEIVED:
                        // We ignore the tx fee as it will be paid by both (once deposit, once payout)
                        Coin balanceInDeposit = FeePolicy.SECURITY_DEPOSIT;
                        // For the seller we add the trade amount
                        if (trade.getContract().getSellerAddress().equals(getAddress()))
                            balanceInDeposit.add(trade.getTradeAmount());
                        balanceLabel.setText(formatter.formatCoinWithCode(balanceInDeposit) + " locked in deposit");
                        break;
                    case PAYOUT_PAID:
                        balanceLabel.setText(formatter.formatCoinWithCode(balance) + " in wallet");
                        break;
                    case WITHDRAWN:
                        log.error("Invalid state at updateBalance (WITHDRAWN)");
                        balanceLabel.setText(formatter.formatCoinWithCode(balance) + " already withdrawn");
                        break;
                    case DISPUTE:
                        balanceLabel.setText(formatter.formatCoinWithCode(balance) + " locked because of open ticket");
                        break;
                    default:
                        log.warn("Not supported tradePhase: " + phase);
                }

            } else
                balanceLabel.setText(formatter.formatCoin(balance));
        }
    }

    private void updateConfidence(TransactionConfidence confidence) {
        if (confidence != null) {
            //log.debug("Type numBroadcastPeers getDepthInBlocks " + confidence.getConfidenceType() + " / " +
            // confidence.numBroadcastPeers() + " / " + confidence.getDepthInBlocks());
{'fraction_non_alphanumeric': 0.07592722183344996, 'fraction_numerical': 0.0013995801259622112, 'mean_word_length': 2.477189781021898, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2772199', 'n_tokens_mistral': 2847, 'n_tokens_neox': 2757, 'n_words': 715}