package com.foxx.androidcast;

/*********************************************************************
 * CastNotifications.java
 * Created at: Fri 15 May 2026 16:49:02 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class CastNotifications
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (3 commits, 68 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	80a4c9129979483b88fd61c08595abf0cccda2057155620e1a5a9bafec97641e
**********************************************************************/
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;

import androidx.core.app.NotificationCompat;

public final class CastNotifications {
    public static final String CHANNEL_CAST = "cast_channel";
    public static final String CHANNEL_RECEIVE = "receive_channel";
    public static final int ID_SENDER = 1;
    public static final int ID_RECEIVER = 2;

    private CastNotifications() {}

    public static void createChannels(Service service) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            return;
        }
        NotificationManager nm = service.getSystemService(NotificationManager.class);
        NotificationChannel cast = new NotificationChannel(
                CHANNEL_CAST, "Screen cast", NotificationManager.IMPORTANCE_LOW);
        NotificationChannel recv = new NotificationChannel(
                CHANNEL_RECEIVE, "Cast receiver", NotificationManager.IMPORTANCE_LOW);
        nm.createNotificationChannel(cast);
        nm.createNotificationChannel(recv);
    }

    public static Notification sender(Service service, String text, Class<?> serviceClass) {
        return build(service, CHANNEL_CAST, ID_SENDER, text, serviceClass, "com.foxx.androidcast.STOP_SEND");
    }

    public static Notification receiver(Service service, String text, Class<?> serviceClass) {
        return build(service, CHANNEL_RECEIVE, ID_RECEIVER, text, serviceClass, "com.foxx.androidcast.STOP_RECV");
    }

    private static Notification build(
            Service service, String channel, int id, String text,
            Class<?> serviceClass, String stopAction) {
        PendingIntent pi = PendingIntent.getActivity(
                service, 0, CastActivityTracker.resumeIntent(service),
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        Intent stop = new Intent(service, serviceClass);
        stop.setAction(stopAction);
        PendingIntent stopPi = PendingIntent.getService(
                service, id, stop, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        return new NotificationCompat.Builder(service, channel)
                .setContentTitle(service.getString(R.string.app_name))
                .setContentText(text)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentIntent(pi)
                .addAction(0, service.getString(R.string.stop), stopPi)
                .setOngoing(true)
                .build();
    }
}
