using GMCabsDriverAssistantSolution.Views;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using GMCabsDriverAssistant.ViewModels;
using GMCabsDriverAssistant.Services;
using GMCabsDriverAssistant.Utils;
using Sentry;
using GMCabsDriverAssistant.Messages;

namespace GMCabsDriverAssistantSolution;

public partial class AppShell : Shell
{
    #region Fields
    private readonly AppShellViewModel _viewModel;
    private string imeiNumber;
    private string carNumber;
    #endregion

    #region Properties   
    #endregion

    #region Constructor
    public AppShell()
    {
        InitializeComponent();
        BindingContext = _viewModel = new AppShellViewModel(this);
        Task.Run(async () =>
        {
            string couponsPermission = await SecureStorageData.GetSecureStorage(SecureStorageData.CanViewCoupons);
            if (string.IsNullOrEmpty(couponsPermission)) { couponsPermission = "false"; }

            string settingsPermission = await SecureStorageData.GetSecureStorage(SecureStorageData.CanViewSettings);
            if (string.IsNullOrEmpty(settingsPermission)) { settingsPermission = "false"; }

            string homePermission = await SecureStorageData.GetSecureStorage(SecureStorageData.CanViewHome);
            if (string.IsNullOrEmpty(homePermission)) { homePermission = "true"; }

            _viewModel.IsVisibleDrivingLicenceScan = Preferences.Get(SecureStorageData.CanUpdateLicence, false);
            _viewModel.IsVisibleVoucherScan = Preferences.Get(SecureStorageData.CanScanVouchers, false);
            _viewModel.IsVisibleRydo = Preferences.Get(SecureStorageData.CanAcceptBookings, false);
            _viewModel.IsVisibleCoupons = Preferences.Get(SecureStorageData.CanViewCoupons, true);
            _viewModel.IsVisibleSettings = Preferences.Get(SecureStorageData.CanViewSettings, true);
            _viewModel.IsCanViewHome = Preferences.Get(SecureStorageData.CanViewHome, false);
            _viewModel.DriverName = Preferences.Get(SecureStorageData.DriverName, "");
            _viewModel.DriverMobileNumber = Preferences.Get("DriverMobileNumber", "");
        });

        Routing.RegisterRoute(nameof(LoginPage), typeof(LoginPage));
        //Routing.RegisterRoute(nameof(NewPage1), typeof(NewPage1));
        Routing.RegisterRoute(nameof(HomePage), typeof(HomePage));
        Routing.RegisterRoute(nameof(AdminPasswordPage), typeof(AdminPasswordPage));
        Routing.RegisterRoute(nameof(TaxiInstallPage), typeof(TaxiInstallPage));
        Routing.RegisterRoute(nameof(InstallCompletePage), typeof(InstallCompletePage));
        Routing.RegisterRoute(nameof(UserRegistrationGenerateOtpPage), typeof(UserRegistrationGenerateOtpPage));
        Routing.RegisterRoute(nameof(UserSignUpPage), typeof(UserSignUpPage));
        Routing.RegisterRoute(nameof(UserRegistrationVerifyOtpPage), typeof(UserRegistrationVerifyOtpPage));
        Routing.RegisterRoute(nameof(UserRegistrationUpdateDriverPinPage), typeof(UserRegistrationUpdateDriverPinPage));
        Routing.RegisterRoute(nameof(CouponsPage), typeof(CouponsPage));
        Routing.RegisterRoute(nameof(CouponsV2Page), typeof(CouponsV2Page));
        Routing.RegisterRoute(nameof(ScanDrivingLicencePage), typeof(ScanDrivingLicencePage));
        Routing.RegisterRoute(nameof(ScanVoucherPage), typeof(ScanVoucherPage));
        Routing.RegisterRoute(nameof(SettingsPage), typeof(SettingsPage));
        Routing.RegisterRoute(nameof(BookingsPage), typeof(BookingsPage));
        Routing.RegisterRoute(nameof(BookingDetailsPage), typeof(BookingDetailsPage));
        Routing.RegisterRoute(nameof(AcceptBookingPage), typeof(AcceptBookingPage));
        Routing.RegisterRoute(nameof(CancelledBookingPage), typeof(CancelledBookingPage));
        Routing.RegisterRoute(nameof(AcceptedFutureBookingsPage), typeof(AcceptedFutureBookingsPage));
        Routing.RegisterRoute(nameof(AcceptedFutureBookingDetailPage), typeof(AcceptedFutureBookingDetailPage));
        Routing.RegisterRoute(nameof(VoucherScanHistory), typeof(VoucherScanHistory));
        //Routing.RegisterRoute(nameof(BookingDetailsTabletPage), typeof(BookingDetailsTabletPage));
        //Routing.RegisterRoute(nameof(OnTripTabletPage), typeof(OnTripTabletPage));
        //Routing.RegisterRoute(nameof(BookingOnWayTabletPage), typeof(BookingOnWayTabletPage));
        Routing.RegisterRoute(nameof(PassgerCollectedPage), typeof(PassgerCollectedPage));
        //Routing.RegisterRoute(nameof(PassengerDroppedWithNoPaymentMethodIdNoFixedFareTablet), typeof(PassengerDroppedWithNoPaymentMethodIdNoFixedFareTablet));
        //Routing.RegisterRoute(nameof(PassengerDroppedWithNoPaymentMethodIdAndFixedFareTablet), typeof(PassengerDroppedWithNoPaymentMethodIdAndFixedFareTablet));
        //Routing.RegisterRoute(nameof(PassengerDroppedWithPaymentMethodIdAndFixedFareTablet), typeof(PassengerDroppedWithPaymentMethodIdAndFixedFareTablet));
        //Routing.RegisterRoute(nameof(PassengerDroppedWithPaymentMethodIdNoFixedFareTablet), typeof(PassengerDroppedWithPaymentMethodIdNoFixedFareTablet));
        //Routing.RegisterRoute(nameof(BookingCompletedTabletPage), typeof(BookingCompletedTabletPage));
        //Routing.RegisterRoute(nameof(NavigateToHomePageTablet), typeof(NavigateToHomePageTablet));
        //Routing.RegisterRoute(nameof(CancelledBookingTabletPage), typeof(CancelledBookingTabletPage));
        Routing.RegisterRoute(nameof(ImeiNumberInstallPage), typeof(ImeiNumberInstallPage));
        //Routing.RegisterRoute(nameof(JobHistoryTabletPage), typeof(JobHistoryTabletPage));
        //Routing.RegisterRoute(nameof(MyAccountTabletPage), typeof(MyAccountTabletPage));
        //Routing.RegisterRoute(nameof(MyShiftsTabletPage), typeof(MyShiftsTabletPage));
        //Routing.RegisterRoute(nameof(AcceptedFutureBookingsTabletPage), typeof(AcceptedFutureBookingsTabletPage));
        //Routing.RegisterRoute(nameof(AcceptedFutureBookingTabletDetailPage), typeof(AcceptedFutureBookingTabletDetailPage));

        //if (!Preferences.ContainsKey(Constants.VoucherScanUseFrontCamera))
        //{
        //    Preferences.Set(Constants.VoucherScanUseFrontCamera, true);
        //}
        imeiNumber = Preferences.Get("imeiNumber", null);
        carNumber = Preferences.Get("carNumber", null);
        var isTablet = Preferences.Get("IsTablet", false);
        //homePageTabletFlyout.IsVisible = true;
        //homePageFlyout.IsVisible = true;
        if (isTablet && !string.IsNullOrWhiteSpace(imeiNumber) && !string.IsNullOrWhiteSpace(carNumber))
        {
            ICollection<ResourceDictionary> collection = Application.Current.Resources.MergedDictionaries;
            if (collection != null)
            {
                collection.Clear();
                //collection.Add(new TabletLayoutStyle());
            }
        }
        //if (Device.RuntimePlatform == Device.Android && isTablet && !string.IsNullOrWhiteSpace(imeiNumber) && !string.IsNullOrWhiteSpace(carNumber))
        //{
        //    homePageTabletFlyout.IsVisible = true;
        //    homePageFlyout.IsVisible = false;
        //    _viewModel.IsVisibleCoupons = false;
        //    _viewModel.IsVisibleSettings = false;
        //    _viewModel.IsCanViewHome = false;
        //    _viewModel.IsVisibleJobHistory = true;
        //    _viewModel.IsVisibleRydo = true;
        //    _viewModel.IsVisibleMyShifts = true;
        //    _viewModel.IsVisibleMyAccount = true;
        //}
        //else
        //{
        //    _viewModel.IsVisibleCoupons = true;
        //    _viewModel.IsVisibleSettings = true;
        //    homePageTabletFlyout.IsVisible = false;
        //    homePageFlyout.IsVisible = true;
        //    _viewModel.IsVisibleJobHistory = false;
        //    _viewModel.IsVisibleMyShifts = false;
        //    _viewModel.IsVisibleMyAccount = false;
        //}
    }
    #endregion

