package com.tofly.ghrq_route.common.util.AlarmUtils;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Handler;
import android.os.PowerManager;
import android.os.Vibrator;
import android.view.WindowManager;

import com.tofly.ghrq_route.base.BaseApp;

/**
 * Created by Amu on 2017/4/20.
 */

public class AlarmUtils {
    private static final String TAG = AlarmUtils.class.getName();

    private static AlarmUtils alarmUtils = null;

    private Context mContext;

    private AlarmUtils(Context context){
        mContext = context;
    }

    public static synchronized AlarmUtils getInstance(){
        if (alarmUtils == null){
            Context context = BaseApp.getInstance();
            alarmUtils = new AlarmUtils(context);
        }
        return alarmUtils;
    }

    //开启轮询服务
    public void startBroadcast(int time, Class<?> cls, String action, int requetCode){
        //获取AlarmManager系统服务
        AlarmManager manager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(mContext, cls);
        intent.setAction(action);
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, requetCode,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        long triggerTime = System.currentTimeMillis() + time;
        manager.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, time, pendingIntent);
    }

    public void stopBroadcast(Class<?> cls, String action, int requetCode){
        AlarmManager manager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(mContext, cls);
        intent.setAction(action);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, requetCode,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        manager.cancel(pendingIntent);
    }

    private static final int screenOffDelay = 60 * 1000;    //30秒后关闭屏幕
    public void wakeupAndScreenDim(){
        PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        boolean screen = powerManager.isScreenOn();
        if (!screen){
            final PowerManager.WakeLock wakeLock = powerManager.newWakeLock(
                    PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG);
            wakeLock.acquire();

            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    wakeLock.release();
                }
            }, screenOffDelay);
        }
    }

    public static void playNotificationAlarm(){
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone ringtone = RingtoneManager.getRingtone(BaseApp.getInstance(), uri);
        ringtone.play();
    }

    public static void playNotificationVibrate(Context context){
        if (context == null)
            context = BaseApp.getInstance().getApplicationContext();
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        long[] pattern = {100, 400, 100, 400};   // 停止 开启 停止 开启
        vibrator.vibrate(pattern, -1);   //重复两次上面的pattern 如果只想震动一次，index设为-1
    }

    public static void keepScreenOn(Activity context){
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
}
