package com.tofly.ghrq_route.module.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;

import com.mgq.base.utils.logger.LogRecorder;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.module.OfflineWork.OfflineWorkTool;
import com.tofly.ghrq_route.module.deviceInfo.DeviceInfoTool;
import com.tofly.ghrq_route.module.deviceInfo.domain.DeviceInfoBean;
import com.tofly.ghrq_route.module.gps.AmapLocationUtils;
import com.tofly.ghrq_route.module.gps.GpsTool;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.common.util.AlarmUtils.AlarmUtils;
import com.tofly.ghrq_route.common.util.app.AppUtil;

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

public class AlarmReceiver extends BroadcastReceiver {
    private PowerManager.WakeLock mWakeLock = null;

    @Override
    public void onReceive(Context context, Intent intent) {
        /*Intent i = new Intent(context, UploadGPSService.class);
        context.startService(i);*/
        //testAlarm(context, intent);
        String action = intent.getAction();
        if (action.equals(UploadGPSService.ACTION_LOCATION)){
            //点亮屏幕
            //AlarmUtils.getInstance().wakeupAndScreenDim();

            boolean isServiceStarted = AppUtil.isServiceStarted(context, UploadGPSService.class.getName());
            if (!isServiceStarted){
                Intent startIntent = new Intent(context, UploadGPSService.class);
                context.startService(startIntent);
            }

            GpsTool gpsTool = GpsTool.getInstance();
            gpsTool.refreshLocationManager();
            //gpsTool.resetTimer();
        }else if (action.equals(DeviceMonitorService.ACTION_DEVICEINFO)){
            boolean isServiceStarted = AppUtil.isServiceStarted(context, DeviceMonitorService.class.getName());
            if (!isServiceStarted){
                Intent startIntent = new Intent(context, DeviceMonitorService.class);
                context.startService(startIntent);
            }

            /*boolean isClientStarted = BaiduLocationUtils.getInstance().isLocationClientStarted();
            if (!isClientStarted){
                BaiduLocationUtils.getInstance().startBaiduService();
            }
            isServiceStarted = AppUtil.isServiceStarted(context, UploadGPSService.class.getName());
            if (!isServiceStarted){
                Intent startIntent = new Intent(context, UploadGPSService.class);
                context.startService(startIntent);
            }*/

            DeviceInfoTool deviceInfoTool = DeviceInfoTool.getInstance();
            deviceInfoTool.checkDeviceInfo(DeviceInfoBean.WORK_STATE_ING);
        }else if (action.equals(Intent.ACTION_SCREEN_OFF)){
            PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            mWakeLock = powerManager.newWakeLock(
                    PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,
                    getClass().getName());
            mWakeLock.acquire();
        }else if (action.equals(AmapLocationUtils.ACTION_AMAP_WAKEUP)){
            AlarmUtils.getInstance().wakeupAndScreenDim();
        }else if (action.equals(OfflineWorkerService.ACTION_OFFLINE_WORK)){
            boolean isServiceStarted = AppUtil.isServiceStarted(context, OfflineWorkerService.class.getName());
            if (!isServiceStarted){
                Intent startIntent = new Intent(context, OfflineWorkerService.class);
                context.startService(startIntent);
            }
            OfflineWorkTool offlineWorkTool = OfflineWorkTool.getInstance();
            offlineWorkTool.checkTodayWork(context);
        }
    }

    public void releaseWakeLock(){
        if (mWakeLock != null){
            mWakeLock.release();
        }
    }

    private void testAlarm(Context context, Intent intent) {
        ToastUtil.showToast(context, intent.getAction());
        AlarmUtils.playNotificationAlarm();

        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean != null) {
            String action = intent.getAction();
            String logMsg = String.format("Alarm_Receiver ：%s \n", action);
            LogRecorder.writeDeviceToolLog(context, logMsg, userBean.getUserid());
        }
    }
}
