import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart' hide Table;

import '../../../../application/checkout/checkout_form/checkout_form_bloc.dart';
import '../../../../common/theme/theme.dart';
import '../../../../common/types/order_type.dart';
import '../../../../domain/table/table.dart';
import '../../spaces/space.dart';
import '../dialog.dart';
import 'order_add_item_dialog.dart';
import 'order_pay_later_dialog.dart';
import 'order_save_confirm_dialog.dart';

class OrderSaveDialog extends StatelessWidget {
  final CheckoutFormState checkoutState;
  final List<Table> tables;
  const OrderSaveDialog({
    super.key,
    required this.checkoutState,
    required this.tables,
  });

  @override
  Widget build(BuildContext context) {
    return CustomModalDialog(
      title: 'Pilih Aksi',
      subtitle: 'Lanjutkan proses pesanan atau simpan untuk nanti.',
      contentPadding: const EdgeInsets.symmetric(
        horizontal: 16.0,
        vertical: 24.0,
      ),
      child: Column(
        children: [
          _item(
            icon: Icons.schedule_outlined,
            title: 'Bayar Nanti',
            subtitle: 'Simpan pesanan dan bayar nanti',
            onTap: () {
              context.maybePop();

              if (checkoutState.orderType == OrderType.dineIn) {
                Future.delayed(const Duration(milliseconds: 100), () {
                  showDialog(
                    context: context,
                    builder: (context) => OrderPayLaterDialog(tables: tables),
                  );
                });
              } else {
                Future.delayed(const Duration(milliseconds: 100), () {
                  showDialog(
                    context: context,
                    builder: (context) =>
                        OrderSaveConfirmDialog(checkoutState: checkoutState),
                  );
                });
              }
            },
          ),
          const SpaceHeight(16.0),
          _item(
            icon: Icons.shopping_cart_checkout_outlined,
            title: 'Tambahkan Pesanan',
            subtitle: 'Tambah item ke daftar pesanan',
            onTap: () {
              showDialog(
                context: context,
                builder: (context) =>
                    OrderAddItemDialog(checkoutState: checkoutState),
              );
            },
          ),
        ],
      ),
    );
  }

  Widget _item({
    required IconData icon,
    required String title,
    required String subtitle,
    required Function() onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
        decoration: BoxDecoration(
          color: AppColor.white,
          borderRadius: BorderRadius.circular(8.0),
          border: Border.all(color: AppColor.border, width: 1.0),
        ),
        child: Row(
          children: [
            Icon(icon, color: AppColor.primary, size: 26.0),
            const SpaceWidth(12.0),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: AppStyle.md.copyWith(fontWeight: FontWeight.w600),
                ),
                Text(
                  subtitle,
                  style: AppStyle.md.copyWith(color: AppColor.textSecondary),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
