import 'dart:typed_data';

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_add_dialog.dart';
import 'package:hl_lieferservice/feature/delivery/detail/presentation/note/note_image_overview.dart';
import 'package:hl_lieferservice/feature/delivery/detail/presentation/note/note_list.dart';
import 'package:hl_lieferservice/model/delivery.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_bloc.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_event.dart';
import 'package:image_picker/image_picker.dart';

class NoteOverview extends StatefulWidget {
  final List<NoteInformation> notes;
  final List<NoteTemplate> templates;
  final List<(ImageNote, Uint8List)> images;
  final String deliveryId;

  const NoteOverview({
    super.key,
    required this.notes,
    required this.deliveryId,
    required this.templates,
    required this.images,
  });

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

class _NoteOverviewState extends State<NoteOverview> {
  final _imagePicker = ImagePicker();

  Widget _notes() {
    return NoteList(notes: widget.notes, deliveryId: widget.deliveryId);
  }

  Widget _images() {
    debugPrint("IMAGES: ${widget.images}");

    return NoteImageOverview(
      images: widget.images,
      deliveryId: widget.deliveryId,
    );
  }

  void _onAddNote(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) {
        return NoteAddDialog(
          delivery: widget.deliveryId,
          templates: widget.templates,
        );
      },
    );
  }

  void _onAddImage(BuildContext context) async {
    XFile? file = await _imagePicker.pickImage(source: ImageSource.camera);
    if (file == null) {
      context.read<OperationBloc>().add(
        FailOperation(message: "Fehler beim Aufnehmen des Bildes"),
      );
      return;
    }

    context.read<NoteBloc>().add(
      AddImageNote(file: file, deliveryId: widget.deliveryId),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Padding(
          padding: const EdgeInsets.all(10),
          child: ListView(
            children: [
              Padding(
                padding: const EdgeInsets.only(bottom: 10),
                child: Text(
                  "Notizen",
                  style: Theme.of(context).textTheme.headlineMedium,
                ),
              ),
              _notes(),

              Padding(
                padding: const EdgeInsets.only(bottom: 10, top: 10),
                child: Text(
                  "Bilder",
                  style: Theme.of(context).textTheme.headlineMedium,
                ),
              ),

              _images(),
            ],
          ),
        ),

        Align(
          alignment: Alignment.bottomRight,
          child: Padding(
            padding: const EdgeInsets.only(right: 25),
            child: PopupMenuButton(
              itemBuilder: (context) {
                return [
                  PopupMenuItem(
                    child: Row(
                      children: [
                        Icon(
                          Icons.note_add_rounded,
                          color: Theme.of(context).primaryColor,
                        ),
                        Padding(
                          padding: const EdgeInsets.only(left: 10),
                          child: const Text("Notiz hinzufügen"),
                        ),
                      ],
                    ),
                    onTap: () => _onAddNote(context),
                  ),
                  PopupMenuItem(
                    child: Row(
                      children: [
                        Icon(
                          Icons.image,
                          color: Theme.of(context).primaryColor,
                        ),
                        Padding(
                          padding: const EdgeInsets.only(left: 10),
                          child: const Text("Bild hochladen"),
                        ),
                      ],
                    ),
                    onTap: () => _onAddImage(context),
                  ),
                ];
              },
              style: ButtonStyle(
                backgroundColor: WidgetStatePropertyAll(
                  Theme.of(context).primaryColor,
                ),
              ),
              child: CircleAvatar(
                radius: 32,
                backgroundColor: Theme.of(context).primaryColor,
                child: Icon(
                  Icons.add,
                  color: Theme.of(context).colorScheme.onSecondary,
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
