import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/detail/presentation/article/article_list.dart';
import 'package:hl_lieferservice/feature/delivery/detail/presentation/delivery_discount.dart';
import 'package:hl_lieferservice/feature/delivery/overview/bloc/tour_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/overview/bloc/tour_state.dart';
import 'package:hl_lieferservice/model/delivery.dart';

class DeliveryStepArticleManagement extends StatefulWidget {
  final Delivery delivery;

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

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

class _DeliveryStepInfo extends State<DeliveryStepArticleManagement> {
  Widget _articleOverview() {
    TourLoaded tour = context.read<TourBloc>().state as TourLoaded;

    return ArticleList(
      articles:
          widget.delivery.articles
              .where(
                (article) =>
                    article.articleNumber != tour.tour.discountArticleNumber,
              )
              .toList(),
      deliveryId: widget.delivery.id,
    );
  }

  Widget _discountView() {
    return DeliveryDiscount(
      disabled: false,
      discount: widget.delivery.discount,
      deliveryId: widget.delivery.id,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(10),
      child: Container(
        width: double.infinity,
        alignment: Alignment.centerLeft,
        child: ListView(
          children: [
            Padding(
              padding: const EdgeInsets.only(bottom: 10),
              child: Text(
                "Artikel",
                style: Theme.of(context).textTheme.headlineMedium,
              ),
            ),
            _articleOverview(),

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

            _discountView(),
          ],
        ),
      ),
    );
  }
}
