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

/// Zähler-Grid wie in Lua CreateLoadingStockStartView etc.
class CounterGrid extends StatelessWidget {
  final List<CounterGroup> groups;
  final bool showDifferences;

  const CounterGrid({
    super.key,
    required this.groups,
    this.showDifferences = false,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: groups.map((group) => _buildGroup(context, group)).toList(),
    );
  }

  Widget _buildGroup(BuildContext context, CounterGroup group) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: _getGroupColor(group.title),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        children: [
          // Titel
          Container(
            width: double.infinity,
            padding: const EdgeInsets.symmetric(vertical: 8),
            child: Text(
              group.title,
              textAlign: TextAlign.center,
              style: const TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 16,
              ),
            ),
          ),
          
          // Zähler
          Container(
            padding: const EdgeInsets.all(8),
            child: Row(
              children: group.counters.map((counter) {
                return Expanded(
                  child: _buildCounterCell(context, counter),
                );
              }).toList(),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildCounterCell(BuildContext context, ObjectCounter counter) {
    final color = counter.isOver 
        ? Colors.red.shade100 
        : counter.isComplete 
            ? Colors.green.shade100 
            : Colors.white;
    
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 2),
      padding: const EdgeInsets.symmetric(vertical: 8),
      decoration: BoxDecoration(
        color: color,
        borderRadius: BorderRadius.circular(4),
      ),
      child: Column(
        children: [
          Text(
            counter.label,
            style: const TextStyle(
              fontSize: 11,
              fontWeight: FontWeight.w500,
            ),
            textAlign: TextAlign.center,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
          const SizedBox(height: 4),
          Text(
            '${counter.currentCount}',
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
              color: counter.isOver ? Colors.red : Colors.black87,
            ),
          ),
          if (showDifferences && counter.targetCount > 0) ...[
            const SizedBox(height: 2),
            Text(
              '${counter.difference > 0 ? "+" : ""}${counter.difference}',
              style: TextStyle(
                fontSize: 12,
                color: counter.difference == 0 
                    ? Colors.green 
                    : counter.difference > 0 
                        ? Colors.orange 
                        : Colors.red,
              ),
            ),
          ],
        ],
      ),
    );
  }

  Color _getGroupColor(String title) {
    switch (title.toLowerCase()) {
      case 'bestand fzg':
        return const Color(0xFFA4D4F0); // Hellblau wie in Lua
      case 'beladezähler':
      case 'hadag':
      case 'sst':
      case 'cr':
        return Colors.grey.shade200;
      default:
        return Colors.grey.shade200;
    }
  }
}

/// Vereinfachte Zähler-Anzeige für eine Zeile
class CounterRow extends StatelessWidget {
  final String label;
  final int count;
  final Color? backgroundColor;
  final VoidCallback? onTap;

  const CounterRow({
    super.key,
    required this.label,
    required this.count,
    this.backgroundColor,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
        decoration: BoxDecoration(
          color: backgroundColor ?? Colors.grey.shade100,
          borderRadius: BorderRadius.circular(8),
        ),
        child: Row(
          children: [
            Expanded(
              child: Text(
                label,
                style: const TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w500,
                ),
              ),
            ),
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20),
                border: Border.all(color: Colors.grey.shade300),
              ),
              child: Text(
                '$count',
                style: const TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
