import 'package:flutter/material.dart';
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
import 'package:get/get.dart';
import 'package:terepi_seged/models/bluetooth_device_list_entry.dart';
import 'package:terepi_seged/pages/bleutooth/presentation/controllers/bluetooth_controller.dart';

class BluetoothTestView extends GetView<BluetoothTestViewController> {
  const BluetoothTestView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.white,
        child: Scaffold(
            extendBody: true,
            backgroundColor: Colors.black.withOpacity(0.05),
            appBar: AppBar(
              elevation: 0.0,
              title: const Text('BluetoothTest'),
              leading: IconButton(
                icon: const Icon(Icons.arrow_back_ios),
                color: Colors.white,
                onPressed: () {
                  Get.back();
                },
              ),
            ),
            body: Column(children: [
              const Center(
                child: Text("BluetoothTestView"),
              ),
              ListTile(
                title: const Text(
                  'Bluetooth status',
                ),
                subtitle: Obx(() => Text("${controller.bluetoothState}")),
                trailing: ElevatedButton(
                  child: const Text('Settings'),
                  onPressed: () {
                    FlutterBluetoothSerial.instance.openSettings();
                  },
                ),
              ),
              Obx(() => Text(
                    "${controller.bluetoothState}",
                    style: TextStyle(
                        color: controller.bluetoothState.value.isEnabled
                            ? Colors.green
                            : Colors.red),
                  )),
              Obx(() => Text("Last update: ${controller.utcOfPositionFix}",
                  style: const TextStyle(
                      color: Colors.blue, fontWeight: FontWeight.bold))),
              Expanded(
                  child: Obx(
                () => ListView(
                    children: controller.bluetoothDiscoveryResults
                        .map((device) => BluetoothDeviceListEntry(
                              device: device.device,
                              enabled: true,
                              onTap: () {
                                print("Device: ${device.device.address}");
                                controller.setSelectedDevice(device.device);
                              },
                            ))
                        .toList()),
              ))
            ])));
  }
}
