package com.gc.call;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;

public class CallManage {

    private String TAG = "CallManage";

    private Map<String, CallTarget> map = new HashMap<>();
    private Handler handler = new Handler(Looper.getMainLooper());

    private static CallManage callManage;

    public static CallManage getInstance() {
        if (callManage == null) {
            callManage = new CallManage();
        }
        return callManage;
    }

    public void addTarget(CallTarget callTarget) {
        map.put(callTarget.getTag(), callTarget);
    }

    public void handleTarget(final CallParticipationBean bean) {
        Log.i(TAG, "handleTarget:" + bean.getTag());
        CallTarget callTarget = map.get(bean.getTag());
        if (callTarget == null) {
            if (bean.getListener() != null) {
                CallReturnBean callReturn = new CallReturnBean();
                callReturn.setMsg("没有找到对应的CallTarget");
                callReturn.setCode(CallConstant.CODE_NO_TARGET);
                bean.getListener().onReturn(callReturn);
                bean.getListener().onReturn(callReturn);
            }
        } else {
            if (callTarget instanceof CallImmediatelyHandlerTarget) {
                onImmediatelyHandler(bean, (CallImmediatelyHandlerTarget) callTarget);
            } else if (callTarget instanceof CallTimeHandTarget) {

            }
        }
    }

    private void onImmediatelyHandler(final CallParticipationBean bean, final CallImmediatelyHandlerTarget target) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (bean.getLooper() == CallConstant.LOOPER_MAIN) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            CallReturnBean c = target.onHandle(bean);
                            if (bean.getListener() != null) {
                                bean.getListener().onReturn(c);
                            }
                        }
                    });
                } else if (bean.getLooper() == CallConstant.LOOPER_NO_MAIN) {
                    AsyncTask asyncTask = new AsyncTask() {
                        @Override
                        protected Object doInBackground(Object[] objects) {
                            CallReturnBean c = target.onHandle(bean);
                            if (bean.getListener() != null) {
                                bean.getListener().onReturn(c);
                            }
                            return null;
                        }
                    };
                    asyncTask.execute();
                } else if (bean.getLooper() == CallConstant.LOOPER_NO_MAIN_HANDLER_AND_MIAN_RETURN) {
                    AsyncTask asyncTask = new AsyncTask() {

                        @Override
                        protected CallReturnBean doInBackground(Object[] objects) {
                            return target.onHandle(bean);
                        }

                        @Override
                        protected void onPostExecute(Object o) {
                            super.onPostExecute(o);
                            if (bean.getListener() != null) {
                                bean.getListener().onReturn((CallReturnBean) o);
                            }
                        }
                    };
                    asyncTask.execute();
                }
            }
        });
    }

//    private CallReturnBean onImmediatelyHandle(CallParticipationBean bean) {
//        CallTarget callTarget = map.get(bean.getTag());
//        if (callTarget != null) {
//            if (callTarget instanceof CallImmediatelyHandlerTarget) {
//                return ((CallImmediatelyHandlerTarget) callTarget).onHandle(bean);
//            }
//        } else {
//            CallReturnBean callReturn = new CallReturnBean();
//            callReturn.setMsg("没有找到对应的CallTarget");
//            callReturn.setCode(CallConstant.CODE_NO_TARGET);
//            bean.getListener().onReturn(callReturn);
//            return callReturn;
//        }
//        return null;
//    }

}
