import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../controllers/ble_service.dart';
import 'setting_logic.dart';

class SettingPage extends StatelessWidget {
  const SettingPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.find<SettingLogic>();
    final state = Get
        .find<SettingLogic>()
        .state;

    return Scaffold(
      appBar: AppBar(
        title: const Text('设置'),
        backgroundColor: Colors.blue,
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.max,
          children: [
            Obx(() {
              return Expanded(
                child: ListView.separated(
                  itemCount: BleService.to.scanResults.length,
                  itemBuilder: (BuildContext context, int index) {
                    return ListTile(
                      title: Text(BleService.to.scanResults[index].device.advName),
                      subtitle: Text(BleService.to.scanResults[index].device.platformName),
                      onTap: ()=>{
                        logic.connectBle(context,index),
                      },
                    );
                  },
                  separatorBuilder: (BuildContext context, int index) {
                    return Container(
                      height: 1,
                      color: Colors.black12,
                    );
                  },
                ),
              );
            }),
            SizedBox(
              height: 1,
              child: Container(
                height: 1,
                color: Colors.black54,
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                TextButton(
                  onPressed: () => {
                    logic.refreshBle(),
                  },
                  child: Obx(() {
                    return Text(
                      BleService.to.isScanning.value ? '停止' : '刷新',
                      style: TextStyle(
                        fontSize: 20,
                      ),
                    );
                  }),
                ),
                // TextButton(
                //   onPressed: () => {},
                //   child: const Text(
                //     '停止',
                //     style: TextStyle(
                //       fontSize: 20,
                //     ),
                //   ),
                // ),
                TextButton(
                  onPressed: () => {
                    BleService.to.scanResults.clear(),
                  },
                  child: const Text(
                    '清空',
                    style: TextStyle(
                      fontSize: 20,
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
