import 'package:flutter/material.dart';

import 'package:hl_lieferservice/domain/entity/car.dart';
import 'car_dialog.dart';

class CarCard extends StatelessWidget {
  final Car car;
  final bool isSelected;
  final Function(Car car) onDelete;
  final Function(Car car, String newName) onEdit;

  const CarCard({
    super.key,
    required this.car,
    required this.onEdit,
    required this.onDelete,
    this.isSelected = false,
  });

  @override
  Widget build(BuildContext context) {
    final primary = Theme.of(context).primaryColor;
    return Card(
      color: isSelected ? primary.withValues(alpha: 0.08) : null,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
        side: isSelected
            ? BorderSide(color: primary, width: 2)
            : BorderSide.none,
      ),
      child: Padding(
        padding: const EdgeInsets.all(10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Padding(
                  padding: const EdgeInsets.only(left: 10),
                  child: Icon(
                    Icons.local_shipping,
                    size: 32,
                    color: primary,
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(left: 10),
                  child: Text(
                    car.plate,
                    style: TextStyle(
                      fontWeight: isSelected
                          ? FontWeight.bold
                          : FontWeight.normal,
                    ),
                  ),
                ),
                if (isSelected)
                  Padding(
                    padding: const EdgeInsets.only(left: 8),
                    child: Icon(
                      Icons.check_circle,
                      size: 20,
                      color: primary,
                      semanticLabel: 'Aktuell ausgewählt',
                    ),
                  ),
              ],
            ),

            Row(
              children: [
                IconButton(
                  onPressed: () {
                    showDialog(
                      context: context,
                      builder: (context) {
                        return CarDialog(
                          onAction: (plate) {
                            onEdit(car, plate);
                          },
                          action: CarAction.edit,
                          initialPlateValue: car.plate,
                        );
                      },
                    );
                  },
                  icon: Icon(Icons.edit, color: Theme.of(context).primaryColor),
                ),
                IconButton(
                  onPressed: () {
                    onDelete(car);
                  },
                  icon: const Icon(Icons.delete, color: Colors.redAccent),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
