import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:naiyouwl/app/controller/controllers.dart';

import '../../../component/sys_app_bar.dart';
import '../controllers/node_controller.dart';
import 'package:naiyouwl/app/utils/utils.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: const EdgeInsets.all(30.0),
              child: Column(
                children: [
                  // 错误消息展示
                  Obx(() {
                    if (controller.errorMsg.value.isNotEmpty) {
                      return Text(controller.errorMsg.value,
                          style: const 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: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        ElevatedButton(
                          onPressed: () => controller.filterNodesWithLeastUsersInHK(),
                          child:  Text('node_auto'.tr),
                        ),
                        const SizedBox(height: 10,),
                        ElevatedButton(
                          onPressed: () => controller.showAllNodes(),
                          child:  Text('node_tit'.tr),
                        ),
                        // ElevatedButton(
                        //   onPressed: () => controller.showSelectedFirst(),
                        //   child: Text("选中的在前"),
                        // ),
                        // 你可以在这里添加更多的按钮
                      ],
                    ),
                  ),
                  Expanded(
                    child: Obx(() {
                      return RefreshIndicator(
                        key: _refreshIndicatorKey,
                        onRefresh: controllers.global.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] ??
                                  '';
                              var type = node.type;
                              if(node.vless == 1){
                                type = "vless";
                              }
                              return Container(
                                color: controllers.global.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(type ?? ""),
                                  trailing: Row(
                                    mainAxisSize: MainAxisSize.min,
                                    children: [
                                      if (isNodeLoading)
                                        const CircularProgressIndicator(),
                                      if (!isNodeLoading) ...[
                                        Text(pingResult),
                                        ElevatedButton(
                                          onPressed: () {
                                            controller.pingSingleNode(node);
                                          },
                                          child: const Text('测速'),
                                        ),
                                      ],

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

      ),
    );
  }
}