package com.samourai.wallet.send;

import android.content.Context;

import com.samourai.wallet.SamouraiWallet;
import com.samourai.wallet.api.APIFactory;

import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionInput;
import org.bouncycastle.util.encoders.Hex;

import java.util.HashMap;

public class UTXOFactory {

    private static Context context = null;

    private static UTXOFactory instance = null;

    private static HashMap<String,UTXO> p2pkh = null;
    private static HashMap<String,UTXO> p2sh_p2wpkh = null;
    private static HashMap<String,UTXO> p2wpkh = null;
    private static HashMap<String,UTXO> postMix = null;

    private UTXOFactory() { ; }

    public static UTXOFactory getInstance() {

        if(instance == null) {
            instance = new UTXOFactory();

            p2pkh = new HashMap<String,UTXO>();
            p2sh_p2wpkh = new HashMap<String,UTXO>();
            p2wpkh = new HashMap<String,UTXO>();
            postMix = new HashMap<String,UTXO>();
        }

        return instance;
    }

    public static UTXOFactory getInstance(Context ctx) {

        context = ctx;

        if(instance == null) {
            instance = new UTXOFactory();

            p2pkh = new HashMap<String,UTXO>();
            p2sh_p2wpkh = new HashMap<String,UTXO>();
            p2wpkh = new HashMap<String,UTXO>();
            postMix = new HashMap<String,UTXO>();
        }

        return instance;
    }

    public void clear() {
        p2pkh.clear();
        p2sh_p2wpkh.clear();
        p2wpkh.clear();
        postMix.clear();
    }

    public HashMap<String,UTXO> getP2PKH() {
        return p2pkh;
    }

    public void setP2PKH(HashMap<String,UTXO> p2pkh) {
        UTXOFactory.p2pkh = p2pkh;
    }

    public HashMap<String,UTXO> getP2SH_P2WPKH() {
        return p2sh_p2wpkh;
    }

    public void setP2SH_P2WPKH(HashMap<String,UTXO> p2sh_p2wpkh) {
        UTXOFactory.p2sh_p2wpkh = p2sh_p2wpkh;
    }

    public HashMap<String,UTXO> getP2WPKH() {
        return p2wpkh;
    }

    public void setP2WPKH(HashMap<String,UTXO> p2wpkh) {
        UTXOFactory.p2wpkh = p2wpkh;
    }

    public HashMap<String,UTXO> getPostMix() {
        return postMix;
    }

    public void setPostMix(HashMap<String,UTXO> postMix) {
        UTXOFactory.postMix = postMix;
    }

    public void addP2PKH(String script, UTXO utxo)  {
        p2pkh.put(script, utxo);
    }

    public void addP2SH_P2WPKH(String script, UTXO utxo)  {
        p2sh_p2wpkh.put(script, utxo);
    }

    public void addP2WPKH(String script, UTXO utxo)  {
        p2wpkh.put(script, utxo);
    }

    public void addPostMix(String script, UTXO utxo)  {
        postMix.put(script, utxo);
    }

    public long getTotalP2PKH() {

        long ret = 0L;

        for(UTXO utxo : p2pkh.values())   {
            ret += utxo.getValue();
        }

        return ret;
    }

    public long getTotalP2SH_P2WPKH() {

        long ret = 0L;

        for(UTXO utxo : p2sh_p2wpkh.values())   {
            ret += utxo.getValue();
        }

        return ret;
    }

    public long getTotalP2WPKH() {

        long ret = 0L;

        for(UTXO utxo : p2wpkh.values())   {
            ret += utxo.getValue();
        }

        return ret;
    }

    public long getTotalPostMix() {

        long ret = 0L;

        for(UTXO utxo : postMix.values())   {
            ret += utxo.getValue();
        }

        return ret;
    }

    public int getCountP2PKH() {

        int ret = 0;

        for(UTXO utxo : p2pkh.values())   {
            ret += utxo.getOutpoints().size();
        }

        return ret;
    }

    public int getCountP2SH_P2WPKH() {

        int ret = 0;

        for(UTXO utxo : p2sh_p2wpkh.values())   {
            ret += utxo.getOutpoints().size();
        }

        return ret;
    }

    public int getCountP2WPKH() {

        int ret = 0;

        for(UTXO utxo : p2wpkh.values())   {
            ret += utxo.getOutpoints().size();
        }

        return ret;
    }

    public int getCountPostMix() {

        int ret = 0;

        for(UTXO utxo : postMix.values())   {
            ret += utxo.getOutpoints().size();
        }

        return ret;
    }

    public void markUTXOAsUnspendable(String hexTx)    {

        HashMap<String, Long> utxos = new HashMap<String,Long>();

        for(UTXO utxo : APIFactory.getInstance(context).getUtxos(true))   {
            for(MyTransactionOutPoint outpoint : utxo.getOutpoints())   {
                utxos.put(outpoint.getTxHash().toString() + "-" + outpoint.getTxOutputN(), outpoint.getValue().longValue());
            }
        }

        Transaction tx = new Transaction(SamouraiWallet.getInstance().getCurrentNetworkParams(), Hex.decode(hexTx));
        for(TransactionInput input : tx.getInputs())   {
            BlockedUTXO.getInstance().add(input.getOutpoint().getHash().toString(), (int)input.getOutpoint().getIndex(), utxos.get(input.getOutpoint().getHash().toString() + "-" + (int)input.getOutpoint().getIndex()));
        }

    }

}

@@ -1668,644 +1668,220 @@
 lic 
-void setP2PKH(HashMap%3CString,UTXO%3E p2pkh) %7B%0A        UTXOFactory.p2pkh = p2pkh;%0A    %7D%0A%0A    public HashMap%3CString,UTXO%3E getP2SH_P2WPKH() %7B%0A        return p2sh_p2wpkh;%0A    %7D%0A%0A    public void setP2SH_P2WPKH(HashMap%3CString,UTXO%3E p2sh_p2wpkh) %7B%0A        UTXOFactory.p2sh_p2wpkh = p2sh_p2wpkh;%0A    %7D%0A%0A    public HashMap%3CString,UTXO%3E getP2WPKH() %7B%0A        return p2wpkh;%0A    %7D%0A%0A    public void setP2WPKH(HashMap%3CString,UTXO%3E p2wpkh) %7B%0A        UTXOFactory.p2wpkh = p2wpkh;%0A    %7D%0A%0A    public HashMap%3CString,UTXO%3E getPostMix() %7B%0A        return postMix;%0A    %7D%0A%0A    public void setPostMix(HashMap%3CString,UTXO%3E postMix) %7B%0A        UTXOFactory.postMix =
+HashMap%3CString,UTXO%3E getP2SH_P2WPKH() %7B%0A        return p2sh_p2wpkh;%0A    %7D%0A%0A    public HashMap%3CString,UTXO%3E getP2WPKH() %7B%0A        return p2wpkh;%0A    %7D%0A%0A    public HashMap%3CString,UTXO%3E getPostMix() %7B%0A        return
  pos

remove set map methods