import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:smart_hotel_app/models/bo/room_bo.dart';

/// 非客房-相关楼层区域设备列表组件
class AreaDeviceList extends StatelessWidget {
  final List<AreaDeviceBO> devices;
  final bool isLoading;

  const AreaDeviceList({
    super.key,
    required this.devices,
    this.isLoading = false,
  });

  /// 根据运行状态获取显示颜色
  Color _statusColor(String runStatus) {
    switch (runStatus) {
      case 'normal':
        return const Color.fromRGBO(82, 196, 26, 1);
      case 'warning':
        return const Color.fromRGBO(250, 173, 20, 1);
      case 'fault':
        return const Color.fromRGBO(245, 34, 45, 1);
      case 'offline':
      default:
        return const Color.fromRGBO(153, 153, 153, 1);
    }
  }

  /// 根据运行状态获取显示文字
  String _statusText(String runStatus) {
    switch (runStatus) {
      case 'normal':
        return '正常';
      case 'warning':
        return '预警';
      case 'fault':
        return '故障';
      case 'offline':
        return '离线';
      default:
        return '未知';
    }
  }

  @override
  Widget build(BuildContext context) {
    if (isLoading) {
      return const Center(
        child: Padding(
          padding: EdgeInsets.all(20),
          child: CircularProgressIndicator(),
        ),
      );
    }

    if (devices.isEmpty) {
      return Container(
        width: double.infinity,
        padding: EdgeInsets.symmetric(vertical: 40.h),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(15.r),
          color: const Color.fromRGBO(245, 245, 245, 1),
        ),
        child: Center(
          child: Text(
            '暂无设备数据',
            style: TextStyle(
              fontSize: 24.sp,
              color: const Color.fromRGBO(153, 153, 153, 1),
            ),
          ),
        ),
      );
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: EdgeInsets.only(bottom: 10.h),
          child: Text(
            '区域设备（共${devices.length}个）',
            style: TextStyle(
              fontSize: 24.sp,
              color: const Color.fromRGBO(100, 116, 139, 1),
            ),
          ),
        ),
        ...devices.map((device) => _buildDeviceItem(device)),
      ],
    );
  }

  Widget _buildDeviceItem(AreaDeviceBO device) {
    final color = _statusColor(device.runStatus);
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(bottom: 8.h),
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(12.r),
        color: Colors.white,
        border: Border.all(
          color: const Color.fromRGBO(238, 238, 238, 1),
          width: 1.w,
        ),
      ),
      child: Row(
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  device.deviceName,
                  style: TextStyle(
                    fontSize: 26.sp,
                    fontWeight: FontWeight.w500,
                    color: const Color.fromRGBO(51, 51, 51, 1),
                  ),
                ),
                SizedBox(height: 4.h),
                Text(
                  '编码：${device.deviceCode}',
                  style: TextStyle(
                    fontSize: 22.sp,
                    color: const Color.fromRGBO(153, 153, 153, 1),
                  ),
                ),
              ],
            ),
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 4.h),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8.r),
              color: color.withValues(alpha: 0.1),
            ),
            child: Text(
              _statusText(device.runStatus),
              style: TextStyle(
                fontSize: 22.sp,
                color: color,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
