part of 'dialog.dart';

class PrinterBluetoothDialog extends StatefulWidget {
  final Function(String) onSelected;

  const PrinterBluetoothDialog({super.key, required this.onSelected});

  @override
  State<PrinterBluetoothDialog> createState() => _PrinterBluetoothDialogState();
}

class _PrinterBluetoothDialogState extends State<PrinterBluetoothDialog> {
  @override
  void initState() {
    super.initState();
    _initBluetooth();
  }

  Future<void> _initBluetooth() async {
    await loadPermissionBluetooth();
    if (mounted) {
      context.read<BluetoothLoaderBloc>().add(
        const BluetoothLoaderEvent.fetched(),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return CustomModalDialog(
      title: 'Bluetooth',
      contentPadding: const EdgeInsets.all(16),
      minHeight: context.deviceHeight * 0.6,
      minWidth: context.deviceWidth * 0.4,
      child: BlocBuilder<BluetoothLoaderBloc, BluetoothLoaderState>(
        builder: (context, state) {
          if (state.isFetching) {
            return const Center(child: LoaderWithText());
          }

          if (state.bluetoothDevices.isEmpty) {
            return const Center(child: Text('No bluetooth printer found'));
          }

          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: state.bluetoothDevices
                .map(
                  (item) => GestureDetector(
                    onTap: () {
                      context.read<BluetoothConnectBloc>().add(
                        BluetoothConnectEvent.connect(item.macAdress),
                      );
                      widget.onSelected(item.macAdress);
                      context.router.maybePop();
                    },
                    child: Container(
                      width: double.infinity,
                      padding: const EdgeInsets.symmetric(vertical: 12),
                      decoration: const BoxDecoration(
                        border: Border(
                          bottom: BorderSide(color: AppColor.border, width: 1),
                        ),
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Text(
                            item.name,
                            style: AppStyle.lg.copyWith(
                              fontWeight: FontWeight.w600,
                              color: AppColor.primary,
                            ),
                          ),
                          const SpaceHeight(4),
                          Text(
                            item.macAdress,
                            style: AppStyle.sm.copyWith(
                              color: AppColor.textSecondary,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                )
                .toList(),
          );
        },
      ),
    );
  }
}
