package com.zkjc.common.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import com.zkjc.common.base.ScreenActivity;

import static com.jess.arms.utils.ArmsUtils.startActivity;

public class StartAppScreen {
    public final static int MSG_SHOW_TIPS = 0x01;
    private static StartAppScreen instance;
    private Context mContext;
    private boolean tipsIsShowed = true;
    private final MainHandler mMainHandler;

    public StartAppScreen(Context mContext) {
        this.mContext = mContext;
        mMainHandler = new MainHandler();

    }

    public static StartAppScreen getInstance(Context context) {
        if (instance == null) {
            synchronized (StartAppScreen.class) {
                if (instance == null) {
                    instance = new StartAppScreen(context);
                }
            }
        }

        return instance;
    }

    private Runnable tipsShowRunable = new Runnable() {

        @Override
        public void run() {
            mMainHandler.obtainMessage(MSG_SHOW_TIPS).sendToTarget();
        }
    };

    /**
     * <无操作时开始计时>
     * <功能详细描述>
     *
     * @see [类、类#方法、类#成员]
     */
    public void startTipsTimer() {
        mMainHandler.postDelayed(tipsShowRunable, 2*60*1000);
    }

    /**
     * <结束当前计时,重置计时>
     * <功能详细描述>
     *
     * @see [类、类#方法、类#成员]
     */
    public void endTipsTimer() {
        mMainHandler.removeCallbacks(tipsShowRunable);
    }

    public void resetTipsTimer() {
        tipsIsShowed = false;
        mMainHandler.removeCallbacks(tipsShowRunable);
        mMainHandler.postDelayed(tipsShowRunable, 2*60*1000);
    }

    public class MainHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_SHOW_TIPS:
                    //展示屏保界面
                    Intent intent = new Intent(mContext, ScreenActivity.class);
                    startActivity(intent);

                    tipsIsShowed = true;
                    // 屏保显示,两秒内连续按下键盘Enter键可关闭屏保
                    break;
            }

        }

    }

}
