using GMCabsDriverAssistant.Services;
using GMCabsDriverAssistant.Utils;

namespace GMCabsDriverAssistantSolution.Views;

public partial class PermissionCheckPage : ContentPage
{
    bool isFirstTime = true;

    public PermissionCheckPage()
    {
        InitializeComponent();
    }

    [Obsolete]
    protected async override void OnAppearing()
    {
        base.OnAppearing();
        isFirstTime = Preferences.Get("isFirstTime", true);
        var status = await Permissions.CheckStatusAsync<Permissions.LocationAlways>();
        if (status == PermissionStatus.Granted)
        {
            Preferences.Set(SecureStorageData.IsLocationPermitted, true);
        }
        else
        {
            Preferences.Set(SecureStorageData.IsLocationPermitted, false);
        }
        if (Device.RuntimePlatform == Device.Android)
        {
            if (DependencyService.Get<IBatteryInfo>().CheckIsIgnoringBatteryOptimizations())
            {
                Preferences.Set(SecureStorageData.IsBatteryOptimizationDisabled, true);
            }
            else
            {
                Preferences.Set(SecureStorageData.IsBatteryOptimizationDisabled, false);
            }
        }
        if ((Preferences.Get(SecureStorageData.IsLocationPermitted, false)) || !string.IsNullOrWhiteSpace(Preferences.Get(SecureStorageData.Token, "")))
        {
            if (Device.RuntimePlatform == Device.Android && Preferences.Get(SecureStorageData.IsBatteryOptimizationDisabled, false))
            {
                await ShowSplashScreen();
            }
            else if (Device.RuntimePlatform == Device.iOS)
            {
                await ShowSplashScreen();
            }
        }
    }

    public void OnPermissionCheckClicked(object sender, EventArgs e)
    {
        //Navigation.ShowPopup(new AppPermissiontSetDialogPage(this, Constant.FROM_PERMISSION_PAGE));
        /*if (!isFirstTime)
        {
            Navigation.ShowPopup(new LocationNotSetDialogPage(this));
        }
        else
        {
            Preferences.Set("isFirstTime", false);
            var status = await OnLocationCheck();
            if (status == PermissionStatus.Granted)
            {
                await ShowSplashScreen();
            }
        }*/
    }
    public async Task<PermissionStatus> OnLocationCheck()
    {

        var status = await Permissions.CheckStatusAsync<Permissions.LocationAlways>();
        if (status == PermissionStatus.Granted)
            return status;

        try
        {
            if (DeviceInfo.Platform == DevicePlatform.Android && int.Parse(DeviceInfo.VersionString) >= 12)
            {


                bool answer = await DisplayAlert("Location", "Please allow your location permission from setting Page and Select 'All the time'", "YES", "NO");
                if (answer)
                    AppInfo.ShowSettingsUI();

            }
            else
            {
                if (DeviceInfo.Platform == DevicePlatform.iOS && status == PermissionStatus.Denied)
                {
                    // Prompt the user to turn on in settings
                    // On iOS once a permission has been denied it may not be requested again from the application
                   // Navigation.ShowPopup(new LocationNotSetDialogPage(this));
                    return status;
                }
                status = await Permissions.RequestAsync<Permissions.LocationAlways>();
                if (status == PermissionStatus.Granted)
                {
                    return status;
                }
                if (!Permissions.ShouldShowRationale<Permissions.LocationAlways>())
                {
                    bool answer = await DisplayAlert("Location", "Please allow your location permission from setting Page", "YES", "NO");
                    if (answer)
                        AppInfo.ShowSettingsUI();
                }

            }
        }
        catch (Exception ex)
        {

        }
        return status;
    }
    private async Task ShowSplashScreen()
    {
        await Shell.Current.GoToAsync($"//{nameof(SplashPage)}");
    }
}