import 'package:flutter/material.dart';

import '../objects/bluetooth_object.dart';
import '../widgets/single_section.dart';

class BluetoothDeviceSettings extends StatefulWidget {
  final BluetoothObject bluetoothObject;

  const BluetoothDeviceSettings({super.key, required this.bluetoothObject});

  @override
  State<StatefulWidget> createState() => BluetoothDeviceSettingsState();
}

class BluetoothDeviceSettingsState extends State<BluetoothDeviceSettings> {
  late BluetoothObject _bluetoothObject;
  bool _isRegistering = false;
  String _textInput = ""; // 0123456789 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ

  @override
  void initState() {
    super.initState();
    _bluetoothObject = widget.bluetoothObject;
  }

  ButtonStyle buttonStyle = ElevatedButton.styleFrom(
    foregroundColor: Colors.black,
    backgroundColor: const Color(0xFFFDE100), // Text Color (Foreground color)
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("Device Settings"),
        ),
        body: Center(
          child: ListView(children: [
            Column(children: <Widget>[
              SingleSection(
                title: "Device Information",
                children: [
                  ListTile(
                    title: const Text("Device Name"),
                    subtitle: Text(_bluetoothObject.name),
                  ),
                  ListTile(
                    title: const Text("Device Adress"),
                    subtitle: Text(_bluetoothObject.address),
                  ),
                  ListTile(
                    title: const Text("Device Connection State"),
                    subtitle: Text(
                        _bluetoothObject.isConnected ? "ConnectionState.DISCONNECTED" : "ConnectionState.CONNECTED"),
                  ),
                ],
              ),
              Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 0.0),
                  child: Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
                    // const EdgeInsets defaultContentPadding = EdgeInsets.symmetric(horizontal: 16.0);
                    Expanded(
                        child: ElevatedButton(
                      onPressed: () async {
                        if (!_bluetoothObject.isConnected) {
                          await _bluetoothObject.connectDevice();
                        }
                      },
                      style: buttonStyle,
                      child: const Text("Connect"),
                    )),
                    const SizedBox(width: 16.0),
                    Expanded(
                        child: ElevatedButton(
                      onPressed: () async {
                        if (_bluetoothObject.isConnected) {
                          await _bluetoothObject.disconnectDevice();
                        }
                      },
                      style: buttonStyle,
                      child: const Text("Disconnect"),
                    ))
                  ])),
              const Divider(),
              SingleSection(title: "Cloud Registration", children: [
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 0.0),
                  child: TextFormField(
                    obscureText: false,
                    decoration: InputDecoration(
                        border: const OutlineInputBorder(),
                        hintText: 'Enter the device name',
                        labelText: "Device Name",
                        filled: false,
                        suffixIcon: _isRegistering
                            ? FittedBox(
                                child: Container(
                                  height: 10,
                                  width: 10,
                                  margin: const EdgeInsets.all(8.0),
                                  child: const CircularProgressIndicator(
                                    strokeWidth: 2.0,
                                  ),
                                ),
                              ) // : const I
                            : IconButton(
                                icon: const Icon(Icons.login),
                                onPressed: () async {
                                  FocusScopeNode currentFocus = FocusScope.of(context);
                                  currentFocus.unfocus();
                                  setState(() {
                                    _isRegistering = true;
                                  });
                                  await _bluetoothObject.sendData(context, _textInput);
                                  setState(() {
                                    _isRegistering = false;
                                  });
                                },
                              )),
                    keyboardType: TextInputType.text,
                    onChanged: (String newValue) {
                      _textInput = newValue;
                    },
                  ),
                ),
              ]),
            ]),
          ]),
        ));
  }
}
