import 'package:flutter/material.dart';

import '../../core/models/sheet.dart';

/// Callback when sheet metadata is saved.
typedef SheetEditCallback = void Function(String newName, String newComposer);

/// Bottom sheet for editing sheet metadata (name and composer).
class EditSheetBottomSheet extends StatefulWidget {
  final Sheet sheet;
  final SheetEditCallback onSave;

  const EditSheetBottomSheet({
    super.key,
    required this.sheet,
    required this.onSave,
  });

  @override
  State<EditSheetBottomSheet> createState() => _EditSheetBottomSheetState();
}

class _EditSheetBottomSheetState extends State<EditSheetBottomSheet> {
  final _formKey = GlobalKey<FormState>();
  late final TextEditingController _nameController;
  late final TextEditingController _composerController;

  @override
  void initState() {
    super.initState();
    _nameController = TextEditingController(text: widget.sheet.name);
    _composerController = TextEditingController(text: widget.sheet.composer);
  }

  @override
  void dispose() {
    _nameController.dispose();
    _composerController.dispose();
    super.dispose();
  }

  void _handleSave() {
    if (!_formKey.currentState!.validate()) return;

    widget.onSave(_nameController.text.trim(), _composerController.text.trim());
    Navigator.pop(context);
  }

  String? _validateNotEmpty(String? value) {
    if (value == null || value.trim().isEmpty) {
      return 'This field cannot be empty';
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.fromLTRB(
        16,
        16,
        16,
        MediaQuery.of(context).viewInsets.bottom + 16,
      ),
      child: Form(
        key: _formKey,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Text(
              'Edit Sheet',
              style: Theme.of(context).textTheme.titleLarge,
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 16),
            TextFormField(
              controller: _nameController,
              validator: _validateNotEmpty,
              decoration: const InputDecoration(
                labelText: 'Sheet Name',
                border: OutlineInputBorder(),
              ),
              textInputAction: TextInputAction.next,
            ),
            const SizedBox(height: 12),
            TextFormField(
              controller: _composerController,
              decoration: const InputDecoration(
                labelText: 'Composer',
                border: OutlineInputBorder(),
              ),
              textInputAction: TextInputAction.done,
              onFieldSubmitted: (_) => _handleSave(),
            ),
            const SizedBox(height: 16),
            ElevatedButton(onPressed: _handleSave, child: const Text('Save')),
          ],
        ),
      ),
    );
  }
}
