import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/bloc/tour_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/bloc/tour_event.dart';

import '../../../../../model/article.dart';

class ResetArticleAmountDialog extends StatefulWidget {
  const ResetArticleAmountDialog({
    super.key,
    required this.article,
    required this.deliveryId,
  });

  final Article article;
  final String deliveryId;

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

class _ResetArticleAmountDialogState extends State<ResetArticleAmountDialog> {
  int _selectedAmount = 1;

  void _reset() {
    String deliveryId = widget.deliveryId;
    String articleId = widget.article.internalId.toString();

    if (widget.article.scannable) {
      context.read<TourBloc>().add(
        ResetScanAmountEvent(
          articleId: widget.article.internalId.toString(),
          deliveryId: widget.deliveryId,
        ),
      );
    } else {
      debugPrint("ID: $articleId");
      debugPrint("AMOUNT :$_selectedAmount");

      context.read<TourBloc>().add(
        SetArticleAmountEvent(
          deliveryId: deliveryId,
          articleId: articleId,
          amount: _selectedAmount,
        ),
      );
    }

    Navigator.pop(context);
  }

  Widget _amountSelection() {
    final list = List.generate(3, (index) => index + 1);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text("Anzahl:", style: Theme.of(context).textTheme.labelLarge),
        Padding(
          padding: const EdgeInsets.all(10),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children:
                list
                    .map(
                      (index) => ChoiceChip(
                        label: Text("$index"),
                        selected: _selectedAmount == index,
                        onSelected: (bool selected) {
                          setState(() {
                            _selectedAmount = index;
                          });
                        },
                      ),
                    )
                    .toList(),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Text("Anzahl Artikel zurücksetzen?"),
      content: SizedBox(
        height: MediaQuery.of(context).size.height * 0.25,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            const Text("Wollen Sie die entfernten Artikel wieder hinzufügen?"),
            !widget.article.scannable ? _amountSelection() : Container(),
            Wrap(
              spacing: 10,
              runSpacing: 8,
              alignment: WrapAlignment.spaceEvenly,
              children: [
                FilledButton(
                  onPressed: _reset,
                  child:
                      widget.article.scannable
                          ? const Text("Zurücksetzen")
                          : const Text("Hinzufügen"),
                ),
                OutlinedButton(
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                  child: const Text("Abbrechen"),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
