package com.ccwangluo.ccim.manage;

import android.os.Handler;
import android.os.Looper;

import com.ccwangluo.ccim.listener.ResultEventCallback;
import com.ccwangluo.ccim.util.ErrorCode;

import java.util.HashMap;

public class CallBackManager<T> {
    private final HashMap<Integer, ResultEventCallback<T>> callbackHashMap = new HashMap<>();

    private static final int time_out = 20 * 1000;


    public void addCallBack(int id, ResultEventCallback<T> resultEventCallback) {
        callbackHashMap.put(id, resultEventCallback);
        //设置60秒超时
        new Handler(Looper.getMainLooper()).postDelayed(() -> {
            ResultEventCallback<T> callBack = getCallBack(id);
            if (callBack != null && id != 0){
                removeCallBack(id);
                callBack.onFailed(ErrorCode.NET_TIME_OUT);
            }
        },time_out);
    }

    public ResultEventCallback<T> getCallBack(int id) {
        return callbackHashMap.get(id);
    }

    public void removeCallBack(int id) {
        callbackHashMap.remove(id);
    }

}
