
package ai.pai.client.wxapi;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class PayListenerManager {
    private static PayListenerManager instance = null;

    private PayListenerManager() {
    }

    public static synchronized PayListenerManager getInstance() {
        if (instance == null) {
            instance = new PayListenerManager();
        }
        return instance;
    }

    private HashMap<String, PayResultListener> payResultListeners = new HashMap<String, PayResultListener>();

    public HashMap<String, PayResultListener> getPayResultListeners() {
        return payResultListeners;
    }

    public void putPayResultListener(String from, PayResultListener payResultListener) {
        payResultListeners.put(from, payResultListener);
    }

    public void removePayResultListener(String from) {
        payResultListeners.remove(from);
    }

    public void onResult(String transaction,int code) {
        try {
            if (payResultListeners != null && payResultListeners.size() > 0) {
                Iterator<Entry<String, PayResultListener>> iter = payResultListeners.entrySet()
                        .iterator();
                while (iter.hasNext()) {
                    Entry<String, PayResultListener> entry = (Entry<String, PayResultListener>) iter.next();
                    Object val = entry.getValue();
                    PayResultListener listener = (PayResultListener) val;
                    listener.onResult(transaction,code);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
