import 'package:cpnav/pages/aim_point/aimpoint_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'main.dart';
import 'pages/aim_point/aimpointController.dart';
import 'pages/real/realController.dart';

final AimPointerController aimcontroller = Get.find();
final RealController realcontroller = Get.find();

class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
  final double appBarHeight;
  final RxInt _currentIndex;

  final isCardVisible = false.obs;
  final isDataVisible = false.obs;
  CustomAppBar({
    required this.appBarHeight,
    required RxInt currentIndex,
  }) : _currentIndex = currentIndex;

  @override
  Widget build(BuildContext context) {
    const textStyle = TextStyle(fontSize: 16);
    return AppBar(
      toolbarHeight: 40,
      centerTitle: true, // 标题居中
      title: Obx(() {
        if (_currentIndex.value == 0) {
          return const Text("桩点:", style: textStyle);
        } else if (_currentIndex.value == 1) {
          return const Text("设备:", style: textStyle);
        } else {
          return const Text("系统:", style: textStyle);
        }
      }),
      // title: _currentIndex.value == 0
      //     ? Obx(() => Text("桩点:${realController.pileId.value}",
      //         style: textStyle))
      //     : const Text("设备:", style: textStyle),
      // // const Center(
      // //     child: Obx(() => Text("设备:", style: textStyle)),
      // //   ),
      // title: Obx(() {
      //   if (_currentIndex.value == 1) {
      //     return const Text("设备:", style: textStyle);
      //   } else if (_currentIndex.value == 0) {
      //     return Text("桩点:${realController.pileId.value}",
      //         style: textStyle);
      //   } else {
      //     return const Text("系统:", style: textStyle);
      //   }
      // }),

      actions: [
        Obx(
          () => InkWell(
            onTap: () {
              appcontroller.isDarkMode.value = !appcontroller.isDarkMode.value;
            },
            child: Icon(
              appcontroller.isDarkMode.value ? Icons.dark_mode : Icons.sunny,
              size: 35,
            ),
          ),
        ),
        const SizedBox(
          width: 10,
        ),
        Obx(
          () => InkWell(
            onTap: () {
              // Navigator.push(context,
              //           MaterialPageRoute(builder: (context) => const ScenceMap())),
            },
            child: Image(
              image: const AssetImage('images/satellite.png'),
              // width: 40,
              color: appcontroller.isDarkMode.value
                  ? Colors.white70
                  : const Color.fromARGB(200, 29, 28, 28),
              height: 40,
            ),
          ),
        ),
        const SizedBox(
          width: 10,
        ),
        _currentIndex.value == 1
            ? Row(
                children: [
                  Obx(
                    () => InkWell(
                      onTap: () {
                        // 点击图标时显示对点卡片
                        aimcontroller.isCardVisible.value =
                            !aimcontroller.isCardVisible.value; // 确保控制器已定义

                        aimcontroller.lastManualTapTime =
                            DateTime.now().millisecondsSinceEpoch;
                      },
                      child: Icon(
                        Icons.my_location_sharp,
                        size: 35,
                        color: aimcontroller.isCardVisible.value
                            ? Colors.blue
                            // : const Color.fromARGB(200, 29, 28, 28),
                            : (appcontroller.isDarkMode.value
                                ? Colors.white70
                                : const Color.fromARGB(200, 29, 28, 28)),
                      ), // 新增图标
                    ),
                  ),
                  const SizedBox(
                    width: 10,
                  ),
                  Obx(
                    () => InkWell(
                      onTap: () {
                        realcontroller.isDataVisible.value =
                            !realcontroller.isDataVisible.value; // 更新控制器中的值
                      },
                      child: Icon(
                        Icons.date_range_rounded,
                        size: 35,
                        color: isDataVisible.value
                            ? Colors.blue
                            : (appcontroller.isDarkMode.value
                                ? Colors.white70
                                : const Color.fromARGB(200, 29, 28, 28)),
                      ), // 新增图标
                    ),
                  ),
                  const SizedBox(
                    width: 10,
                  ),
                  UnconstrainedBox(
                    child: SizedBox(
                      height: 30,
                      child: Builder(
                        builder: (context) => InkWell(
                          child: Icon(
                            Icons.settings_outlined,
                            size: 35,
                            color: appcontroller.isDarkMode.value
                                ? Colors.white70
                                : const Color.fromARGB(200, 29, 28, 28),
                          ),
                          onTap: () => Scaffold.of(context).openEndDrawer(),
                        ),
                      ),
                    ),
                  )
                ],
              )
            : const Text(""),
        // Obx(() => Icon(
        //       blueToothController.connectedType.value == ConnectType.wifi
        //           ? Icons.wifi
        //           : (blueToothController.connectedType.value ==
        //                   ConnectType.bluetooth
        //               ? Icons.bluetooth
        //               : Icons.close),
        //       color: Colors.green,
        //       size: 30,
        //     ))
      ],
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(appBarHeight);
}