package com.example.bem;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Calendar;

public class AlarmRingingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            setShowWhenLocked(true);
            setTurnScreenOn(true);
        } else {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            );
        }
        
        setContentView(R.layout.activity_alarm_ringing);
        
        String title = getIntent().getStringExtra("alarm_title");
        String time = getIntent().getStringExtra("alarm_time");
        String ringtoneUri = getIntent().getStringExtra("ringtone_uri");
        int alarmId = getIntent().getIntExtra("alarm_id", -1);
        
        TextView textTitle = findViewById(R.id.textAlarmTitle);
        TextView textTime = findViewById(R.id.textAlarmTime);
        Button btnSnooze = findViewById(R.id.btnSnooze);
        Button btnStop = findViewById(R.id.btnStop);
        
        if (title != null) {
            textTitle.setText(title);
        }
        if (time != null) {
            textTime.setText(time);
        }
        
        btnSnooze.setOnClickListener(v -> {
            snoozeAlarm(title, ringtoneUri, alarmId);
            stopAlarmService();
            finish();
        });
        
        btnStop.setOnClickListener(v -> {
            stopAlarmService();
            finish();
        });
    }
    
    private void snoozeAlarm(String title, String ringtoneUri, int alarmId) {
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        if (alarmManager == null) return;
        
        Intent intent = new Intent(this, AlarmReceiver.class);
        intent.putExtra("alarm_title", title);
        intent.putExtra("alarm_message", "Hora de tomar: " + title);
        intent.putExtra("ringtone_uri", ringtoneUri);
        
        int snoozeId = alarmId >= 0 ? alarmId + 10000 : (int) System.currentTimeMillis();
        
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this,
                snoozeId,
                intent,
                PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
        );
        
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, 5);
        
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                alarmManager.setExactAndAllowWhileIdle(
                        AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(),
                        pendingIntent
                );
            } else {
                alarmManager.setExact(
                        AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(),
                        pendingIntent
                );
            }
            
            android.widget.Toast.makeText(this, "⏰ Alarme adiado por 5 minutos", android.widget.Toast.LENGTH_LONG).show();
        } catch (SecurityException e) {
            android.widget.Toast.makeText(this, "Erro ao adiar alarme", android.widget.Toast.LENGTH_SHORT).show();
        }
    }
    
    private void stopAlarmService() {
        Intent serviceIntent = new Intent(this, AlarmSoundService.class);
        stopService(serviceIntent);
    }
    
    @Override
    public void onBackPressed() {
        // Não permite voltar sem parar o alarme
    }
}




