import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:smart_hotel_app/views/home/abnormal_list/cubit/abnormal_list_state.dart';

class AlarmListItem extends StatelessWidget {
  final AlarmItem item;
  final Function(AlarmItem) onTap;

  const AlarmListItem({
    super.key,
    required this.item,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () => onTap(item),
      child: Container(
        width: double.infinity,
        margin: EdgeInsets.only(top: 20.h),
        padding: EdgeInsets.all(25.w),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(25.r),
          color: Colors.white,
        ),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          spacing: 20.w,
          children: [
            _buildIcon(),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                spacing: 10.h,
                children: [
                  _buildTitleRow(),
                  Row(
                    children: [
                      Expanded(
                          child: Text(
                            item.deviceInfo,
                            style: TextStyle(
                              fontSize: 24.sp,
                              color: const Color.fromRGBO(100, 116, 139, 1),
                            ),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                      ),
                      SizedBox(width: 15.w),
                      Text(
                        item.level == '3' ? '高警' :
                        item.level == '2' ? '中警' :
                        item.level == '1' ? '低警' : '',
                        style: TextStyle(
                          fontSize: 24.sp,
                          color: const Color.fromRGBO(255, 100, 101, 1),
                        ),
                      ),
                      if (item.level == '3') ...[
                        Container(
                          width: 5.w,
                          height: 5.h,
                          margin: EdgeInsets.symmetric(horizontal: 5.w),
                          decoration: const BoxDecoration(
                            color: Color.fromRGBO(255, 100, 101, 1),
                            shape: BoxShape.circle
                          ),
                        ),
                        _buildLevelBadge()
                      ],
                    ],
                  ),
                  Text(
                    '告警时间: ${item.alarmTime}',
                    style: TextStyle(
                      fontSize: 24.sp,
                      color: const Color.fromRGBO(100, 116, 139, 1),
                    ),
                  ),
                  Text(
                    '处理时间: ${item.processTime}',
                    style: TextStyle(
                      fontSize: 24.sp,
                      color: const Color.fromRGBO(100, 116, 139, 1),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildIcon() {
    Color? bgColor;
    String icon = '';
    double iconW = 0;
    double iconH = 0;
    switch (item.alertIcon) {
      case 'temperature'://温度
        bgColor = const Color.fromRGBO(237, 245, 255, 1);
        icon = 'lib/assets/icon/thermostat_bg.png';
        iconW = 20.w;
        iconH = 38.h;
        break;
      case 'voltage'://电压
        bgColor = const Color.fromRGBO(255, 211, 213, 1);
        icon = 'lib/assets/icon/voltage_bg.png';
        iconW = 40.w;
        iconH = 30.h;
        break;
      case 'current'://电流
        bgColor = const Color.fromRGBO(255, 161, 72, 1);
        icon = 'lib/assets/icon/current_bg.png';
        iconW = 30.w;
        iconH = 38.h;
        break;
      case 'alert'://漏电
        bgColor = const Color.fromRGBO(255, 128, 114, 1);
        icon = 'lib/assets/icon/alert_bg.png';
        iconW = 46.w;
        iconH = 32.h;
        break;
      case 'offline'://离线
        bgColor = const Color.fromRGBO(229, 241, 255, 1);
        icon = 'lib/assets/icon/offline_bg.png';
        iconW = 44.w;
        iconH = 32.h;
        break;
    }
    return Container(
      width: 80.w,
      height: 64.h,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.circular(8.r),
      ),
      child: icon.isNotEmpty ? Image.asset(icon, width: iconW, height: iconH) :
      const SizedBox.shrink(),
    );
  }

  Widget _buildTitleRow() {
    return Row(
      children: [
        Expanded(
          child: Text(
            item.title,
            style: TextStyle(
              fontSize: 28.sp,
              color: Colors.black,
              fontWeight: FontWeight.bold,
            ),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ),
        _buildStatusBadge(),
      ],
    );
  }

  Widget _buildStatusBadge() {
    Color? bgColor;
    Color? textColor;
    String text = '';
    String iconImg = '';
    IconData? iconData;
    double iconW = 0;
    double iconH = 0;

    switch (item.status) {
      case AlarmStatus.pending:
        bgColor = const Color.fromRGBO(254, 247, 217, 1);
        textColor = const Color.fromRGBO(250, 204, 21, 1);
        text = '待处理';
        iconImg = 'lib/assets/icon/info_dcl_bg.png';
        iconW = 4.w;
        iconH = 15.h;
        break;
      case AlarmStatus.processing:
        bgColor = const Color.fromRGBO(245, 245, 245, 1);
        textColor = const Color.fromRGBO(51, 51, 51, 1);
        text = '处理中';
        iconData = Icons.check;
        iconW = 18.w;
        iconH = 11.h;
        break;
      case AlarmStatus.processed:
        bgColor = const Color.fromRGBO(209, 250, 229, 1);
        textColor = const Color.fromRGBO(20, 184, 166, 1);
        text = '已处理';
        iconData = Icons.check;
        iconW = 18.w;
        iconH = 11.h;
        break;
      case AlarmStatus.close:
        bgColor = const Color.fromRGBO(245, 245, 245, 1);
        textColor = const Color.fromRGBO(51, 51, 51, 1);
        text = '关闭';
        iconData = Icons.close;
        iconW = 18.w;
        iconH = 15.h;
        break;
      case AlarmStatus.falseAlarm:
        bgColor = const Color.fromRGBO(255, 224, 224, 1);
        textColor = const Color.fromRGBO(255, 100, 101, 1);
        text = '误报';
        iconData = Icons.close;
        iconW = 14.w;
        iconH = 11.h;
        break;
      default:
        break;
    }

    if (text.isEmpty) {
      return const SizedBox.shrink();
    }

    return Container(
      width: 132.w,
      height: 32.h,
      decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.circular(40.r),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        spacing: 6.w,
        children: [
          if (iconData != null)
            Icon(iconData, color: textColor, size: 26.w)
          else
            Image.asset(iconImg, width: iconW, height: iconH),//没有纯感叹号的图标
          Text(
            text,
            style: TextStyle(
              fontSize: 24.sp,
              color: textColor,
              fontWeight: FontWeight.w500,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildLevelBadge() {
    return Container(
      width: 73.w,
      height: 32.h,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: const Color.fromRGBO(255, 100, 101, 0.2),
        borderRadius: BorderRadius.circular(16.r),
        border: Border.all(
          color: const Color.fromRGBO(255, 158, 159, 0.5),
          width: 1.w,
        )
      ),
      child: Text(
        '紧急',
        style: TextStyle(
          fontSize: 24.sp,
          color: const Color.fromRGBO(255, 100, 101, 1),
        ),
      ),
    );
  }

}
