using GMCabsDriverAssistant.Models;
using GMCabsDriverAssistant.Services;
using GMCabsDriverAssistant.Utils;
using Plugin.FirebasePushNotification;
using Sentry;

namespace GMCabsDriverAssistantSolution.Views;

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SplashPage : ContentPage
{
    public SplashPage()
    {
        InitializeComponent();
    }

    [Obsolete]
    protected async override void OnAppearing()
    {
        base.OnAppearing();

        var checkFcmToken = Preferences.Get("FcmToken", "");
        var token = Preferences.Get(SecureStorageData.Token, "");        
        if (Device.RuntimePlatform != Device.iOS)
        {
            DependencyService.Resolve<INotificationChannelService>().ManageNotificationChannelService();
        }

        if (string.IsNullOrWhiteSpace(checkFcmToken) || string.IsNullOrWhiteSpace(token))
        {
            await Shell.Current.GoToAsync($"//{nameof(LoginPage)}");
        }
        else
        {
            var userCode = Preferences.Get("UserCode", null);
            if (!string.IsNullOrWhiteSpace(userCode))
            {
                SentrySdk.ConfigureScope(scope =>
                {
                    scope.User = new User
                    {
                        Id = userCode.ToString()
                    };
                });
            }
            GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
            var imeiNumber = Preferences.Get("imeiNumber", null);
            var carNumber = Preferences.Get("carNumber", null);
            var fcmToken = CrossFirebasePushNotification.Current.Token;
            ValidateTokenResponseDto validateTokenResponseDto = new ValidateTokenResponseDto();
            validateTokenResponseDto = await gmCabsDriverService.ValidateAuthToken(token, fcmToken);
            var isTablet = Preferences.Get("IsTablet", false);
            if (Device.RuntimePlatform == Device.Android && isTablet && !string.IsNullOrWhiteSpace(imeiNumber) && !string.IsNullOrWhiteSpace(carNumber))
            {
                validateTokenResponseDto.CanAcceptBookings = true;
                validateTokenResponseDto.CanUpdateLicence = false;
                validateTokenResponseDto.CanScanVouchers = false;
                validateTokenResponseDto.CanViewCoupons = false;
                validateTokenResponseDto.CanViewSettings = false;
                validateTokenResponseDto.CanViewHome = false;
                validateTokenResponseDto.CanViewJobHistory = true;
                Preferences.Set(SecureStorageData.CanAcceptBookings, validateTokenResponseDto.CanAcceptBookings);
                Preferences.Set(SecureStorageData.CanUpdateLicence, validateTokenResponseDto.CanUpdateLicence);
                Preferences.Set(SecureStorageData.CanScanVouchers, validateTokenResponseDto.CanScanVouchers);
                Preferences.Set(SecureStorageData.CanViewCoupons, validateTokenResponseDto.CanViewCoupons);
                Preferences.Set(SecureStorageData.CanViewSettings, validateTokenResponseDto.CanViewSettings);
                Preferences.Set(SecureStorageData.CanViewHome, validateTokenResponseDto.CanViewHome);
                Preferences.Set(SecureStorageData.CanViewJobHistory, validateTokenResponseDto.CanViewJobHistory);
            }
            else
            {
                //validateTokenResponseDto.CanAcceptBookings = true;
                //validateTokenResponseDto.CanUpdateLicence = true;
                //validateTokenResponseDto.CanScanVouchers = true;
                //validateTokenResponseDto.CanViewCoupons = true;
                //validateTokenResponseDto.CanViewSettings = true;
                //validateTokenResponseDto.CanViewHome = true;
                //validateTokenResponseDto.CanViewJobHistory = false;
                Preferences.Set(SecureStorageData.CanAcceptBookings, validateTokenResponseDto.CanAcceptBookings);
                Preferences.Set(SecureStorageData.CanUpdateLicence, validateTokenResponseDto.CanUpdateLicence);
                Preferences.Set(SecureStorageData.CanScanVouchers, validateTokenResponseDto.CanScanVouchers);
                Preferences.Set(SecureStorageData.CanViewCoupons, validateTokenResponseDto.CanViewCoupons);
                Preferences.Set(SecureStorageData.CanViewSettings, validateTokenResponseDto.CanViewSettings);
                Preferences.Set(SecureStorageData.CanViewHome, validateTokenResponseDto.CanViewHome);
                Preferences.Set(SecureStorageData.CanViewJobHistory, validateTokenResponseDto.CanViewJobHistory);
            }
            if (validateTokenResponseDto.Result)
            {
                Preferences.Set("FcmToken", fcmToken.ToString());
                if (Device.RuntimePlatform == Device.Android && isTablet && !string.IsNullOrWhiteSpace(imeiNumber) && !string.IsNullOrWhiteSpace(carNumber))
                {
                    //await Shell.Current.GoToAsync($"//{nameof(HomePageTablet)}?{nameof(HomePageTabletViewModel.IsBookingConfirmed)}={false}");
                    IsBusy = false;
                }
                else
                {
                    IsBusy = false;
                    await Shell.Current.GoToAsync($"//{nameof(HomePage)}");
                    //await Shell.Current.GoToAsync(nameof(HomePage));
                }
            }
            else
            {
                var lastLatitude = Convert.ToDouble(Preferences.Get("lastLat", "0"));
                var lastLongitude = Convert.ToDouble(Preferences.Get("lastLng", "0"));
                await gmCabsDriverService.LogoutDriverApp(token, lastLatitude, lastLongitude);
                await Shell.Current.GoToAsync($"//{nameof(LoginPage)}");
            }
        }
    }
}
