import 'dart:typed_data';

import 'package:carousel_slider/carousel_slider.dart';
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 NoteImageOverview extends StatefulWidget {
  final List<ImageNote> images;
  final String deliveryId;

  const NoteImageOverview({
    super.key,
    required this.images,
    required this.deliveryId,
  });

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

class _NoteImageOverviewState extends State<NoteImageOverview> {
  int? _imageDeleting;

  void _onRemoveImage(int index) {
    ImageNote note = widget.images[index];

    context.read<NoteBloc>().add(
      RemoveImageNote(objectId: note.objectId, deliveryId: widget.deliveryId),
    );
  }

  Widget _buildImageCarousel() {
    return CarouselSlider(
      options: CarouselOptions(
        height: 300.0,
        aspectRatio: 2.0,
        enableInfiniteScroll: false,
      ),
      items:
          widget.images.mapIndexed((index, data) {
            Uint8List bytes = data.data!;

            return Builder(
              builder: (BuildContext context) {
                return Stack(
                  children: [
                    Padding(
                      padding: const EdgeInsets.all(15.0),
                      child: Image.memory(
                        bytes,
                        fit: BoxFit.fill,
                        width: 1920.0,
                        height: 1090.0,
                      ),
                    ),
                    _imageDeleting == index
                        ? Stack(
                          children: [
                            Padding(
                              padding: const EdgeInsets.all(15.0),
                              child: Container(
                                color: Colors.black.withValues(alpha: 0.5),
                              ),
                            ),
                            Center(
                              child: CircularProgressIndicator(
                                backgroundColor:
                                    Theme.of(context).colorScheme.onSecondary,
                              ),
                            ),
                          ],
                        )
                        : Container(),
                    Positioned(
                      right: 0.0,
                      top: 0.0,
                      child: CircleAvatar(
                        radius: 20,
                        child: IconButton.filled(
                          onPressed:
                              !(_imageDeleting == index)
                                  ? () {
                                    _onRemoveImage(index);
                                  }
                                  : null,
                          icon: const Icon(Icons.delete, color: Colors.white),
                        ),
                      ),
                    ),
                  ],
                );
              },
            );
          }).toList(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return widget.images.isEmpty
        ? const Center(child: Text("Noch keine Bilder hochgeladen"))
        : _buildImageCarousel();
  }
}
