using System;
using CafeTerrace.Services;
using CafeTerrace.Utilities;
using Prism;
using Prism.Ioc;
using CafeTerrace.ViewModels;
using CafeTerrace.Views;
using CafeTerrace.Views.PopUps;
using Prism.Navigation;
using Rg.Plugins.Popup.Pages;
using Rg.Plugins.Popup.Services;
using Xamarin.Forms;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using Xamarin.Forms.Xaml;

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace CafeTerrace
{
    public partial class App
    {
        private static BuyBagService _buyBagService;
        public static BuyBagService BuyBagService
        {
            get
            {
                if (_buyBagService == null)
                {
                    _buyBagService = new BuyBagService();
                }
                return _buyBagService;
            }
        }

        public static RealTimeSignal RealTimeSignal { get; set; } = new RealTimeSignal();
        /* 
         * The Xamarin Forms XAML Previewer in Visual Studio uses System.Activator.CreateInstance.
         * This imposes a limitation in which the App class must have a default constructor. 
         * App(IPlatformInitializer initializer = null) cannot be handled by the Activator.
         */
        public App() : this(null)
        {
        }

        public App(IPlatformInitializer initializer) : base(initializer) { }

        protected override async void OnInitialized()
        {
            try
            {
                Plugin.Settings.CrossSettings.Current.Remove("Order");
                InitializeComponent();
            
                if(Instance.LoginedUser==null)
                    await NavigationService.NavigateAsync("NavigationPage/LoginPage");
                else
                    await NavigationService.NavigateAsync("NavigationPage/MainPage");
            }
            catch (Exception e)
            {
                PopUpUtilites.ErrorPage(e.Message);
            }
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<NavigationPage>();
            containerRegistry.RegisterForNavigation<MainPage, MainPageViewModel>();
            containerRegistry.RegisterForNavigation<LoginPage, LoginPageViewModel>();
            containerRegistry.RegisterForNavigation<HomePage, HomePageViewModel>();
            containerRegistry.RegisterForNavigation<FoodPage, FoodPageViewModel>();
            containerRegistry.RegisterForNavigation<BuyBagPage, BuyBagPageViewModel>();
            containerRegistry.RegisterForNavigation<ProfilePage, ProfilePageViewModel>();
            containerRegistry.RegisterForNavigation<FoodListedPage, FoodListedPageViewModel>();
            containerRegistry.RegisterForNavigation<PaymentPage, PaymentPageViewModel>();
            containerRegistry.RegisterForNavigation<CategoryPage, CategoryPageViewModel>();
        }

        protected override void OnSleep()
        {
            
            base.OnSleep();
            App.BuyBagService.Foods.Clear();
        }
    }
}
