import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:cashumit/services/bluetooth_service.dart';
import 'package:provider/provider.dart';
import 'package:cashumit/providers/receipt_provider.dart';

class ReceiptSpeedDial extends StatelessWidget {
  final BluetoothService bluetoothService;
  final Future<bool> Function() onCheckConnection;
  final Future<void> Function() onPrint;
  final VoidCallback onSettings;
  final Future<void> Function() onReloadAccounts;
  final bool hasItems;
  final bool allGroupsHaveSource;
  final bool hasDestination;
  final Future<void> Function() onSendToFirefly;
  final VoidCallback onPrintingStart;
  final VoidCallback onPrintingEnd;
  final VoidCallback onOpenLocalReceipts;

  const ReceiptSpeedDial({
    super.key,
    required this.bluetoothService,
    required this.onCheckConnection,
    required this.onPrint,
    required this.onSettings,
    required this.onReloadAccounts,
    required this.hasItems,
    required this.allGroupsHaveSource,
    required this.hasDestination,
    required this.onSendToFirefly,
    required this.onPrintingStart,
    required this.onPrintingEnd,
    required this.onOpenLocalReceipts,
  });

  bool get _canSubmit =>
      hasItems && allGroupsHaveSource && hasDestination;

  @override
  Widget build(BuildContext context) {
    return SpeedDial(
      icon: Icons.menu,
      activeIcon: Icons.close,
      spacing: 3,
      spaceBetweenChildren: 4,
      children: [
        SpeedDialChild(
          child: const Icon(Icons.list_alt),
          label: 'Lihat Nota Tersimpan',
          onTap: onOpenLocalReceipts,
          backgroundColor: Colors.blue,
        ),
        SpeedDialChild(
          child: const Icon(Icons.send),
          label: 'Kirim ke Firefly',
          onTap: _canSubmit
              ? onSendToFirefly
              : () {
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(
                      content: Text(!hasDestination
                          ? 'Pilih akun tujuan terlebih dahulu'
                          : !allGroupsHaveSource
                              ? 'Setiap grup harus punya akun sumber'
                              : 'Tambahkan item terlebih dahulu'),
                      duration: const Duration(seconds: 2),
                    ),
                  );
                },
          backgroundColor: _canSubmit ? Colors.blue : Colors.grey,
        ),
        SpeedDialChild(
          child: const Icon(Icons.save),
          label: 'Simpan untuk Dikirim Nanti',
          onTap: _canSubmit
              ? () async {
                  final receiptProvider = context.read<ReceiptProvider>();
                  try {
                    final receiptId =
                        await receiptProvider.saveTransactionLocally();
                    if (receiptId != null) {
                      if (context.mounted) {
                        ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(
                            content: Text(
                                'Nota berhasil disimpan untuk dikirim nanti'),
                          ),
                        );
                      }
                    }
                  } catch (e) {
                    if (context.mounted) {
                      ScaffoldMessenger.of(context).showSnackBar(
                        SnackBar(
                          content: Text('Gagal menyimpan nota: $e'),
                        ),
                      );
                    }
                  }
                }
              : () {
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(
                      content: Text(!hasDestination
                          ? 'Pilih akun tujuan terlebih dahulu'
                          : !allGroupsHaveSource
                              ? 'Setiap grup harus punya akun sumber'
                              : 'Tambahkan item terlebih dahulu'),
                      duration: const Duration(seconds: 2),
                    ),
                  );
                },
          backgroundColor: _canSubmit ? Colors.orange : Colors.grey,
        ),
        SpeedDialChild(
          child: const Icon(Icons.refresh),
          label: 'Muat Ulang Akun',
          onTap: onReloadAccounts,
          backgroundColor: Colors.orange,
        ),
        SpeedDialChild(
          child: const Icon(Icons.settings),
          label: 'Pengaturan',
          onTap: onSettings,
          backgroundColor: Colors.green,
        ),
        SpeedDialChild(
          child: bluetoothService.isPrinting
              ? const CircularProgressIndicator(
                  color: Colors.white, strokeWidth: 2)
              : const Icon(Icons.receipt),
          label: 'Cetak Struk',
          onTap: bluetoothService.isPrinting
              ? null
              : () async {
                  // Panggil callback untuk memulai printing status
                  onPrintingStart();

                  try {
                    // Periksa koneksi secara real-time
                    final isConnected = await onCheckConnection();
                    if (isConnected) {
                      await onPrint();
                    } else {
                      // Coba sambungkan kembali jika ada device yang tersimpan
                      if (bluetoothService.connectedDevice != null) {
                        ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(
                              content:
                                  Text('Mencoba menyambungkan ke printer...')),
                        );
                        try {
                          bool connectResult =
                              await bluetoothService.connectToDevice(
                                  bluetoothService.connectedDevice!);
                          if (!connectResult) {
                            throw Exception('Gagal menyambungkan ke printer');
                          }
                          // Tunggu sebentar untuk memastikan koneksi stabil
                          await Future.delayed(
                              const Duration(milliseconds: 500));
                          // Periksa koneksi lagi
                          final isConnectedAfterConnect =
                              await onCheckConnection();
                          if (isConnectedAfterConnect) {
                            await onPrint();
                          } else {
                            ScaffoldMessenger.of(context).showSnackBar(
                              const SnackBar(
                                  content:
                                      Text('Gagal menyambungkan ke printer')),
                            );
                          }
                        } catch (e) {
                          ScaffoldMessenger.of(context).showSnackBar(
                            SnackBar(
                                content:
                                    Text('Gagal menyambungkan ke printer: $e')),
                          );
                        }
                      } else {
                        ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(
                              content:
                                  Text('Hubungkan printer terlebih dahulu')),
                        );
                      }
                    }
                  } catch (e) {
                    // Tangani error secara umum
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(content: Text('Terjadi kesalahan: $e')),
                    );
                  } finally {
                    // Pastikan printing status selalu diakhiri
                    onPrintingEnd();
                  }
                },
          backgroundColor: Colors.purple,
        ),
      ],
    );
  }
}
