package hh.dejsem.fm;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import hh.dejsem.K;
import hh.dejsem.R;
import hh.lib.D;

class BarNotification {
	/**
	 * ----------------------------------------------------------
	 * notification in Notification Bar
	 * ----------------------------------------------------------
	 */
	D d;
	final static int id = K.TRANSFER_NOTIFICATION_ID;
	NotificationManager notifManager;
	Notification notif;
	/* ----------------------------------------------------------*/

	BarNotification(D d, Class<?> cls) {
		this.d = d.klon(this);
		final Context c = this.d.getContext();
		notifManager = (NotificationManager)c.getSystemService(Context.NOTIFICATION_SERVICE);
		final Intent notifIntent = new Intent(c, cls).putExtra(K.ACTION_KEY, K.TRANSFER_DISPLAY_LIST);
		final Intent deleteIntent     // restore notification icon in notif bar when cleared
				= new Intent(c, cls).putExtra(K.ACTION_KEY, K.TRANSFER_PIN_NOTIFICATION);
		notif = new Notification.Builder(c)
				.setSmallIcon(R.drawable.ic_launcher)    // mandatory
				.setContentTitle("dejsem progress")
					/* - FLAG_UPDATE_CURRENT znamená, že nový výskyt aktivity v případě, že aktivita už běží se napojí na stávající
					*  - 2.arg u getActivity() odlišuje aktivitu od jiné aktivity téže třídy*/
				.setContentIntent(PendingIntent.getActivity(c, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT))
				.setDeleteIntent(PendingIntent.getActivity(c, 1, deleteIntent, 0))
				.build();
		notif.flags |= Notification.FLAG_NO_CLEAR;

		// alternativa
		/*Notification.Builder notifBuilder;
		TaskStackBuilder stackBuilder;
		stackBuilder = TaskStackBuilder.create(c);
		// stackBuilder.addParentStack(cls); 			// Adds the back stack for the Intent (but not the Intent itself)
		stackBuilder.addNextIntent(notifIntent); 	// Adds the Intent that starts the Activity to the top of the stack
		notifBuilder.setContentIntent(stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));*/
	}

	void pin() {
		d.l(4, "notification pin");
		notifManager.notify(id, notif);
	}

	void cut() {
		notifManager.cancelAll();
	}
}
