package com.xxfc.rv.other;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.text.TextUtils;
import android.widget.Toast;

import com.xxfc.rv.RvClientApplication;
import com.yuyife.okgo.OkGoUtil;

public class ActivityStartCheckUtils {
    private static final int TIME_DELAY = 600;
    private static ActivityStartCheckUtils sInstance;
    private boolean mPostDelayIsRunning;
    private String mClassName;
    //    private PermissionGuideDialog mDialog;
    private Handler mHhandler = new Handler();


    private ActivityStartCheckUtils() {
    }

    public static ActivityStartCheckUtils getInstance() {
        if (sInstance == null) {
            synchronized (ActivityStartCheckUtils.class) {
                if (sInstance == null) {
                    sInstance = new ActivityStartCheckUtils();
                }
            }
        }
        return sInstance;
    }

    //这里是倒计时完成后的判断逻辑
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            mPostDelayIsRunning = false;
            // 判断要打开的Activity是不是已经在栈顶了
            if (!isActivityOnTop()) {
                // context 这里根据自己项目具体处理 能获得context就行
                Context context = OkGoUtil.application;//这个getAppContext需要自行修改
                if (context != null ) {
//                    if (mDialog == null) {
//                        // 自定义的Dialog，这个代码就没必要贴了
//                        mDialog = new PermissionGuideDialog(context, mToken);
//                    }
//                    mDialog.setCancelable(false);
//                    mDialog.show();
                    Toast.makeText(context, "44444", Toast.LENGTH_SHORT).show();
                }
            }
        }
    };

    public void startActivity(Context context, Intent intent, String className) {
        if (context == null || intent == null || TextUtils.isEmpty(className)) {
            return;
        }
        context.startActivity(intent);
        mClassName = className;
        if (mPostDelayIsRunning) {
            mHhandler.removeCallbacks(mRunnable);
        }
        mPostDelayIsRunning = true;

        mHhandler.postDelayed(mRunnable, TIME_DELAY);
    }

    private boolean isActivityOnTop() {
        boolean result = false;
        String topActivityName = CustomActivityManager.getTopActivity();
        if (!TextUtils.isEmpty(topActivityName)) {
            if (topActivityName.contains(mClassName)) {
                result = true;
            }
        }
        return result;
    }
}
