import 'package:flutter/material.dart';

import 'package:get/get.dart';

import '../../../component/sys_app_bar.dart';
import '../controllers/node_controller.dart';

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

  @override
  Widget build(BuildContext context) {
    final _refreshIndicatorKey = GlobalKey<RefreshIndicatorState>();


    return Container(
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage("assets/images/node/nodebg.png"),
          fit: BoxFit.fill,
        ),
      ),
      child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: SysAppBar(
            title: Text("节点列表"),
            toolbarHeight: kToolbarHeight + 40,
            actions: [
              Row(
                children: [
                  IconButton(
                    icon: Icon(Icons.refresh),
                    onPressed: () {
                      controller.pingAllNodes();
                    },
                  ),
                  Text('Ping All'), // 这里是您的标题
                ],
              ),
            ],),
          body: Container(
            width: 376,
            height: 600,
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage("assets/images/node/nodetablebg.png"),
                fit: BoxFit.fill,
              ),
            ),
            child: Padding(
              padding: EdgeInsets.all(30.0),
              child: Column(
                children: [
                  // 错误消息展示
                  Obx(() {
                    if (controller.errorMsg.value.isNotEmpty) {
                      return Text(controller.errorMsg.value,
                          style: TextStyle(color: Colors.red));
                    }
                    return const SizedBox.shrink(); // 返回一个不占空间的widget
                  }),

                  // 加载指示器
                  Obx(() {
                    if (controller.isLoading.value) {
                      return const CircularProgressIndicator();
                    }
                    return const SizedBox.shrink();
                  }),
                  Padding(
                    padding: const EdgeInsets.fromLTRB(0, 0, 0, 10),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        ElevatedButton(
                          onPressed: () => controller.filterNodesWithLeastUsersInHK(),
                          child: Text("人数最少"),
                        ),
                        ElevatedButton(
                          onPressed: () => controller.showAllNodes(),
                          child: Text("显示所有节点"),
                        ),
                        // ElevatedButton(
                        //   onPressed: () => controller.showSelectedFirst(),
                        //   child: Text("选中的在前"),
                        // ),
                        // 你可以在这里添加更多的按钮
                      ],
                    ),
                  ),
                  Expanded(
                    child: Obx(() {
                      return RefreshIndicator(
                        key: _refreshIndicatorKey,
                        onRefresh: controller.globalController.fetchNodes,
                        child: ListView.builder(
                          itemCount: controller.nodesToShow.length,
                          itemBuilder: (BuildContext context, int index) {
                            final node = controller.nodesToShow[index];

                            return Obx(() {
                              // print(controller.selectedNode.value?.id);
                              // print(
                              //     controller.selectedNode.value?.id == node.id);
                              // //  controller.nodeModes[controller.selectedIndex.value]
                              // print("node ---- ${node.id} index ---- $index");
                              bool isNodeLoading = controller.isLoadingMap[node
                                  .id!] ?? false;
                              var pingResult = controller.pingResults[node
                                  .id] ??
                                  '';
                              return Container(
                                color: controller.selectedNode.value?.id ==
                                    node.id ? Colors.black12 : null,
                                child: ListTile(
                                  key: ValueKey(node.id),
                                  title: Text(node.name.toString()),
                                  //tileColor: controller.selectedNode.value?.id == node.id ? Colors.blueAccent : null,
                                  // 如果选中则更改背景颜色
                                  subtitle: Text('${node.type}'),
                                  trailing: Row(
                                    mainAxisSize: MainAxisSize.min,
                                    children: [
                                      if (isNodeLoading)
                                        const CircularProgressIndicator(),
                                      if (!isNodeLoading) ...[
                                        Text(pingResult),
                                        ElevatedButton(
                                          onPressed: () {
                                            controller.pingSingleNode(node);
                                          },
                                          child: Text('测速'),
                                        ),
                                      ],

                                    ],
                                  ),
                                  onTap: () {
                                    controller.selectNode(node);
                                  },
                                ),
                              );
                            });
                          },
                        ),
                      );
                    }),
                  ),
                ],
              ),
            ),
          )

      ),
    );
  }
}
