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: state.scanResults.length,
                  itemBuilder: (BuildContext context, int index) {
                    return ListTile(
                      title: Text(state.scanResults[index]),
                      // subtitle: Text(state.scanResults[index]),
                      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(
                      state.isScanning.value ? '停止' : '刷新',
                      style: TextStyle(
                        fontSize: 20,
                      ),
                    );
                  }),
                ),
                // TextButton(
                //   onPressed: () => {},
                //   child: const Text(
                //     '停止',
                //     style: TextStyle(
                //       fontSize: 20,
                //     ),
                //   ),
                // ),
                TextButton(
                  onPressed: () => {
                    state.scanResults.clear(),
                  },
                  child: const Text(
                    '清空',
                    style: TextStyle(
                      fontSize: 20,
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
