package org.SHS.Pult;

import android.os.Build;
import android.graphics.Color;
import android.graphics.drawable.Icon;
import android.media.RingtoneManager;
import android.media.AudioAttributes;
import android.net.Uri;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.NotificationChannel;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.Context;
import android.content.pm.ServiceInfo;

public class MainService extends org.qtproject.qt5.android.bindings.QtService {

	public static MainService m_instance;
	private static NotificationButtons m_br;

	private NotificationChannel m_notificationChannel;
	private NotificationManager ntf_manager;
	private Notification.Builder ntf_builder;

	private static String CHANNEL_FRG_ID = "shs_frg_ch";
	private static String CHANNEL_NTF_ID = "shs_ntf_ch";


	public MainService() {
	}


	public static void start(Context ctx) {
		//notify("service starting ...");
		ctx.startService(new Intent(ctx, MainService.class));
	}


//try {
//} catch (Exception e) {e.printStackTrace();}

	@Override
	public void onCreate() {
		super.onCreate();
		m_instance = this;

		ntf_manager = getSystemService(NotificationManager.class);
		//ntf_manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

		if (Build.VERSION.SDK_INT >= 26) {
			NotificationChannel channel;

			// foreground
			channel = new NotificationChannel(CHANNEL_FRG_ID, getString(R.string.server_status), NotificationManager.IMPORTANCE_DEFAULT);
			channel.enableLights(false);
			channel.enableVibration(false);
			//channel.setSound(Uri.EMPTY, new AudioAttributes.Builder().build());
			ntf_manager.createNotificationChannel(channel);

			// notifications
			channel = new NotificationChannel(CHANNEL_NTF_ID, getString(R.string.server_messages), NotificationManager.IMPORTANCE_DEFAULT);
			channel.enableLights(true);
			channel.enableVibration(true);
			//channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), new AudioAttributes.Builder()
			//	.setUsage(AudioAttributes.USAGE_NOTIFICATION)
			//	.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
			//	.build());
			ntf_manager.createNotificationChannel(channel);
		}

		ntf_builder = new Notification.Builder(this)
			.setSmallIcon(R.drawable.icon)
			//.setLargeIcon(Icon.createWithResource(this, R.drawable.icon))
			.setOnlyAlertOnce(true)
			.setContentTitle(getString(R.string.status_title))
			.setContentText(getString(R.string.idle));
		if (Build.VERSION.SDK_INT >= 26) {
			ntf_builder.setChannelId(CHANNEL_FRG_ID);
		}

		startForeground(1, ntf_builder.build());

	}

	private void postNotification(String channel, int id, String title, String text, boolean alarm) {
		ntf_builder.setContentTitle(title);
		ntf_builder.setContentText(text);
		if (alarm) ntf_builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
		else ntf_builder.setDefaults(0);
		if (Build.VERSION.SDK_INT >= 26) {
			ntf_builder.setChannelId(channel);
		}
		ntf_manager.notify(id, ntf_builder.build());
	}


	public static void notify(int id, String s) {
		if (m_instance == null) return;
		m_instance.postNotification(CHANNEL_NTF_ID, id + 1, m_instance.getString(R.string.title), s, true);
	}


	public static void status(String s) {
		if (m_instance == null) return;
		m_instance.postNotification(CHANNEL_FRG_ID, 1, m_instance.getString(R.string.status_title), s, false);
	}

}
