package com.example.bem;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;

import androidx.core.app.NotificationCompat;

public class AlarmReceiver extends BroadcastReceiver {
    
    private static final String CHANNEL_ID = "alarm_channel";
    private static final int NOTIFICATION_ID = 1001;
    
    @Override
    public void onReceive(Context context, Intent intent) {
        String title = intent.getStringExtra("alarm_title");
        String message = intent.getStringExtra("alarm_message");
        String ringtoneUriString = intent.getStringExtra("ringtone_uri");
        int alarmId = intent.getIntExtra("alarm_id", -1);
        
        if (title == null) {
            title = "Alarme de Medicamento";
        }
        if (message == null) {
            message = "Hora de tomar o seu medicamento!";
        }
        
        Intent activityIntent = new Intent(context, AlarmRingingActivity.class);
        activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
                                Intent.FLAG_ACTIVITY_CLEAR_TOP |
                                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        activityIntent.putExtra("alarm_title", title);
        activityIntent.putExtra("alarm_time", new java.text.SimpleDateFormat("HH:mm", java.util.Locale.getDefault()).format(new java.util.Date()));
        activityIntent.putExtra("ringtone_uri", ringtoneUriString);
        activityIntent.putExtra("alarm_id", alarmId);
        context.startActivity(activityIntent);
        
        Intent serviceIntent = new Intent(context, AlarmSoundService.class);
        serviceIntent.putExtra("alarm_title", title);
        serviceIntent.putExtra("ringtone_uri", ringtoneUriString);
        
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            context.startForegroundService(serviceIntent);
        } else {
            context.startService(serviceIntent);
        }
        
        createNotificationChannel(context);
        showNotification(context, title, message, ringtoneUriString, alarmId);
    }
    
    private void createNotificationChannel(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Alarmes de Medicação";
            String description = "Notificações para lembretes de medicamentos";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            channel.enableVibration(true);
            channel.enableLights(true);
            
            NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
    }
    
    private void showNotification(Context context, String title, String message, String ringtoneUriString, int alarmId) {
        Intent activityIntent = new Intent(context, AlarmRingingActivity.class);
        activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activityIntent.putExtra("alarm_title", title);
        activityIntent.putExtra("alarm_time", new java.text.SimpleDateFormat("HH:mm", java.util.Locale.getDefault()).format(new java.util.Date()));
        activityIntent.putExtra("ringtone_uri", ringtoneUriString);
        activityIntent.putExtra("alarm_id", alarmId);
        
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context, 
                alarmId >= 0 ? alarmId : 0, 
                activityIntent, 
                PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
        );
        
        Intent stopIntent = new Intent(context, AlarmSoundService.class);
        stopIntent.setAction("STOP_ALARM");
        PendingIntent stopPendingIntent = PendingIntent.getService(
                context,
                alarmId >= 0 ? alarmId + 1000 : 1000,
                stopIntent,
                PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
        );
        
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
                .setContentTitle("⏰ " + title)
                .setContentText(message)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setCategory(NotificationCompat.CATEGORY_ALARM)
                .setFullScreenIntent(pendingIntent, true)
                .setOngoing(true)
                .setAutoCancel(false)
                .setContentIntent(pendingIntent)
                .addAction(android.R.drawable.ic_media_pause, "Parar", stopPendingIntent);
        
        NotificationManager notificationManager = 
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.notify(NOTIFICATION_ID, builder.build());
        }
    }
    
}

