import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/feature/authentication/bloc/auth_bloc.dart';
import 'package:hl_lieferservice/model/delivery.dart';
import 'package:hl_lieferservice/feature/delivery/detail/presentation/delivery_detail_page.dart';

import '../../../../widget/operations/bloc/operation_bloc.dart';
import '../../detail/bloc/note_bloc.dart';
import '../../detail/repository/note_repository.dart';
import '../../detail/service/notes_service.dart';

class DeliveryListItem extends StatelessWidget {
  final Delivery delivery;
  final double? distance;

  const DeliveryListItem({
    super.key,
    required this.delivery,
    this.distance,
  });

  void _goToDelivery(BuildContext context) {
    Navigator.of(context).push(
      MaterialPageRoute(
        builder: (context) => BlocProvider(
          create: (context) => NoteBloc(
            deliveryId: delivery.id,
            opBloc: context.read<OperationBloc>(),
            authBloc: context.read<AuthBloc>(),
            repository: NoteRepository(service: NoteService()),
          ),
          child: DeliveryDetail(deliveryId: delivery.id),
        ),
      ),
    );
  }

  (Color, Color, IconData, String) _stateStyle(BuildContext context) {
    switch (delivery.state) {
      case DeliveryState.finished:
        return (
          Colors.green.withValues(alpha: 0.07),
          Colors.green.withValues(alpha: 0.35),
          Icons.check_circle_rounded,
          "Abgeschlossen",
        );
      case DeliveryState.canceled:
        return (
          Colors.red.withValues(alpha: 0.07),
          Colors.red.withValues(alpha: 0.35),
          Icons.cancel_rounded,
          "Storniert",
        );
      case DeliveryState.onhold:
        return (
          Colors.orange.withValues(alpha: 0.07),
          Colors.orange.withValues(alpha: 0.35),
          Icons.pause_circle_rounded,
          "Pausiert",
        );
      case DeliveryState.ongoing:
        final distanceLabel = distance != null && !distance!.isNaN
            ? "${distance!.toStringAsFixed(1)} km"
            : "–";
        return (
          Theme.of(context).colorScheme.surfaceContainerLow,
          Colors.transparent,
          Icons.local_shipping_outlined,
          distanceLabel,
        );
    }
  }

  @override
  Widget build(BuildContext context) {
    final (cardColor, borderColor, icon, statusLabel) = _stateStyle(context);
    final isOngoing = delivery.state == DeliveryState.ongoing;

    final iconColor = switch (delivery.state) {
      DeliveryState.finished => Colors.green,
      DeliveryState.canceled => Colors.red,
      DeliveryState.onhold => Colors.orange,
      DeliveryState.ongoing => Theme.of(context).primaryColor,
    };

    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
      elevation: 0,
      color: cardColor,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
        side: BorderSide(color: borderColor),
      ),
      child: InkWell(
        borderRadius: BorderRadius.circular(12),
        onTap: () => _goToDelivery(context),
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
          child: Row(
            children: [
              Icon(icon, color: iconColor, size: 28),
              const SizedBox(width: 14),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      delivery.customer.name,
                      style: Theme.of(context).textTheme.titleMedium?.copyWith(
                        fontWeight: FontWeight.w600,
                        color: isOngoing ? null : iconColor,
                      ),
                    ),
                    const SizedBox(height: 2),
                    Text(
                      delivery.customer.address.toString(),
                      style: TextStyle(
                        fontSize: 13,
                        color: Theme.of(context).colorScheme.onSurfaceVariant,
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(width: 8),
              Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Text(
                    statusLabel,
                    style: TextStyle(
                      fontSize: 12,
                      fontWeight: FontWeight.w500,
                      color: isOngoing
                          ? Theme.of(context).colorScheme.onSurfaceVariant
                          : iconColor,
                    ),
                  ),
                  const SizedBox(height: 4),
                  Icon(
                    Icons.arrow_forward_ios,
                    size: 14,
                    color: Theme.of(context).colorScheme.onSurfaceVariant,
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
