import 'package:flutter/material.dart';
import 'package:spotiflac_android/l10n/l10n.dart';
import 'package:spotiflac_android/utils/adaptive_layout.dart';
import 'package:spotiflac_android/widgets/app_sliver_header.dart';

class PrioritySettingsScaffold extends StatelessWidget {
  final bool hasChanges;
  final String title;
  final String description;
  final String infoText;
  final String? saveLabel;
  final EdgeInsetsGeometry descriptionPadding;
  final List<Widget> slivers;
  final Future<void> Function() onSave;
  final Future<bool> Function(BuildContext context) onConfirmDiscard;

  const PrioritySettingsScaffold({
    super.key,
    required this.hasChanges,
    required this.title,
    required this.description,
    required this.infoText,
    required this.slivers,
    required this.onSave,
    required this.onConfirmDiscard,
    this.saveLabel,
    this.descriptionPadding = const EdgeInsets.fromLTRB(16, 4, 16, 8),
  });

  Future<void> _handleBack(BuildContext context) async {
    if (!hasChanges) {
      Navigator.pop(context);
      return;
    }
    final shouldPop = await onConfirmDiscard(context);
    if (shouldPop && context.mounted) {
      Navigator.pop(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;
    final wideInset = wideListInset(context);

    return PopScope(
      canPop: !hasChanges,
      onPopInvokedWithResult: (didPop, result) async {
        if (didPop) return;
        final shouldPop = await onConfirmDiscard(context);
        if (shouldPop && context.mounted) {
          Navigator.pop(context);
        }
      },
      child: Scaffold(
        body: CustomScrollView(
          slivers: [
            AppSliverHeader.page(
              title: title,
              leading: IconButton(
                tooltip: MaterialLocalizations.of(context).backButtonTooltip,
                icon: const Icon(Icons.arrow_back),
                onPressed: () => _handleBack(context),
              ),
              actions: [
                if (hasChanges)
                  TextButton(
                    onPressed: onSave,
                    child: Text(saveLabel ?? context.l10n.dialogSave),
                  ),
              ],
            ),
            SliverToBoxAdapter(
              child: Padding(
                padding: descriptionPadding.add(
                  EdgeInsets.symmetric(horizontal: wideInset),
                ),
                child: Text(
                  description,
                  style: Theme.of(context).textTheme.bodyMedium?.copyWith(
                    color: colorScheme.onSurfaceVariant,
                  ),
                ),
              ),
            ),
            ...slivers,
            SliverToBoxAdapter(
              child: Padding(
                padding: EdgeInsets.fromLTRB(
                  16 + wideInset,
                  16,
                  16 + wideInset,
                  16,
                ),
                child: Container(
                  padding: const EdgeInsets.all(12),
                  decoration: BoxDecoration(
                    color: colorScheme.tertiaryContainer.withValues(alpha: 0.3),
                    borderRadius: BorderRadius.circular(12),
                  ),
                  child: Row(
                    children: [
                      Icon(
                        Icons.info_outline,
                        size: 20,
                        color: colorScheme.tertiary,
                      ),
                      const SizedBox(width: 12),
                      Expanded(
                        child: Text(
                          infoText,
                          style: Theme.of(context).textTheme.bodySmall
                              ?.copyWith(
                                color: colorScheme.onTertiaryContainer,
                              ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            const SliverToBoxAdapter(child: SizedBox(height: 32)),
          ],
        ),
      ),
    );
  }
}
