part of 'dialog.dart';

class DeliveryDialog extends StatefulWidget {
  const DeliveryDialog({super.key});

  @override
  State<DeliveryDialog> createState() => _DeliveryDialogState();
}

class _DeliveryDialogState extends State<DeliveryDialog> {
  @override
  Widget build(BuildContext context) {
    return CustomModalDialog(
      title: 'Pilih Pengiriman',
      subtitle: 'Silahkan pilih pengiriman yang sesuai',
      contentPadding: const EdgeInsets.symmetric(
        horizontal: 16.0,
        vertical: 24.0,
      ),
      child: BlocBuilder<CheckoutFormBloc, CheckoutFormState>(
        builder: (context, state) {
          return Column(
            children: List.generate(deliveries.length, (index) {
              return _buildItem(
                context,
                deliveries[index],
                selectedType: state.delivery,
              );
            }),
          );
        },
      ),
    );
  }

  Widget _buildItem(
    BuildContext context,
    Delivery delivery, {
    Delivery? selectedType,
  }) {
    return GestureDetector(
      onTap: () {
        context.read<CheckoutFormBloc>().add(
          CheckoutFormEvent.updateDelivery(delivery),
        );
        Navigator.pop(context);
      },
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 16.0),
        margin: const EdgeInsets.only(bottom: 8.0),
        decoration: BoxDecoration(
          color: selectedType?.id == delivery.id
              ? AppColor.primary
              : AppColor.white,
          borderRadius: BorderRadius.circular(8.0),
          border: Border.all(
            color: selectedType?.id == delivery.id
                ? AppColor.primary
                : AppColor.textSecondary,
            width: 1.0,
          ),
        ),
        child: Row(
          children: [
            Image.asset(
              delivery.imageUrl,
              width: 40.0,
              height: 40.0,
              fit: BoxFit.contain,
            ),
            const SpaceWidth(12.0),
            Expanded(
              child: Text(
                delivery.name,
                style: TextStyle(
                  fontSize: 16,
                  color: selectedType?.id == delivery.id
                      ? AppColor.white
                      : AppColor.black,
                  fontWeight: FontWeight.bold,
                ),
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            const SpaceWidth(12.0),
            Icon(
              Icons.check_circle,
              color: selectedType?.id == delivery.id
                  ? AppColor.success
                  : Colors.transparent,
            ),
          ],
        ),
      ),
    );
  }
}
