import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:kasy_kit/core/theme/theme.dart';
import 'package:kasy_kit/features/notifications/providers/models/notification.dart';
import 'package:kasy_kit/features/notifications/repositories/notifications_repository.dart';
import 'package:kasy_kit/features/settings/ui/widgets/settings_tile.dart';
import 'package:kasy_kit/i18n/translations.g.dart';

class PushNotificationSwitcher extends ConsumerStatefulWidget {
  const PushNotificationSwitcher({super.key});

  @override
  ConsumerState<PushNotificationSwitcher> createState() =>
      _PushNotificationSwitcherState();
}

class _PushNotificationSwitcherState
    extends ConsumerState<PushNotificationSwitcher> {
  NotificationPermission? _permission;
  bool _loading = false;

  @override
  void initState() {
    super.initState();
    _loadPermission();
  }

  Future<void> _loadPermission() async {
    final p =
        await ref.read(notificationRepositoryProvider).getPermissionStatus();
    if (mounted) setState(() => _permission = p);
  }

  bool get _isEnabled => _permission is NotificationPermissionGranted;

  String _subtitle(BuildContext context) {
    final tr = context.t.notifications;
    return switch (_permission) {
      NotificationPermissionGranted() => tr.push_subtitle_enabled,
      NotificationPermissionDenied() => tr.push_subtitle_disabled,
      _ => tr.push_subtitle_waiting,
    };
  }

  Future<void> _onChanged(bool value) async {
    if (_loading || _permission == null) return;
    setState(() => _loading = true);
    await _permission!.maybeAsk();
    await _loadPermission();
    if (mounted) setState(() => _loading = false);
  }

  @override
  Widget build(BuildContext context) {
    final tr = context.t.notifications;
    return Container(
      padding: const EdgeInsets.symmetric(
        vertical: KasySpacing.smd,
        horizontal: KasySpacing.md,
      ),
      decoration: BoxDecoration(
        borderRadius: KasyRadius.smBorderRadius,
        color: context.colors.surface,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SettingsSwitchTile(
            icon: _isEnabled
                ? KasyIcons.notificationActive
                : KasyIcons.notification,
            title: tr.push_title,
            value: _isEnabled,
            iconBackgroundColor: _isEnabled
                ? const Color(0xFFFF6B35)
                : const Color(0xFF78909C),
            onChanged: _loading ? (_) {} : _onChanged,
          ),
          Padding(
            padding: const EdgeInsets.only(
              left: 32 + KasySpacing.sm,
              bottom: KasySpacing.xs,
            ),
            child: _permission == null
                ? SizedBox(
                    height: 14,
                    width: 120,
                    child: LinearProgressIndicator(
                      borderRadius: BorderRadius.circular(4),
                      color: context.colors.primary.withValues(alpha: 0.3),
                      backgroundColor:
                          context.colors.onSurface.withValues(alpha: 0.06),
                    ),
                  )
                : Text(
                    _subtitle(context),
                    style: context.textTheme.bodySmall?.copyWith(
                      color: context.colors.muted,
                    ),
                  ),
          ),
        ],
      ),
    );
  }
}
