import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:smart_hotel_app/routes/app_router.gr.dart';
import '../cubit/inspection_topology_state.dart';

class DeviceDetailDialog extends StatelessWidget {
  final TopologyNode node;
  final InspectionTopologyState curentState;
  final VoidCallback onClose;

  const DeviceDetailDialog({
    super.key,
    required this.node,
    required this.curentState,
    required this.onClose,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.transparent,
      child: Stack(
        children: [
          Positioned.fill(
            child: GestureDetector(
              onTap: onClose,
              child: Container(
                color: Colors.black.withOpacity(0.5),
              ),
            ),
          ),
          Positioned(
            left: 0,
            right: 0,
            bottom: 0,
            child: Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(48.r),
                  topRight: Radius.circular(48.r),
                ),
              ),
              child: SafeArea(
                top: false,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      margin: EdgeInsets.only(top: 20.h),
                      width: 120.w,
                      height: 8.h,
                      decoration: BoxDecoration(
                        color: const Color.fromRGBO(203, 213, 225, 1),
                        borderRadius: BorderRadius.circular(4.r),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.all(48.w),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Container(
                                width: 80.w,
                                height: 80.h,
                                decoration: BoxDecoration(
                                  color: const Color.fromRGBO(239, 246, 255, 1),
                                  borderRadius: BorderRadius.circular(20.r),
                                ),
                                child: Icon(
                                  _getDeviceIcon(node.name),
                                  size: 80.sp,
                                  color: const Color.fromRGBO(59, 130, 246, 1),
                                ),
                              ),
                              SizedBox(width: 32.w),
                              Expanded(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      _getDeviceFullName(node.name),
                                      style: TextStyle(
                                        fontSize: 28.sp,
                                        fontWeight: FontWeight.w600,
                                        color: const Color.fromRGBO(15, 23, 42, 1),
                                      ),
                                    ),
                                    SizedBox(height: 12.h),
                                    Text(
                                      curentState.deviceModel,
                                      style: TextStyle(
                                        fontSize: 24.sp,
                                        color: const Color.fromRGBO(148, 163, 184, 1),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Container(
                                padding: EdgeInsets.symmetric(
                                  horizontal: 24.w,
                                  vertical: 12.h,
                                ),
                                decoration: BoxDecoration(
                                  color: const Color.fromRGBO(209, 250, 229, 1),
                                  borderRadius: BorderRadius.circular(40.r),
                                ),
                                child: Text(
                                  node.statusText?? '-',
                                  style: TextStyle(
                                    fontSize: 24.sp,
                                    fontWeight: FontWeight.w500,
                                    color: const Color.fromRGBO(16, 185, 129, 1),
                                  ),
                                ),
                              ),
                            ],
                          ),
                          SizedBox(height: 48.h),
                          Container(
                            width: double.infinity,
                            padding: EdgeInsets.symmetric(
                              horizontal: 32.w,
                              vertical: 32.h,
                            ),
                            decoration: BoxDecoration(
                              color: const Color.fromRGBO(241, 245, 249, 1),
                              borderRadius: BorderRadius.circular(32.r),
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text(
                                      '设备ID',
                                      style: TextStyle(
                                        fontSize: 24.sp,
                                        color: const Color.fromRGBO(148, 163, 184, 1),
                                      ),
                                    ),
                                    Text(
                                      '${node.id}',
                                      style: TextStyle(
                                        fontSize: 24.sp,
                                        fontWeight: FontWeight.w500,
                                        color: const Color.fromRGBO(15, 23, 42, 1),
                                      ),
                                    ),
                                  ],
                                ),
                                SizedBox(height: 32.h),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text(
                                      '连接方式',
                                      style: TextStyle(
                                        fontSize: 24.sp,
                                        color: const Color.fromRGBO(148, 163, 184, 1),
                                      ),
                                    ),
                                    Text(
                                      '自动检测',
                                      style: TextStyle(
                                        fontSize: 24.sp,
                                        fontWeight: FontWeight.w500,
                                        color: const Color.fromRGBO(15, 23, 42, 1),
                                      ),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                          SizedBox(height: 48.h),
                          SizedBox(
                            width: double.infinity,
                            height: 104.h,
                            child: ElevatedButton(
                              onPressed: () {
                                final deviceId = int.tryParse(node.id) ?? 0;
                                if (deviceId > 0) {
                                  context.pushRoute(
                                    DeviceDetailRoute(deviceId: deviceId),
                                  );
                                }
                              },
                              style: ElevatedButton.styleFrom(
                                backgroundColor: const Color.fromRGBO(59, 130, 246, 1),
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(40.r),
                                ),
                              ),
                              child: Text(
                                '查看详情',
                                style: TextStyle(
                                  fontSize: 40.sp,
                                  color: Colors.white,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  IconData _getDeviceIcon(String name) {
    if (name.contains('环境') || name.contains('温湿')) {
      return Icons.thermostat;
    } else if (name.contains('人体')) {
      return Icons.person_search;
    } else if (name.contains('空开')) {
      return Icons.power;
    } else if (name.contains('网关')) {
      return Icons.router;
    } else if (name.contains('WIFI')) {
      return Icons.wifi;
    } else if (name.contains('串口')) {
      return Icons.usb;
    }
    return Icons.devices;
  }

  String _getDeviceFullName(String name) {
    if (name == '环境采集') {
      return '温湿度传感器';
    }
    return name;
  }
}
