import 'package:flutter/material.dart';

import '../../theme/theme_extension.dart';

/// Modal bottom sheet that requires the user to type [confirmWord]
/// (default "DELETE") before the confirm button enables. Used for
/// destructive actions like quarantine delete-file / delete-via-Lidarr
/// and user delete.
///
/// Returns true if the user confirmed, false (or null → false) otherwise.
class TypedConfirmSheet extends StatefulWidget {
  const TypedConfirmSheet({
    super.key,
    required this.title,
    required this.message,
    this.confirmWord = 'DELETE',
    this.confirmLabel = 'Delete',
  });

  final String title;
  final String message;
  final String confirmWord;
  final String confirmLabel;

  static Future<bool> show(
    BuildContext context, {
    required String title,
    required String message,
    String confirmWord = 'DELETE',
    String confirmLabel = 'Delete',
  }) async {
    final ok = await showModalBottomSheet<bool>(
      context: context,
      isScrollControlled: true,
      builder: (_) => TypedConfirmSheet(
        title: title,
        message: message,
        confirmWord: confirmWord,
        confirmLabel: confirmLabel,
      ),
    );
    return ok ?? false;
  }

  @override
  State<TypedConfirmSheet> createState() => _TypedConfirmSheetState();
}

class _TypedConfirmSheetState extends State<TypedConfirmSheet> {
  final _controller = TextEditingController();
  bool _enabled = false;

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final fs = Theme.of(context).extension<FabledSwordTheme>()!;
    return Padding(
      padding: EdgeInsets.only(
        bottom: MediaQuery.of(context).viewInsets.bottom,
      ),
      child: Container(
        color: fs.iron,
        padding: const EdgeInsets.all(20),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              widget.title,
              style: TextStyle(
                color: fs.parchment,
                fontFamily: 'Fraunces',
                fontSize: 20,
              ),
            ),
            const SizedBox(height: 12),
            Text(widget.message, style: TextStyle(color: fs.ash)),
            const SizedBox(height: 16),
            Text(
              'Type ${widget.confirmWord} to confirm:',
              style: TextStyle(color: fs.ash),
            ),
            const SizedBox(height: 8),
            TextField(
              key: const Key('typed_confirm_input'),
              controller: _controller,
              autofocus: true,
              style: TextStyle(color: fs.parchment, fontFamily: 'JetBrainsMono'),
              decoration: const InputDecoration(border: OutlineInputBorder()),
              onChanged: (v) =>
                  setState(() => _enabled = v == widget.confirmWord),
            ),
            const SizedBox(height: 16),
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                TextButton(
                  onPressed: () => Navigator.pop(context, false),
                  child: const Text('Cancel'),
                ),
                const SizedBox(width: 8),
                ElevatedButton(
                  key: const Key('typed_confirm_button'),
                  onPressed:
                      _enabled ? () => Navigator.pop(context, true) : null,
                  style: ElevatedButton.styleFrom(
                    backgroundColor: fs.oxblood,
                    foregroundColor: fs.parchment,
                  ),
                  child: Text(widget.confirmLabel),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
