package com.nurulwafa.nufakuproject;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.nurulwafa.nufakuproject.R;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String CHANNEL_ID = "NUFA_CHANNEL_2";

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        String title = "Notifikasi Baru";
        String body = "Ada pesan masuk";

        if (remoteMessage.getNotification() != null) {
            title = remoteMessage.getNotification().getTitle();
            body = remoteMessage.getNotification().getBody();
        } else if (remoteMessage.getData().size() > 0) {
            title = remoteMessage.getData().get("title");
            body = remoteMessage.getData().get("body");
        }

        String idSantri = remoteMessage.getData().get("id_santri");
        String idWaliSantri = remoteMessage.getData().get("id_wali_santri");
        String kondisi = remoteMessage.getData().get("kondisi_kesehatan");
        String type = remoteMessage.getData().get("type");
        String apiUrl = remoteMessage.getData().get("api_url");

        Intent intent;
        if ("kesehatan".equalsIgnoreCase(type)) {
            intent = new Intent(this, HomeActivity.class);
            intent.putExtra("id_santri", idSantri);
            intent.putExtra("id_wali_santri", idWaliSantri);
            intent.putExtra("kondisi_kesehatan", kondisi);
            intent.putExtra("from_notif", true);
        } else if ("pelanggaran".equalsIgnoreCase(type)) {
            intent = new Intent(this, PelanggaranSantriActivity.class);
            intent.putExtra("login_key", "id_santri");
            intent.putExtra("login_value", idSantri);
            intent.putExtra("from_notif", true);
        } else if ("berita".equalsIgnoreCase(type)) {
            intent = new Intent(this, DetailBeritaActivity.class);
            intent.putExtra("from_notif", true);
            intent.putExtra("judul", remoteMessage.getData().get("judul"));
            intent.putExtra("gambar", remoteMessage.getData().get("gambar"));
            intent.putExtra("tanggal", remoteMessage.getData().get("tanggal"));
            intent.putExtra("deskripsi", remoteMessage.getData().get("deskripsi"));
        } else {
            intent = new Intent(this, HomeActivity.class);
        }

        PendingIntent pendingIntent = PendingIntent.getActivity(
                this,
                0,
                intent,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE
        );

        // Deklarasikan di atas agar bisa digunakan dua kali
        Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/nufa_alert");
        Log.d("NOTIF_SOUND", "Sound URI: " + soundUri.toString());

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.logo)
                .setContentTitle(title)
                .setContentText(body)
                .setAutoCancel(true)
                .setColor(Color.GREEN)
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setSound(soundUri); // dipakai di sini

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .build();

            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "Notifikasi NUFA",
                    NotificationManager.IMPORTANCE_HIGH
            );
            channel.setDescription("Channel untuk notifikasi");
            channel.enableLights(true);
            channel.enableVibration(true);
            channel.setSound(soundUri, audioAttributes); // dipakai di sini juga

            notificationManager.createNotificationChannel(channel);
        }

        // BARIS INI YANG BELUM ADA
        notificationManager.notify(1, builder.build());
    }
}

