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/feature/delivery/detail/model/note.dart';
import 'package:hl_lieferservice/feature/delivery/detail/presentation/note/note_edit_dialog.dart';
import 'package:hl_lieferservice/feature/delivery/bloc/tour_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/bloc/tour_state.dart';

enum NoteItemAction { noteEdit, noteDelete }

class NoteListItem extends StatelessWidget {
  final NoteInformation note;
  final String deliveryId;
  final int index;

  const NoteListItem({
    super.key,
    required this.note,
    required this.deliveryId,
    required this.index,
  });

  void _onDelete(BuildContext context) {
    context.read<NoteBloc>().add(RemoveNote(noteId: note.note.id.toString()));
  }

  Widget? _subtitle(BuildContext context) {
    String discountArticleId =
        (context.read<TourBloc>().state as TourLoaded)
            .tour
            .discountArticleNumber;

    if (note.article != null &&
        note.article?.articleNumber == discountArticleId) {
      return const Text("Begründung der Gutschrift");
    }

    return note.article != null ? Text(note.article!.name) : null;
  }

  void _onEdit(BuildContext context) {
    showDialog(
      context: context,
      builder:
          (_) => BlocProvider.value(
            value: context.read<NoteBloc>(),
            child: NoteEditDialog(note: note.note),
          ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(0),
      child: ListTile(
        title: Text(note.note.content),
        subtitle: _subtitle(context),
        tileColor: Theme.of(context).colorScheme.surfaceContainerLowest,
        leading: CircleAvatar(child: Text("${index + 1}")),
        trailing: PopupMenuButton<NoteItemAction>(
          onSelected: (NoteItemAction action) {
            switch (action) {
              case NoteItemAction.noteDelete:
                _onDelete(context);
                break;
              case NoteItemAction.noteEdit:
                _onEdit(context);
                break;
            }
          },
          itemBuilder: (_) {
            return [
              PopupMenuItem<NoteItemAction>(
                value: NoteItemAction.noteEdit,
                child: Row(
                  children: [
                    Icon(Icons.edit, color: Colors.blueAccent),
                    Padding(
                      padding: const EdgeInsets.only(left: 5),
                      child: const Text("Editieren"),
                    ),
                  ],
                ),
              ),
              PopupMenuItem<NoteItemAction>(
                value: NoteItemAction.noteDelete,
                child: Row(
                  children: [
                    Icon(Icons.delete, color: Colors.redAccent),
                    Padding(
                      padding: const EdgeInsets.only(left: 5),
                      child: const Text("Löschen"),
                    ),
                  ],
                ),
              ),
            ];
          },
        ),
      ),
    );
  }
}
