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(
      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,
          children: [
            _buildIcon(),
            SizedBox(width: 20.w),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _buildTitleRow(),
                  SizedBox(height: 15.h),
                  Text(
                    item.deviceInfo,
                    style: TextStyle(
                      fontSize: 24.sp,
                      color: const Color.fromRGBO(100, 116, 139, 1),
                    ),
                  ),
                  SizedBox(height: 10.h),
                  Text(
                    '告警时间: ${item.alarmTime}',
                    style: TextStyle(
                      fontSize: 24.sp,
                      color: const Color.fromRGBO(100, 116, 139, 1),
                    ),
                  ),
                  SizedBox(height: 10.h),
                  Text(
                    '处理时间: ${item.processTime}',
                    style: TextStyle(
                      fontSize: 24.sp,
                      color: const Color.fromRGBO(100, 116, 139, 1),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildIcon() {
    return Container(
      width: 96.w,
      height: 96.w,
      decoration: BoxDecoration(
        color: const Color.fromRGBO(237, 245, 255, 1),
        borderRadius: BorderRadius.circular(16.r),
      ),
      child: Icon(
        Icons.thermostat,
        size: 48.w,
        color: const Color.fromRGBO(73, 149, 234, 1),
      ),
    );
  }

  Widget _buildTitleRow() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          item.title,
          style: TextStyle(
            fontSize: 28.sp,
            color: Colors.black,
            fontWeight: FontWeight.bold,
          ),
        ),
        Row(
          children: [
            _buildStatusBadge(),
            SizedBox(width: 12.w),
            _buildLevelBadge(),
            SizedBox(width: 8.w),
            _buildUrgencyBadge(),
          ],
        ),
      ],
    );
  }

  Widget _buildStatusBadge() {
    Color bgColor;
    Color textColor;
    String text;
    IconData? icon;

    switch (item.status) {
      case AlarmStatus.pending:
        bgColor = const Color.fromRGBO(250, 204, 21, 0.15);
        textColor = const Color.fromRGBO(250, 204, 21, 1);
        text = '待处理';
        icon = Icons.error_outline;
        break;
      case AlarmStatus.processed:
        bgColor = const Color.fromRGBO(84, 214, 120, 0.15);
        textColor = const Color.fromRGBO(84, 214, 120, 1);
        text = '已处理';
        icon = Icons.check_circle_outline;
        break;
      case AlarmStatus.falseAlarm:
        bgColor = const Color.fromRGBO(255, 100, 101, 0.15);
        textColor = const Color.fromRGBO(255, 100, 101, 1);
        text = '误报';
        icon = Icons.cancel_outlined;
        break;
      default:
        bgColor = const Color.fromRGBO(100, 116, 139, 0.15);
        textColor = const Color.fromRGBO(100, 116, 139, 1);
        text = '';
    }

    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
      decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.circular(12.r),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (icon != null) ...[
            Icon(icon, size: 20.w, color: textColor),
            SizedBox(width: 6.w),
          ],
          Text(
            text,
            style: TextStyle(
              fontSize: 22.sp,
              color: textColor,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildLevelBadge() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
      decoration: BoxDecoration(
        color: const Color.fromRGBO(255, 100, 101, 0.15),
        borderRadius: BorderRadius.circular(12.r),
      ),
      child: Text(
        item.level,
        style: TextStyle(
          fontSize: 22.sp,
          color: const Color.fromRGBO(255, 100, 101, 1),
        ),
      ),
    );
  }

  Widget _buildUrgencyBadge() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
      decoration: BoxDecoration(
        color: const Color.fromRGBO(255, 100, 101, 0.15),
        borderRadius: BorderRadius.circular(12.r),
      ),
      child: Text(
        item.urgency,
        style: TextStyle(
          fontSize: 22.sp,
          color: const Color.fromRGBO(255, 100, 101, 1),
        ),
      ),
    );
  }
}
