import 'package:flutter/material.dart';
import 'package:collection/collection.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 NoteAddDialog extends StatefulWidget {
  final String delivery;
  final List<NoteTemplate> templates;

  const NoteAddDialog({
    super.key,
    required this.delivery,
    required this.templates,
  });

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

class _NoteAddDialogState extends State<NoteAddDialog> {
  final _noteController = TextEditingController();
  final _noteSelectionController = TextEditingController();
  late FocusNode _noteFieldFocusNode;
  bool _isCustomNotesEmpty = true;

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

    _noteFieldFocusNode = FocusNode();

    _noteController.addListener(() {
      setState(() {
        _isCustomNotesEmpty = _noteController.text.isEmpty;
      });
    });
  }

  void _onSave() {
    String content = _noteController.text;

    if (_noteSelectionController.text.isNotEmpty) {
      NoteTemplate template = widget.templates.firstWhere(
        (note) => note.title == _noteSelectionController.text,
      );

      content = template.content;
    }

    context.read<NoteBloc>().add(
      AddNote(note: content, deliveryId: widget.delivery),
    );

    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      child: SizedBox(
        width: MediaQuery.of(context).size.width * 0.75,
        height: MediaQuery.of(context).size.height * 0.45,
        child: Padding(
          padding: const EdgeInsets.all(20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    "Notiz hinzufügen",
                    style: Theme.of(context).textTheme.headlineSmall,
                  ),
                  IconButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    icon: Icon(Icons.close),
                  ),
                ],
              ),
              Padding(
                padding: const EdgeInsets.only(bottom: 10.0, top: 20),
                child: DropdownMenu(
                  controller: _noteSelectionController,
                  onSelected: (int? value) {
                    setState(() {
                      _noteSelectionController.text =
                          widget.templates[value!].title;
                    });
                  },
                  enabled: _isCustomNotesEmpty,
                  width: double.infinity,
                  label: const Text("Notiz auswählen"),
                  dropdownMenuEntries:
                      widget.templates
                          .mapIndexed(
                            (i, note) =>
                                DropdownMenuEntry(value: i, label: note.title),
                          )
                          .toList(),
                ),
              ),
              const Padding(
                padding: EdgeInsets.only(top: 0.0, bottom: 0.0),
                child: Text("oder"),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 10.0, bottom: 20.0),
                child: TextFormField(
                  controller: _noteController,
                  enabled: _noteSelectionController.text.isEmpty,
                  focusNode: _noteFieldFocusNode,
                  decoration: const InputDecoration(
                    labelText: "Eigene Notiz",
                    border: OutlineInputBorder(),
                  ),
                  minLines: 3,
                  maxLines: 5,
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  FilledButton(
                    onPressed:
                        _noteSelectionController.text.isNotEmpty ||
                                _noteController.text.isNotEmpty
                            ? _onSave
                            : null,
                    child: const Text("Hinzufügen"),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(left: 10.0),
                    child: OutlinedButton(
                      onPressed: null,
                      child: const Text("Zurücksetzen"),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
