import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:kasy_kit/components/components.dart';
import 'package:kasy_kit/core/theme/theme.dart';
import 'package:kasy_kit/features/notifications/api/local_notifier.dart';
import 'package:kasy_kit/i18n/translations.g.dart';

typedef FutureCallback = Future<void> Function();

/// A Page to ask the user to accept or refuse a permission before showing the real permission dialog
/// - ex : ask for notification permission. If the user accept, we show the real permission dialog
/// On iOS you can't ask more than once for it
///
/// TIPS: use this on onboarding to ask for permissions
/// You can also use the same logic to ask for location permission, camera permission, etc.
class RequestNotificationPermission extends ConsumerWidget {
  final FutureCallback? onAccept;
  final FutureCallback? onRefuse;

  const RequestNotificationPermission({
    super.key,
    this.onAccept,
    this.onRefuse,
  });

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final translations = Translations.of(context) //
        .request_notification_permission;
    return Padding(
      padding: const EdgeInsets.fromLTRB(KasySpacing.lg, KasySpacing.xl, KasySpacing.lg, 0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const Spacer(),
          Padding(
            padding: const EdgeInsets.only(bottom: KasySpacing.lg),
            child: Icon(
              KasyIcons.notification,
              size: 40,
              color: context.colors.primary,
            ),
          ),
          Text(
            translations.title,
            textAlign: TextAlign.center,
            style: context.textTheme.headlineMedium?.copyWith(
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: KasySpacing.md),
          Text(
            translations.description,
            textAlign: TextAlign.center,
            style: context.textTheme.bodyLarge,
          ),
          const SizedBox(height: KasySpacing.xl),
          const Spacer(),
          KasyButton(
            label: translations.continue_button,
            expand: true,
            onPressed: () {
              ref.read(notificationsSettingsProvider).askPermission().then(
                (accepted) => switch (accepted) {
                  true => onAccept?.call(),
                  false => onRefuse?.call(),
                },
              );
            },
          ),
          const SizedBox(height: KasySpacing.smd),
          KasyButton(
            label: translations.skip_button,
            variant: KasyButtonVariant.soft,
            expand: true,
            onPressed: () => onRefuse?.call(),
          ),
          const SizedBox(height: KasySpacing.md),
        ],
      ),
    );
  }
}
