import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/detail/bloc/note_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/detail/bloc/note_event.dart';
import 'package:hl_lieferservice/model/delivery.dart';

class NoteEditDialog extends StatefulWidget {
  final Note note;

  const NoteEditDialog({super.key, required this.note});

  @override
  State<StatefulWidget> createState() => _NoteEditDialogState();
}

class _NoteEditDialogState extends State<NoteEditDialog> {
  final _formKey = GlobalKey<FormState>();
  late TextEditingController _editController;
  late FocusNode _noteFieldFocusNode;

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

    _noteFieldFocusNode = FocusNode();

    _editController = TextEditingController(text: widget.note.content);
  }

  void _onEdit(BuildContext context) {
    context.read<NoteBloc>().add(
      EditNote(
        content: _editController.text,
        noteId: widget.note.id.toString(),
      ),
    );

    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      child: SizedBox(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height * 0.5,
        child: Padding(
          padding: const EdgeInsets.all(20),
          child: ListView(
            children: [
              Text(
                "Notiz bearbeiten",
                style: Theme.of(context).textTheme.headlineMedium,
              ),

              Padding(
                padding: const EdgeInsets.only(top: 15),
                child: Form(
                  key: _formKey,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      TextFormField(
                        focusNode: _noteFieldFocusNode,
                        onTapOutside: (_) {
                          _noteFieldFocusNode.unfocus();
                        },
                        textInputAction: TextInputAction.done,
                        onFieldSubmitted: (_) {
                          _noteFieldFocusNode.unfocus();
                        },
                        decoration: InputDecoration(label: const Text("Notiz")),
                        controller: _editController,
                        minLines: 10,
                        maxLines: 12,
                      ),

                      Padding(
                        padding: const EdgeInsets.only(top: 20),
                        child: Row(
                          children: [
                            FilledButton(
                              onPressed: () {
                                _onEdit(context);
                              },
                              child: const Text("Bearbeiten"),
                            ),

                            Padding(
                              padding: const EdgeInsets.only(left: 10),
                              child: OutlinedButton(
                                onPressed: () {
                                  Navigator.of(context).pop();
                                },
                                child: const Text("Abbrechen"),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
