using GMCabsDriverAssistant.Models;
using GMCabsDriverAssistant.Services;
using GMCabsDriverAssistant.Utils;
using GMCabsDriverAssistantSolution.Styles;
using Plugin.FirebasePushNotification;
using Plugin.SimpleAudioPlayer;
using System.Diagnostics;

namespace GMCabsDriverAssistantSolution;

public partial class App : Application
{
    private static SQLiteDatabaseService database;
    public static SQLiteDatabaseService Database
    {
        get
        {
            if (database == null)
            {
                string databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AvailableBooking.db3");
                database = new SQLiteDatabaseService(databasePath);
            }

            return database;
        }
    }
    public App()
	{
		InitializeComponent();
        dictionary.MergedDictionaries.Add(PhoneLayoutStyle.SharedInstance);
        MainPage = new AppShell();
	}

    [Obsolete]
    protected override async void OnStart()
    {
        try
        {
            ShareConstant.IsInForeground = true;
            Preferences.Set(SecureStorageData.InitLaunched, "");
            CrossFirebasePushNotification.Current.Subscribe("all");
            CrossFirebasePushNotification.Current.OnTokenRefresh += (s, e) =>
            {
                Debug.WriteLine($"Token: {e.Token}");
            };
            Debug.WriteLine($"TOKEN: {CrossFirebasePushNotification.Current.Token}");

            CrossFirebasePushNotification.Current.OnNotificationReceived += (s, e) =>
            {
                try
                {
                    Debug.WriteLine("Received");
                    if (e.Data.ContainsKey("category"))
                    {
                        if (e.Data["category"].ToString().Equals("BookingCancelled"))
                        {
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                CancelledBookingResponseDto cancelledBookingResponseDto = new CancelledBookingResponseDto();
                                cancelledBookingResponseDto.PickUPAddress = e.Data["fromAddress"].ToString();
                                cancelledBookingResponseDto.DropUpAddress = e.Data["toAddress"].ToString();
                                MessagingCenter.Send(this, nameof(App), cancelledBookingResponseDto);
                                if (Device.RuntimePlatform == Device.iOS)
                                {
                                    if (ShareConstant.IsInForeground)
                                    {
                                        var player = CrossSimpleAudioPlayer.Current;
                                        player.Load("system.wav");
                                        player.Play();
                                    }
                                }
                            });
                        }
                        else if (e.Data["category"].ToString().Equals("BookingClearAccepted"))
                        {
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                MessagingCenter.Send(this, "ClearAcceptance", string.Empty);
                            });
                        }
                        else if (e.Data["category"].ToString().Equals("CouponRedeemed"))
                        {
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                CouponDto coupon = new CouponDto();
                                coupon.Id = e.Data["couponid"].ToString();
                                coupon.Status = e.Data["status"].ToString();
                                MessagingCenter.Send(this, nameof(App), coupon);
                            });
                        }
                        else if (e.Data["category"].ToString().Equals("NotificationAvailable"))
                        {
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                NotificationDto notification = new NotificationDto();
                                notification.Id = e.Data["id"].ToString();
                                notification.Subject = e.Data["subject"].ToString();
                                notification.Body = e.Data["notification_body"].ToString();
                                MessagingCenter.Send(this, nameof(App), notification);
                                if (Device.RuntimePlatform == Device.iOS)
                                {
                                    if (ShareConstant.IsInForeground)
                                    {
                                        var player = CrossSimpleAudioPlayer.Current;
                                        player.Load("system.wav");
                                        player.Play();
                                    }
                                }
                            });
                        }
                        else if (e.Data["category"].ToString().Equals("IsTabletInstallation"))
                        {

                            Debug.WriteLine($"{e.Data["category"]}");
                            var istablet = Convert.ToBoolean(e.Data["istabletinstallation"].ToString());
                            Preferences.Set("IsTablet", istablet);
                            MessagingCenter.Send(this, nameof(App), e.Data["category"].ToString());

                        }
                        else if (e.Data["category"].ToString().Equals("DriverAlertNotification"))
                        {
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                Debug.WriteLine($"{e.Data["category"]}");
                                MessagingCenter.Send(this, nameof(App), e.Data["category"].ToString());
                            });
                        }
                        else if (e.Data["category"].ToString().Equals("CouponAvailable"))
                        {
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                Debug.WriteLine($"{e.Data["category"]}");
                                MessagingCenter.Send(this, nameof(App), e.Data["category"].ToString());
                                if (Device.RuntimePlatform == Device.iOS)
                                {
                                    if (ShareConstant.IsInForeground)
                                    {
                                        var player = CrossSimpleAudioPlayer.Current;
                                        player.Load("system.wav");
                                        player.Play();
                                    }
                                }

                            });
                        }
                        else if (e.Data["category"].ToString().Equals("BookingAvailable"))
                        {
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                Debug.WriteLine($"{e.Data["category"]}");
                                MessagingCenter.Send(this, nameof(App), e.Data["category"].ToString());
                                if (Device.RuntimePlatform == Device.iOS)
                                {
                                    if (ShareConstant.IsInForeground)
                                    {
                                        var player = CrossSimpleAudioPlayer.Current;
                                        player.Load("newbooking.wav");
                                        player.Play();
                                    }
                                }

                            });
                        }
                        else
                        {
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                Debug.WriteLine($"{e.Data["category"]}");
                                MessagingCenter.Send(this, nameof(App), e.Data["category"].ToString());
                            });
                        }
                    }
                    else if (e.Data.ContainsKey("coupon_category"))
                    {
                        if (e.Data["coupon_category"].ToString().Equals("CouponRedeemed"))
                        {
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                MessagingCenter.Send(this, nameof(App), e.Data["coupon_category"].ToString());
                            });

                        }
                    }
                }
                catch (Exception ex)
                {

                }
            };
        }
        catch (Exception ex)
        {
            Debug.WriteLine($"Onstart Error Entry: {ex.StackTrace}");
        }
    }

    protected override void OnSleep()
    {
        try
        {
            ShareConstant.IsInForeground = false;
        }
        catch (Exception ex)
        {
            Debug.WriteLine($"Onsleep Error Entry: {ex.StackTrace}");
        }
    }
    protected override void OnResume()
    {
        try
        {
            ShareConstant.IsInForeground = true;
        }
        catch (Exception ex)
        {
            Debug.WriteLine($"OnResume Error Entry: {ex.StackTrace}");
        }
    }

}
