package com.rnboat.process.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.rnboat.framework.ConfigManager;
import com.rnboat.framework.base.BaseActivity;
import com.rnboat.framework.base.RNBoatApplication;
import com.rnboat.framework.diskcache.AsyncDiskCache;
import com.rnboat.framework.model.AppConfig;
import com.rnboat.process.RNBoatDaemonManager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * 接收唤醒应用核心服务的广播接收器，接收守护进程发送过来的广播
 *
 */
public class RNBoatWakeReceiver extends BroadcastReceiver {

    private final static String TAG = RNBoatWakeReceiver.class.getSimpleName();

    // 定义守护进程发送唤醒广播的 Action
    public final static String DAEMON_WAKE_ACTION = "com.rnboat.process.daemon.wake";
    public final static String DAEMON_BOOT_ACTION = "android.intent.action.BOOT_COMPLETED";


    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(DAEMON_WAKE_ACTION)) {
            Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int date = c.get(Calendar.DATE);
            String today = String.format("%d/%d/%d", year, month, date);

            ArrayList rebootedDays = AsyncDiskCache.syncReadCache(ConfigManager.APP_REBOOT_KEY, ArrayList.class);
            if (rebootedDays == null) {
                rebootedDays = new ArrayList<>();
                AsyncDiskCache.syncWriteCache(ConfigManager.APP_REBOOT_KEY, new ArrayList<>());
            }

            if (hour == ConfigManager.APP_REBOOT_HOUR && !rebootedDays.contains(today)){
                rebootedDays.add(today);
                AsyncDiskCache.syncWriteCache(ConfigManager.APP_REBOOT_KEY, rebootedDays);

                Log.i(TAG, "onReceive: 重启了！");
                Intent finishIntent = new Intent();
                finishIntent.setAction(RNBoatApplication.APP_FINISH_ACTION);
                context.sendBroadcast(finishIntent);
            }
            else {
                Log.i(TAG, "onReceive: 起床了！");
                // 接收到守护进程发送的广播，开始启动核心进程
                RNBoatDaemonManager.startCoreProcess(context);
            }
        } else if (action.equals(DAEMON_BOOT_ACTION)) {
            Log.i(TAG, "onReceive: Boot！");
            AppConfig appConfig = AsyncDiskCache.syncReadCache(ConfigManager.APP_CONFIG_KEY, AppConfig.class);
            if (appConfig != null && appConfig.getApp() != null && appConfig.getApp().isDaemon()) {
                RNBoatDaemonManager.startCoreProcess(context);
            }
        }  else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
            Log.i(TAG, "onReceive: 锁屏了！");
            // 检测到锁屏，启动一像素的流氓 Activity
        } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
            Log.i(TAG, "onReceive: 解锁了！");
            // 解锁后，结束一像素的流氓 Activity
        }
    }
}
