import 'package:enaklo_pos/core/components/custom_modal_dialog.dart';
import 'package:enaklo_pos/core/components/spaces.dart';
import 'package:enaklo_pos/core/constants/colors.dart';
import 'package:enaklo_pos/data/datasources/delivery_local_datasource.dart';
import 'package:enaklo_pos/data/models/response/delivery_response_model.dart';
import 'package:enaklo_pos/presentation/home/bloc/checkout/checkout_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.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<CheckoutBloc, CheckoutState>(
        builder: (context, state) {
          return state.maybeWhen(
              orElse: () => const SizedBox.shrink(),
              loaded: (items,
                  discountModel,
                  discount,
                  discountAmount,
                  tax,
                  serviceCharge,
                  totalQuantity,
                  totalPrice,
                  draftName,
                  orderType,
                  deliveryType) {
                return Column(
                  children: List.generate(deliveries.length, (index) {
                    return _buildItem(
                      context,
                      deliveries[index],
                      selectedType: deliveryType,
                    );
                  }),
                );
              });
        },
      ),
    );
  }

  Widget _buildItem(BuildContext context, DeliveryModel delivery,
      {DeliveryModel? selectedType}) {
    return GestureDetector(
      onTap: () {
        context.read<CheckoutBloc>().add(
              CheckoutEvent.updateDeliveryType(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
              ? AppColors.primary
              : AppColors.white,
          borderRadius: BorderRadius.circular(8.0),
          border: Border.all(
            color: selectedType?.id == delivery.id
                ? AppColors.primary
                : AppColors.grey,
            width: 1.0,
          ),
        ),
        child: Row(
          children: [
            Image.asset(
              delivery.imageUrl,
              width: 40.0,
              height: 40.0,
              fit: BoxFit.contain,
            ),
            SpaceWidth(12.0),
            Expanded(
              child: Text(
                delivery.name,
                style: TextStyle(
                  fontSize: 16,
                  color: selectedType?.id == delivery.id
                      ? AppColors.white
                      : AppColors.black,
                  fontWeight: FontWeight.bold,
                ),
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            SpaceWidth(12.0),
            Icon(
              Icons.check_circle,
              color: selectedType?.id == delivery.id
                  ? AppColors.green
                  : Colors.transparent,
            ),
          ],
        ),
      ),
    );
  }
}
