import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/widgets/ui_kit/dialogs/kw_dialog.dart';
import 'package:krow/features/shifts/domain/blocs/shift_deteils_bloc/shift_details_bloc.dart';

class GeocodingDialogs {
  static bool dialogAlreadyOpen = false;

  static void showGeocodingErrorDialog(
      ShiftDetailsState state, BuildContext context) async{
    if (dialogAlreadyOpen) {
      return;
    }
    dialogAlreadyOpen = true;
    var future;
    switch (state.proximityState) {
      case GeofencingProximityState.tooFar:
        future =  KwDialog.show(
          context: context,
          icon: Assets.images.icons.alertTriangle,
          state: KwDialogState.warning,
          title: "You're too far",
          message: 'Please move closer to the designated location.',
          primaryButtonLabel: 'OK',
          onPrimaryButtonPressed: (dialogContext) {
            dialogContext.router.maybePop();
            dialogAlreadyOpen = false;

          },
        );
        break;
      case GeofencingProximityState.locationDisabled:
        future = KwDialog.show(
          context: context,
          icon: Assets.images.icons.alertTriangle,
          state: KwDialogState.negative,
          title: 'Location Disabled',
          message: 'Please enable location services to continue.',
          primaryButtonLabel: 'Go to Settings',
          onPrimaryButtonPressed: (dialogContext) async {
            dialogContext.router.maybePop();
            dialogAlreadyOpen = false;
            await Geolocator.openLocationSettings();

          },
        );
        break;
      case GeofencingProximityState.goToSettings:
        future = KwDialog.show(
          context: context,
          icon: Assets.images.icons.alertTriangle,
          state: KwDialogState.info,
          title: 'Permission Required',
          message: 'You need to allow location access in settings.',
          primaryButtonLabel: 'Open Settings',
          onPrimaryButtonPressed: (dialogContext) async {
            dialogContext.maybePop();
            dialogAlreadyOpen = false;

            await Geolocator.openLocationSettings();

          },
        );
        break;
      case GeofencingProximityState.onlyInUse:
        future =  KwDialog.show(
          context: context,
          icon: Assets.images.icons.alertTriangle,
          state: KwDialogState.info,
          title: 'Track "All the time" required',
          message:
              'To ensure accurate time tracking, we need access to your location. Time tracking will automatically stop if you move more than 500 meters away from your assigned work location. '
              'Please grant “Allow all the time” access to your location. '
              'Go to Settings → Permissions → Location and select “Allow all the time”.',
          primaryButtonLabel: 'Open Settings',
          onPrimaryButtonPressed: (dialogContext) async {
            dialogContext.maybePop();
            dialogAlreadyOpen = false;
            await Geolocator.openLocationSettings();
          },
        );
        break;
      case GeofencingProximityState.permissionDenied:
        future = KwDialog.show(
            context: context,
            icon: Assets.images.icons.alertTriangle,
            state: KwDialogState.negative,
            title: 'Permission Denied',
            message:
                'You have denied location access. Please allow it manually.',
            primaryButtonLabel: 'OK',
            onPrimaryButtonPressed: (dialogContext) async {
              dialogContext.maybePop();
              dialogAlreadyOpen = false;
              await Geolocator.openAppSettings();
            });
        break;
      default:
        dialogAlreadyOpen = false;
        break;
    }
    await future;
    dialogAlreadyOpen = false;
  }
}
