import 'package:flutter/material.dart';

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

class ArticleOverview extends StatefulWidget {
  const ArticleOverview({super.key, required this.articleGroups});

  final Map<String, ArticleGroup> articleGroups;

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

class _ArticleOverviewState extends State<ArticleOverview> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final sortedArticles =
        widget.articleGroups.values.toList()
          ..sort((a, b) => a.articleName.compareTo(b.articleName));

    return sortedArticles.isEmpty
        ? Center(
          child: Text(
            'Keine Artikel zum Scannen vorhanden',
            style: TextStyle(fontSize: 18),
          ),
        )
        : ListView.separated(
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          itemCount: sortedArticles.length,
          separatorBuilder: (context, index) => Divider(height: 0, color: Theme.of(context).colorScheme.surfaceContainerHighest),
          itemBuilder: (context, index) {
            final group = sortedArticles[index];

            return ListTile(
              leading:
                  group.isComplete
                      ? Icon(Icons.check_circle, color: Colors.green, size: 32)
                      : Container(
                        width: 32,
                        alignment: Alignment.center,
                        child: Text(
                          '${group.scannedCount}/${group.totalCount}',
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.bold,
                            color:
                                group.scannedCount > 0
                                    ? Colors.blue
                                    : Colors.grey,
                          ),
                        ),
                      ),
              title: Text(
                "${group.articleName}  (Artikelnr. ${group.articleNumber})",
                style: TextStyle(fontWeight: FontWeight.w500),
              ),
              subtitle: Padding(
                padding: const EdgeInsets.only(top: 10, bottom: 10),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children:
                      group.deliveryIds
                          .map(
                            (delivery) => Row(
                              children: [
                                Icon(Icons.person),
                                Padding(
                                  padding: const EdgeInsets.only(left: 5, bottom: 10),
                                  child: Text(
                                    "${delivery.customer.name.toString()}\n${delivery.customer.address.toString()}",
                                  ),
                                ),
                              ],
                            ),
                          )
                          .toList(),
                ),
              ),
              tileColor:
                  group.isComplete
                      ? Colors.green.withValues(alpha: 0.1)
                      : Theme.of(context).colorScheme.onSecondary,
            );
          },
        );
  }
}
