using Android.App;
using Android.Content;
using Android.OS;
using GMCabsDriverAssistant.Services;
using GMCabsDriverAssistantSolution.Platforms.Android;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


[assembly: Dependency(typeof(AndroidAlarmReceiver))]
namespace GMCabsDriverAssistantSolution.Platforms.Android
{
    [BroadcastReceiver(Name = "au.com.gmcabs.driverassistant.app.AndroidAlarmReceiver", Enabled = true, Exported = true, DirectBootAware = true)]
    public class AndroidAlarmReceiver : BroadcastReceiver, IAlarmService
    {
        [Obsolete]
        public override void OnReceive(Context context, Intent intent)
        {
            string foregroundChannelId = "GMCabsForeGroundServiceRunningChannel";
            var message = "Foreground Service Is On";
            var title = "Service Status";
            var resultIntent = new Intent(context, typeof(MainActivity));
            resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
            var pendingIntentFlags = (Build.VERSION.SdkInt >= BuildVersionCodes.S)
           ? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Mutable
           : PendingIntentFlags.UpdateCurrent;

            var pending = PendingIntent.GetActivity(context, 0,
                resultIntent,
                pendingIntentFlags);

            var builder =
            new Notification.Builder(context)
                    .SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
                    //.SetColor(global::Android.App.Application.Context.GetColor(Resource.Color.color_notification_icon))
                    .SetContentTitle(title)
                    .SetContentText(message)
                    .SetPriority((int)NotificationImportance.Max)
                    .SetAutoCancel(true)
                    .SetTimeoutAfter(60000);

            builder.SetContentIntent(pending);
            var notification = builder.Build();
            var notifManager = global::Android.App.Application.Context.GetSystemService(global::Android.Content.Context.NotificationService) as NotificationManager;
            
            if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O)
            {
                var notificationChannel = new NotificationChannel(foregroundChannelId, "GMCabsAlarmManagerNotification", NotificationImportance.Low);
                notificationChannel.Importance = NotificationImportance.Low;
                notificationChannel.EnableLights(true);
                notificationChannel.EnableVibration(true);
                notificationChannel.SetShowBadge(true);
                notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300 });
                if (builder != null)
                {
                    builder.SetChannelId(foregroundChannelId);
                    notifManager.CreateNotificationChannel(notificationChannel);
                }
            }
            notifManager.Notify(DateTime.Now.Millisecond, notification);
        }

        public void StartMyAlarmService()
        {
            var alarmIntent = new Intent(global::Android.App.Application.Context, typeof(AndroidAlarmReceiver));
            var pendingIntentFlags = (Build.VERSION.SdkInt >= BuildVersionCodes.S)
            ? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Mutable
            : PendingIntentFlags.UpdateCurrent;

            var pending = PendingIntent.GetBroadcast(global::Android.App.Application.Context, 0, alarmIntent, pendingIntentFlags);
            var alarmManager = (AlarmManager)global::Android.App.Application.Context.GetSystemService(Context.AlarmService);
            alarmManager.Set(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + 1 * 1000, pending);

        }
    }
}
