import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../application/checkout/checkout_form/checkout_form_bloc.dart';
import '../../../../application/order/order_form/order_form_bloc.dart';
import '../../../../common/extension/extension.dart';
import '../../../../common/theme/theme.dart';
import '../../../../common/types/order_type.dart';
import '../../button/button.dart';
import '../../spaces/space.dart';
import '../dialog.dart';

class OrderSaveConfirmDialog extends StatelessWidget {
  final CheckoutFormState checkoutState;
  const OrderSaveConfirmDialog({super.key, required this.checkoutState});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<OrderFormBloc, OrderFormState>(
      builder: (context, state) {
        return CustomModalDialog(
          title: 'Konfirmasi Pesanan',
          subtitle: 'Tindakan ini tidak dapat dibatalkan',
          contentPadding: const EdgeInsets.symmetric(
            horizontal: 16.0,
            vertical: 24.0,
          ),
          minWidth: context.deviceWidth * 0.5,
          minHeight: context.deviceHeight * 0.8,
          bottom: Container(
            padding: const EdgeInsets.all(16),
            child: Row(
              children: [
                Expanded(
                  child: AppElevatedButton.outlined(
                    onPressed: () => context.maybePop(),
                    label: 'Batalkan',
                  ),
                ),
                const SpaceWidth(12),
                Expanded(
                  child: AppElevatedButton.filled(
                    isLoading: state.isCreating,
                    onPressed: state.isCreating
                        ? null
                        : () {
                            context.read<OrderFormBloc>().add(
                              OrderFormEvent.createOrder(
                                items: checkoutState.items,
                                orderType: checkoutState.orderType,
                                table: checkoutState.table,
                              ),
                            );
                          },
                    label: 'Konfirmasi',
                  ),
                ),
              ],
            ),
          ),
          child: Column(
            children: [
              Container(
                width: double.infinity,
                padding: const EdgeInsets.all(16),
                decoration: BoxDecoration(
                  color: AppColor.textSecondary.withOpacity(0.05),
                  borderRadius: BorderRadius.circular(12),
                  border: Border.all(color: AppColor.border),
                ),
                child: Text(
                  message(state.customerName ?? '-'),
                  style: AppStyle.md.copyWith(fontSize: 14, height: 1.4),
                ),
              ),
              if (checkoutState.items.isNotEmpty) ...[
                const SpaceHeight(16),
                Container(
                  width: double.infinity,
                  decoration: BoxDecoration(
                    color: AppColor.primaryWithOpacity(0.05),
                    borderRadius: BorderRadius.circular(12),
                    border: Border.all(color: AppColor.primaryWithOpacity(0.2)),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Padding(
                        padding: const EdgeInsets.all(16),
                        child: Row(
                          children: [
                            Container(
                              padding: const EdgeInsets.all(6),
                              decoration: BoxDecoration(
                                color: AppColor.primary,
                                borderRadius: BorderRadius.circular(6),
                              ),
                              child: const Icon(
                                Icons.list_alt_rounded,
                                color: AppColor.white,
                                size: 16,
                              ),
                            ),
                            const SizedBox(width: 8),
                            Text(
                              'Item yang akan dipesan:',
                              style: AppStyle.md.copyWith(
                                fontWeight: FontWeight.bold,
                                color: AppColor.primary,
                              ),
                            ),
                          ],
                        ),
                      ),
                      Container(
                        constraints: const BoxConstraints(maxHeight: 120),
                        child: Scrollbar(
                          child: SingleChildScrollView(
                            padding: const EdgeInsets.only(
                              left: 16,
                              right: 16,
                              bottom: 16,
                            ),
                            child: Column(
                              children: checkoutState.items.map((item) {
                                return Container(
                                  margin: const EdgeInsets.only(bottom: 6),
                                  padding: const EdgeInsets.all(10),
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                    borderRadius: BorderRadius.circular(6),
                                    boxShadow: [
                                      BoxShadow(
                                        color: Colors.black.withOpacity(0.03),
                                        blurRadius: 2,
                                        offset: const Offset(0, 1),
                                      ),
                                    ],
                                  ),
                                  child: Row(
                                    children: [
                                      Container(
                                        width: 6,
                                        height: 6,
                                        decoration: const BoxDecoration(
                                          color: AppColor.primary,
                                          shape: BoxShape.circle,
                                        ),
                                      ),
                                      const SizedBox(width: 8),
                                      Expanded(
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Text(
                                              item.product.name,
                                              style: AppStyle.md.copyWith(
                                                fontWeight: FontWeight.w600,
                                              ),
                                              maxLines: 1,
                                              overflow: TextOverflow.ellipsis,
                                            ),
                                            Text(
                                              '${item.quantity} qty',
                                              style: AppStyle.sm.copyWith(
                                                color: AppColor.textSecondary,
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                      Container(
                                        padding: const EdgeInsets.symmetric(
                                          horizontal: 6,
                                          vertical: 2,
                                        ),
                                        decoration: BoxDecoration(
                                          color: AppColor.primaryWithOpacity(
                                            0.1,
                                          ),
                                          borderRadius: BorderRadius.circular(
                                            4,
                                          ),
                                        ),
                                        child: Text(
                                          ((item.product.price) * item.quantity)
                                              .currencyFormatRpV2,
                                          style: AppStyle.md.copyWith(
                                            fontWeight: FontWeight.bold,
                                            color: AppColor.primary,
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                );
                              }).toList(),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ],
          ),
        );
      },
    );
  }

  String message(String customerName) {
    switch (checkoutState.orderType) {
      case OrderType.dineIn:
        return 'Konfirmasi untuk menyimpan pesanan Dine-In\n\n'
            'Pesanan untuk ${customerName.isNotEmpty ? customerName : "Pelanggan"} akan disimpan ke dalam sistem. '
            'Pelanggan dapat langsung menikmati pesanan di meja yang telah disediakan. '
            'Pastikan semua item pesanan sudah sesuai sebelum menyimpan.';

      case OrderType.delivery:
        return 'Konfirmasi untuk menyimpan pesanan Delivery\n\n'
            'Pesanan delivery untuk ${customerName.isNotEmpty ? customerName : "Pelanggan"} akan disimpan. '
            '${checkoutState.delivery != null ? "Pengiriman: ${checkoutState.delivery?.name ?? "-"}. " : ""}'
            'Tim delivery akan segera mempersiapkan pesanan.';

      case OrderType.takeAway:
        return 'Konfirmasi untuk menyimpan pesanan Take Away\n\n'
            'Pesanan take away untuk ${customerName.isNotEmpty ? customerName : "Pelanggan"} akan disimpan. '
            'Dapur akan mulai mempersiapkan pesanan dan pelanggan dapat mengambil pesanan sesuai estimasi waktu yang diberikan.';

      case OrderType.freeTable:
        return 'Konfirmasi untuk menyimpan pesanan Free Table\n\n'
            'Pesanan free table untuk ${customerName.isNotEmpty ? customerName : "Pelanggan"} akan disimpan. '
            'Meja akan direservasi dan pesanan akan dipersiapkan sesuai dengan waktu kedatangan pelanggan.';
    }
  }
}
