package com.tofly.ghrq_route.module.gps;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.RequiresApi;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.common.ui.activity.HomeActivity;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by hasee on 2018/3/20.
 */

public class DService extends Service {
    Intent nfIntent = null;
    private final IBinder binder = new MyBinder();
    public DService(){
    }
    public class MyBinder extends Binder {
       public DService getService() {
            return DService.this;
        }
    }
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public IBinder onBind(Intent intent) {
        constructNotification();
        timer.schedule(task, 0, 10000);
        return binder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
//        constructNotification();
//        timer.schedule(task, 0, 10000);
        return super.onStartCommand(intent, flags, startId);

    }

    //构建前台service
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    private void constructNotification() {
        // 在API11之后构建Notification的方式
        Notification.Builder builder = new Notification.Builder(this.getApplicationContext()); //获取一个Notification构造器
        nfIntent = new Intent(this, HomeActivity.class);
        builder.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0))
                .setContentTitle("上传GPS") // 设置下拉列表里的标题
                .setContentText("巡检")// 设置上下文内容
                .setSmallIcon(R.mipmap.abc_setting_2);

        Notification notification = builder.build(); // 获取构建好的Notification
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
        notification.flags = Notification.FLAG_ONGOING_EVENT;
        notification.flags |= Notification.FLAG_NO_CLEAR;
        notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
        // 参数一：唯一的通知标识；参数二：通知消息。
        this.startForeground(110, notification);// 开始前台服务
    }

    @Override
    public void onDestroy() {
        stopForeground(true);// 停止前台服务--参数：表示是否移除之前的通知
        super.onDestroy();
    }
    private Timer timer = new Timer();
    private TimerTask task = new TimerTask() {
        @Override
        public void run() {

           /* String userid = "";
            UserBean userBean = BaseApp.getInstance().getLoginUser();
            if (userBean != null)
                userid = userBean.getUserid();
           boolean isAlive =  CheckUtil.isServiceWorked(DService.this, C.BAIDU_SERVICE_NAME);
           boolean isClentAlive = BaiduLocationUtils.getInstance().isLocationClientStarted();
            Log.d("CheckUtil","监测百度定位服务是否存活:"+isAlive + " locationClient是否开启: " + isClentAlive);
            String logMsg = "监测百度定位服务是否存活:" + isAlive + " locationClient是否开启: " + isClentAlive;
            LogRecorder.writeBaiduLog(DService.this, logMsg, userid);
           if((!isAlive || !isClentAlive) && listener!=null){
                listener.notAlive();
               /*logMsg = "百度定位服务已关闭，重新开启服务";
               LogRecorder.writeBaiduLog(DService.this, logMsg, userid);
            }*/
        }
    };

    private CheckProcessAlive listener;
    public interface CheckProcessAlive{
        void notAlive();
    }
    public void setAliveListener(CheckProcessAlive listener){
        this.listener = listener;
    }

}
