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;

  String _messageBuffer = "";

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

  String _textInput = "0123456789 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  String _textOuput = "";

  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.DISCONECTED" : "ConnectionState.CONNECED"),
                  ),
                ],
              ),
              Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 16.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(),
              ElevatedButton(
                onPressed: () async {
                  _bluetoothObject.sendData(_textInput);
                },
                style: buttonStyle,
                child: const Text("Send Data"),
              ),
              ElevatedButton(
                onPressed: () async {
                  setState(() {
                    _textOuput = "";
                  });
                },
                style: buttonStyle,
                child: const Text("Clear Output"),
              ),
              Text(_textOuput),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
                child: TextField(
                  decoration: const InputDecoration(
                    border: OutlineInputBorder(),
                    hintText: 'Enter the device name',
                  ),
                  onChanged: (String newValue) {
                    _textInput = newValue;
                  },
                ),
              ),
            ]),
          ]),
        ));
  }
}