    #region Methods
    [Obsolete]
    private async void LogoutClicked(object sender, EventArgs e)
    {
        DispatchAppComponentService.SetToInitialProperties();
        var imeiNumber = Preferences.Get("imeiNumber", null);
        var carNumber = Preferences.Get("carNumber", null);
        var isTablet = Preferences.Get("IsTablet", false);
        //if (!isTablet && Device.RuntimePlatform != Device.iOS)
        //{
        //    Preferences.Set("isForeground", "NO");
        //    DependencyService.Resolve<IForegroundService>().StopMyForegroundService();
        //}
        //else if (Device.RuntimePlatform == Device.iOS)
        //{
        //    var message = new StopServiceMessage();
        //    MessagingCenter.Send(message, "ServiceStopped");
        //}
        var token = Preferences.Get(SecureStorageData.Token, "");
        Debug.WriteLine("TOKEN-------------", token);
        var lastLatitude = Convert.ToDouble(Preferences.Get("lastLat", "0"));
        var lastLongitude = Convert.ToDouble(Preferences.Get("lastLng", "0"));
        GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
        await gmCabsDriverService.LogoutDriverApp(token, lastLatitude, lastLongitude);
        SecureStorage.RemoveAll();
        Preferences.Clear();
        Preferences.Set(SecureStorageData.Token, token);
        if (!string.IsNullOrWhiteSpace(imeiNumber))
        {
            Preferences.Set("imeiNumber", imeiNumber);
        }
        if (!string.IsNullOrWhiteSpace(carNumber))
        {
            Preferences.Set("carNumber", carNumber);
        }
        Preferences.Set("IsTablet", isTablet);
        SentrySdk.ConfigureScope(scope =>
        {
            scope.User = null;
        });
        await Current.GoToAsync($"//{nameof(LoginPage)}");
    }
    private async void Coupons_Clicked(object sender, EventArgs e)
    {
         await Current.GoToAsync($"//{nameof(HomePage)}/{nameof(CouponsPage)}");
        await Current.GoToAsync($"//{nameof(HomePage)}/{nameof(CouponsV2Page)}");
    }
    private async void Rydo_Clicked(object sender, EventArgs e)
    {
        //if (!string.IsNullOrEmpty(imeiNumber) && !string.IsNullOrEmpty(carNumber))
        //{
        //    await Current.GoToAsync($"//{nameof(HomePageTablet)}/{nameof(AcceptedFutureBookingsTabletPage)}");
        //}
        //else
        //{
            await Current.GoToAsync($"//{nameof(HomePage)}/{nameof(AcceptedFutureBookingsPage)}");
        //}
    }
    private async void ScanDrivingLicence_Clicked(object sender, EventArgs e)
    {
        await Current.GoToAsync($"//{nameof(HomePage)}/{nameof(ScanDrivingLicencePage)}");
    }
    private async void ScanVoucher_Clicked(object sender, EventArgs e)
    {
        await Current.GoToAsync($"//{nameof(HomePage)}/{nameof(ScanVoucherPage)}");
    }
    private async void Settings_Clicked(object sender, EventArgs e)
    {
        await Current.GoToAsync($"//{nameof(HomePage)}/{nameof(SettingsPage)}");
    }
    //private async void JobHistory_Clicked(object sender, EventArgs e)
    //{
    //    await Current.GoToAsync($"//{nameof(HomePageTablet)}/{nameof(JobHistoryTabletPage)}");
    //}
    //private async void MyAccount_Clicked(object sender, EventArgs e)
    //{
    //    if (!string.IsNullOrEmpty(imeiNumber) && !string.IsNullOrEmpty(carNumber))
    //    {
    //        await Current.GoToAsync($"//{nameof(HomePageTablet)}/{nameof(MyAccountTabletPage)}");
    //    }
    //    else
    //    {
    //        await Current.GoToAsync($"//{nameof(HomePage)}/{nameof(MyAccountTabletPage)}");
    //    }
    //}
    //private async void MyShifts_Clicked(object sender, EventArgs e)
    //{
    //    if (!string.IsNullOrEmpty(imeiNumber) && !string.IsNullOrEmpty(carNumber))
    //    {
    //        await Current.GoToAsync($"//{nameof(HomePageTablet)}/{nameof(MyShiftsTabletPage)}");
    //    }
    //    else
    //    {
    //        await Current.GoToAsync($"//{nameof(HomePage)}/{nameof(MyShiftsTabletPage)}");
    //    }
    //}
    #endregion


}
