package com.yztiot.streetlamp;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import java.util.Calendar;

public class BootCompleteReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            // 读取文件并设置AlarmManager
            SharedPreferences prefs = context.getSharedPreferences(MainActivity.PREFS_NAME, Context.MODE_PRIVATE);
            int switchValue = prefs.getInt(MainActivity.SWITCH_KEY,-1);
            Log.d("BootCompleteReceiver","BootCompleteReceiver: "+switchValue);
            Toast.makeText(context, "switchValue: "+switchValue, Toast.LENGTH_SHORT).show();
            if (switchValue == 1){
                setAlarms(context);
            }
        }
    }

    private void setAlarms(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(MainActivity.PREFS_NAME, Context.MODE_PRIVATE);
        int startHour = prefs.getInt(MainActivity.START_HOUR_KEY, -1);
        int startMinute = prefs.getInt(MainActivity.START_MINUTE_KEY, -1);
        int endHour = prefs.getInt(MainActivity.END_HOUR_KEY, -1);
        int endMinute = prefs.getInt(MainActivity.END_MINUTE_KEY, -1);




        if (startHour != -1 && startMinute != -1 && endHour != -1 && endMinute != -1) {
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

            // 设置开始时间
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.set(Calendar.HOUR_OF_DAY, startHour);
            startCalendar.set(Calendar.MINUTE, startMinute);
            startCalendar.set(Calendar.SECOND, 0);

            if (startCalendar.getTimeInMillis() < System.currentTimeMillis()) {
                startCalendar.add(Calendar.DAY_OF_YEAR, 1);
            }

            Intent startIntent = new Intent(context, TimingReceiver.class);
            startIntent.setAction("com.yztiot.streetlamp.START_ALARM");
            PendingIntent startPendingIntent = PendingIntent.getBroadcast(context, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, startCalendar.getTimeInMillis(), startPendingIntent);
            Log.d("BootCompleteReceiver", "Start alarm set for: " + startCalendar.getTime());

            // 设置结束时间
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.set(Calendar.HOUR_OF_DAY, endHour);
            endCalendar.set(Calendar.MINUTE, endMinute);
            endCalendar.set(Calendar.SECOND, 0);

            if (endCalendar.getTimeInMillis() < System.currentTimeMillis()) {
                endCalendar.add(Calendar.DAY_OF_YEAR, 1);
            }

            Intent endIntent = new Intent(context, TimingReceiver.class);
            endIntent.setAction("com.yztiot.streetlamp.END_ALARM");
            PendingIntent endPendingIntent = PendingIntent.getBroadcast(context, 1, endIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, endCalendar.getTimeInMillis(), endPendingIntent);
            Log.d("BootCompleteReceiver", "End alarm set for: " + endCalendar.getTime());
        } else {
            Log.d("BootCompleteReceiver", "No valid time settings found");
        }
    }
}