import 'package:flutter/material.dart';
import 'package:sense_the_rhythm/utils/esense_input.dart';

class ESenseConnectDialog extends StatefulWidget {
  final void Function(String) connect;
  final VoidCallback disconnect;
  final ValueNotifier<String> deviceStatus;

  const ESenseConnectDialog(
      {super.key,
      required this.deviceStatus,
      required this.connect,
      required this.disconnect});

  @override
  State<ESenseConnectDialog> createState() => _ESenseConnectDialogState();
}

class _ESenseConnectDialogState extends State<ESenseConnectDialog> {
  String eSenseDeviceName = '';

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder(
        valueListenable: widget.deviceStatus,
        builder: (BuildContext context, String deviceStatus, Widget? child) {
          return AlertDialog(
            title: const Text('Connect to ESense'),
            content: Column(mainAxisSize: MainAxisSize.min, children: [
              TextField(
                onChanged: (input) {
                  setState(() {
                    eSenseDeviceName = input;
                  });
                },
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  hintText: 'eSense-xxxx',
                  labelText: 'Device name',
                ),
              ),
              // Text(eSenseDeviceName),
              Text(deviceStatus)
            ]),
            actions: <Widget>[
              TextButton(
                onPressed: () => Navigator.pop(context, 'Cancel'),
                child: const Text('Close'),
              ),
              ESenseInput.instance.connected
                  ? TextButton(
                      onPressed: () => widget.disconnect(),
                      child: const Text('Disconnect'),
                    )
                  : TextButton(
                      onPressed: () => widget.connect(eSenseDeviceName),
                      child: const Text('Connect'),
                    ),
            ],
          );
        });
  }
}
