import 'package:flutter/material.dart';

import '../../../../widget/centered_leading.dart';

/// Prompts for a link password in a bottom sheet. Resolves to the entered
/// password, or null if the sheet was dismissed without submitting. Used both
/// when creating a password-protected link and when setting/changing the
/// password of an existing one.
Future<String?> promptSharePassword(
  BuildContext context, {
  required bool isChange,
  String? policyHint,
}) {
  return showModalBottomSheet<String>(
    context: context,
    isScrollControlled: true,
    showDragHandle: true,
    useSafeArea: true,
    builder: (ctx) =>
        _SharePasswordSheet(isChange: isChange, policyHint: policyHint),
  );
}

class _SharePasswordSheet extends StatefulWidget {
  final bool isChange;
  final String? policyHint;

  const _SharePasswordSheet({required this.isChange, this.policyHint});

  @override
  State<_SharePasswordSheet> createState() => _SharePasswordSheetState();
}

class _SharePasswordSheetState extends State<_SharePasswordSheet> {
  final TextEditingController _controller = TextEditingController();
  bool _obscured = true;

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

  void _submit() {
    final value = _controller.text.trim();
    if (value.isEmpty) return;
    Navigator.of(context).pop(value);
  }

  @override
  Widget build(BuildContext context) {
    final title = widget.isChange ? 'Passwort ändern' : 'Passwort setzen';
    return SafeArea(
      child: SingleChildScrollView(
        padding: EdgeInsets.only(
          bottom: 16 + MediaQuery.viewInsetsOf(context).bottom,
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            ListTile(
              leading: const CenteredLeading(Icon(Icons.lock_outline)),
              title: Text(title),
            ),
            const Divider(height: 1),
            Padding(
              padding: const EdgeInsets.fromLTRB(16, 16, 16, 8),
              child: TextField(
                controller: _controller,
                obscureText: _obscured,
                autofocus: true,
                onSubmitted: (_) => _submit(),
                decoration: InputDecoration(
                  labelText: 'Passwort',
                  helperText: widget.policyHint,
                  helperMaxLines: 3,
                  border: const OutlineInputBorder(),
                  suffixIcon: IconButton(
                    icon: Icon(
                      _obscured
                          ? Icons.visibility_outlined
                          : Icons.visibility_off_outlined,
                    ),
                    tooltip: _obscured ? 'Anzeigen' : 'Verbergen',
                    onPressed: () => setState(() => _obscured = !_obscured),
                  ),
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16),
              child: FilledButton(
                onPressed: _submit,
                child: Text(widget.isChange ? 'Ändern' : 'Setzen'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
