import 'package:flutter/material.dart';
import '../../domain/entities/counter.dart';

/// Zeigt zuletzt gescannte Objekte an
/// Entspricht Lua: Die Liste in ShowStockStartScreen etc.
class RecentScansList extends StatelessWidget {
  final List<RecentScan>? scans;

  const RecentScansList({
    super.key,
    this.scans,
  });

  @override
  Widget build(BuildContext context) {
    // Demo-Daten falls keine vorhanden
    final demoScans = scans ?? const [
      // RecentScan(
      //   objectCode: 'MEKA123456',
      //   objectName: 'MEK A',
      //   state: 'to_delivery',
      //   scanTime: '',
      // ),
    ];

    if (demoScans.isEmpty) {
      return Container(
        padding: const EdgeInsets.all(16),
        decoration: BoxDecoration(
          color: Colors.grey.shade100,
          borderRadius: BorderRadius.circular(8),
        ),
        child: const Center(
          child: Text(
            'Noch keine Objekte gescannt',
            style: TextStyle(
              color: Colors.grey,
              fontStyle: FontStyle.italic,
            ),
          ),
        ),
      );
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          'Zuletzt gescannt',
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 8),
        ...demoScans.map((scan) => _buildScanItem(context, scan)),
      ],
    );
  }

  Widget _buildScanItem(BuildContext context, RecentScan scan) {
    return Container(
      margin: const EdgeInsets.only(bottom: 8),
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: Colors.grey.shade200,
        borderRadius: BorderRadius.circular(8),
      ),
      child: Row(
        children: [
          // Objekt-Icon
          Container(
            width: 48,
            height: 48,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8),
            ),
            child: Icon(
              _getIconForObject(scan.objectName),
              color: Colors.blue,
            ),
          ),
          const SizedBox(width: 12),
          
          // Objekt-Info
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  scan.objectName,
                  style: const TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.w600,
                  ),
                ),
                Text(
                  '#${scan.objectCode}',
                  style: TextStyle(
                    fontSize: 13,
                    color: Colors.grey.shade600,
                  ),
                ),
              ],
            ),
          ),
          
          // Status
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
            decoration: BoxDecoration(
              color: _getColorForState(scan.state),
              borderRadius: BorderRadius.circular(12),
            ),
            child: Text(
              _getShortStateName(scan.state),
              style: const TextStyle(
                fontSize: 12,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ],
      ),
    );
  }

  IconData _getIconForObject(String objectName) {
    final name = objectName.toLowerCase();
    if (name.contains('mek')) return Icons.money;
    if (name.contains('bek')) return Icons.money_off;
    if (name.contains('hp')) return Icons.print;
    if (name.contains('fr')) return Icons.receipt;
    if (name.contains('sb')) return Icons.shopping_bag;
    if (name.contains('abs')) return Icons.delete;
    return Icons.inventory_2;
  }

  Color _getColorForState(String state) {
    switch (state) {
      case 'to_delivery':
        return Colors.grey.shade300;
      case 'delivery':
        return Colors.grey.shade400;
      case 'station':
        return const Color(0xFFFFDD00);
      case 'in_fa':
        return const Color(0xFF9CDA7A);
      case 'ret_fail':
        return const Color(0xFFFF9081);
      default:
        return Colors.grey.shade200;
    }
  }

  String _getShortStateName(String state) {
    switch (state) {
      case 'to_delivery':
        return 'Zum Fzg';
      case 'delivery':
        return 'Im Fzg';
      case 'station':
        return 'Station';
      case 'in_fa':
        return 'Im FA';
      case 'ret_fail':
        return 'Fehler';
      default:
        return state;
    }
  }
}
