import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:vbvs_app/common/color/app_uri_status.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/component/tool/CustomCard.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/controller/device/blueteeth_bind_controller.dart';
import 'package:vbvs_app/controller/device/device_type_controller.dart';
import 'package:vbvs_app/controller/main_bottom/global_controller.dart';
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';
import 'package:vbvs_app/controller/user_info_controller.dart';

class DeviceTypeListPage extends StatefulWidget {
  DeviceTypeListPage({super.key});

  @override
  State<DeviceTypeListPage> createState() => _DeviceTypeListPageState();
}

class _DeviceTypeListPageState extends State<DeviceTypeListPage> {
  GlobalController globalController = Get.find();
  UserInfoController userInfoController = Get.find();
  BlueteethBindController blueteethBindController = Get.find();
  ThemeController themeController = Get.find();
  DeviceTypeController deviceTypeController = Get.find();

  @override
  void initState() {
    super.initState();
    deviceTypeController.getDeviceType().then((response) {
      if (response.code != HttpStatusCodes.ok) {
        WidgetsBinding.instance.addPostFrameCallback((_) {
          TopSlideNotification.show(
            context,
            text: response.msg ?? "服务器.失败".tr,
            textColor: themeController.currentColor.sc9,
          );
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, bodySize) => GestureDetector(
        onTap: () => FocusScope.of(context).unfocus(),
        child: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/img/bgNoImg.png'), // 本地图片
              fit: BoxFit.fill, // 填满整个 Container
            ),
          ),
          child: Scaffold(
            backgroundColor: Colors.transparent, // 加上这一行
            appBar: AppBar(
              backgroundColor: themeController.currentColor.sc17,
              // backgroundColor: Colors.transparent,
              automaticallyImplyLeading: false,
              iconTheme: IconThemeData(color: themeController.currentColor.sc3),
              titleSpacing: 0,
              // leading: returnIconButtom,
              title: Container(
                width: double.infinity,
                height: 180.rpx,
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    /// 居中标题
                    Text(
                      '设备列表'.tr,
                      style: TextStyle(
                        fontFamily: 'Readex Pro',
                        color: themeController.currentColor.sc3,
                        letterSpacing: 0,
                        fontSize: 30.rpx,
                      ),
                    ),

                    /// 左边返回按钮
                    Positioned(
                      left: 0,
                      child: returnIconButtom,
                    ),
                  ],
                ),
              ),

              actions: [],
              centerTitle: false,
            ),

            body: SafeArea(
              top: true,
              child: Padding(
                padding: EdgeInsetsDirectional.fromSTEB(30.rpx, 0, 30.rpx, 0),
                child: SingleChildScrollView(
                  child: Column(
                    mainAxisSize: MainAxisSize.max,
                    children: [
                      // 使用 Obx 来监听 deviceTypeList 的变化
                      Obx(() {
                        return Column(
                          children: [
                            SizedBox(height: 26.rpx), // 开始的间隔
                            ...deviceTypeController.deviceTypeList.value
                                .map((device) {
                              return Padding(
                                padding: EdgeInsets.only(
                                    bottom: 26.rpx), // 添加每个设备之间的间隔
                                child: _buildDeviceCard(
                                  context,
                                  title: device['name'], // 这里假设 device 是一个 Map
                                  imageUrl: device['image'],
                                  type: device['type'],
                                ),
                              );
                            }).toList(),
                            SizedBox(height: 26.rpx), // 结束的间隔
                          ],
                        );
                      }),
                    ],
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildDeviceCard(BuildContext context,
      {required String title, required String imageUrl, required double type}) {
    return CustomCard(
      borderRadius: 20.rpx, // 圆角大小
      onTap: () {
        if (type != null) {
          if (type == 1) {
            Get.toNamed("/bodyDevice");
          }
          if (type == 2) {
            TopSlideNotification.show(
              context,
              text: "待开发.提示".tr,
              textColor: themeController.currentColor.sc2,
            );
          }
        }
      },
      colors: [themeController.currentColor.sc17], // 背景色
      child: Container(
        width: double.infinity,
        height: MediaQuery.sizeOf(context).height * 0.135,
        constraints: BoxConstraints(
          minHeight: 220.rpx,
        ),
        padding: EdgeInsetsDirectional.fromSTEB(77.rpx, 0, 21.rpx, 0),
        child: Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: Text(
                title,
                style: TextStyle(
                  fontFamily: 'Inter',
                  color: themeController.currentColor.sc3,
                  fontSize: 30.rpx,
                  letterSpacing: 0.0,
                ),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            ClipRRect(
              borderRadius: BorderRadius.circular(8.rpx),
              // child: Image.asset(
              //   imageUrl,
              //   width: 212.rpx,
              //   height: 168.rpx,
              // ),
              child: Image.network(
                imageUrl,
                // fit: BoxFit.cover,
                width: 212.rpx,
                height: 168.rpx,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
