import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import '../../domain/entities/tour.dart';
import '../../core/constants/app_constants.dart';

class TourListItem extends StatelessWidget {
  final Tour tour;
  final VoidCallback onTap;
  final bool isCompleted;

  const TourListItem({
    super.key,
    required this.tour,
    required this.onTap,
    this.isCompleted = false,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final iconData = _getIconForTourType(tour.type);
    final color = _getColorForTourType(tour.type);

    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
      child: Card(
        elevation: isCompleted ? 0 : 2,
        shadowColor: Colors.black.withValues(alpha: 26),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(16),
          side: isCompleted
              ? BorderSide(color: Colors.grey.shade300)
              : BorderSide.none,
        ),
        child: InkWell(
          onTap: isCompleted ? null : onTap,
          borderRadius: BorderRadius.circular(16),
          child: Padding(
            padding: const EdgeInsets.all(16),
            child: Row(
              children: [
                // Icon Container
                Container(
                  width: 56,
                  height: 56,
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      colors: [
                        color,
                        color.withValues(alpha: 204),
                      ],
                      begin: Alignment.topLeft,
                      end: Alignment.bottomRight,
                    ),
                    borderRadius: BorderRadius.circular(14),
                    boxShadow: [
                      BoxShadow(
                        color: color.withValues(alpha: 77),
                        blurRadius: 8,
                        offset: const Offset(0, 4),
                      ),
                    ],
                  ),
                  child: Icon(
                    iconData,
                    color: Colors.white,
                    size: 28,
                  ),
                ),
                const SizedBox(width: 16),
                
                // Content
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        tour.locationName ?? 'Station ${tour.locationId}',
                        style: theme.textTheme.titleMedium?.copyWith(
                          fontWeight: FontWeight.w600,
                          color: isCompleted ? Colors.grey : null,
                          decoration: isCompleted ? TextDecoration.lineThrough : null,
                        ),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                      const SizedBox(height: 4),
                      Text(
                        _getTypeLabel(tour.type),
                        style: theme.textTheme.bodySmall?.copyWith(
                          color: color,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      if (tour.remark != null && tour.remark!.isNotEmpty) ...[
                        const SizedBox(height: 4),
                        Text(
                          tour.remark!,
                          style: theme.textTheme.bodySmall?.copyWith(
                            color: Colors.grey.shade600,
                          ),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ],
                    ],
                  ),
                ),
                
                // Status Indicator
                if (isCompleted)
                  Container(
                    padding: const EdgeInsets.all(8),
                    decoration: BoxDecoration(
                      color: Colors.green.withValues(alpha: 26),
                      shape: BoxShape.circle,
                    ),
                    child: const Icon(
                      Icons.check,
                      color: Colors.green,
                      size: 20,
                    ),
                  )
                else
                  Container(
                    padding: const EdgeInsets.all(8),
                    decoration: BoxDecoration(
                      color: Colors.grey.shade100,
                      shape: BoxShape.circle,
                    ),
                    child: Icon(
                      Icons.chevron_right,
                      color: Colors.grey.shade600,
                      size: 24,
                    ),
                  ),
              ],
            ),
          ),
        ),
      ),
    ).animate().fadeIn(duration: 300.ms).slideX(begin: 0.1, end: 0);
  }

  IconData _getIconForTourType(String type) {
    switch (type) {
      case TourTypes.stockStart:
        return Icons.warehouse;
      case TourTypes.stockEnd:
        return Icons.archive;
      case TourTypes.start:
        return Icons.business;
      case TourTypes.end:
        return Icons.flag;
      case TourTypes.station:
        return Icons.directions_bus;
      case TourTypes.hls:
        return Icons.train;
      case TourTypes.fsa:
        return Icons.confirmation_number;
      case TourTypes.vs:
        return Icons.store;
      case TourTypes.gi:
        return Icons.account_balance;
      case TourTypes.veh:
      case TourTypes.vehStart:
      case TourTypes.vehEnd:
        return Icons.local_shipping;
      default:
        return Icons.location_on;
    }
  }

  Color _getColorForTourType(String type) {
    switch (type) {
      case TourTypes.stockStart:
      case TourTypes.stockEnd:
        return const Color(0xFF2196F3);
      case TourTypes.start:
      case TourTypes.end:
        return const Color(0xFF4CAF50);
      case TourTypes.station:
        return const Color(0xFFFF9800);
      case TourTypes.hls:
        return const Color(0xFF9C27B0);
      case TourTypes.fsa:
        return const Color(0xFFE91E63);
      case TourTypes.vs:
        return const Color(0xFF00BCD4);
      case TourTypes.gi:
        return const Color(0xFF3F51B5);
      case TourTypes.veh:
      case TourTypes.vehStart:
      case TourTypes.vehEnd:
        return const Color(0xFF795548);
      default:
        return const Color(0xFF607D8B);
    }
  }

  String _getTypeLabel(String type) {
    switch (type) {
      case TourTypes.stockStart:
        return 'Lager - Beladung';
      case TourTypes.stockEnd:
        return 'Lager - Rückgabe';
      case TourTypes.start:
        return 'Dienststelle';
      case TourTypes.end:
        return 'Tour Ende';
      case TourTypes.station:
        return 'Haltestelle';
      case TourTypes.hls:
        return 'Hochbahnstation';
      case TourTypes.fsa:
        return 'Fahrscheinautomat';
      case TourTypes.vs:
        return 'Versorgungsstelle';
      case TourTypes.gi:
        return 'Geldinstitut';
      case TourTypes.veh:
        return 'Fahrzeug';
      case TourTypes.vehStart:
        return 'Fahrzeug - Beladung';
      case TourTypes.vehEnd:
        return 'Fahrzeug - Entladung';
      default:
        return type;
    }
  }
}
