import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:smart_hotel_app/views/home/abnormal/cubit/abnormal_state.dart';

class AlarmInfoBlock extends StatelessWidget {
  final AlarmInfo alarmInfo;

  const AlarmInfoBlock({super.key, required this.alarmInfo});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(top: 10.h),
      padding: EdgeInsets.all(25.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(25.r),
        color: Colors.white,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '电压波动告警',
            style: TextStyle(
              fontSize: 28.sp,
              color: Colors.black,
              fontWeight: FontWeight.bold,
            ),
          ),
          SizedBox(height: 30.h),
          _buildInfoRow('触发时间', alarmInfo.triggerTime),
          SizedBox(height: 25.h),
          _buildInfoRow(
            '告警级别',
            '${alarmInfo.alarmLevel}(Level 3)',
            valueColor: Color.fromRGBO(255, 100, 101, 1),
          ),
          SizedBox(height: 25.h),
          _buildInfoRow('闭值设置', alarmInfo.threshold),
          SizedBox(height: 25.h),
          _buildInfoRow('持续时间', alarmInfo.duration),
          SizedBox(height: 25.h),
          _buildInfoRow(
            '峰值记录',
            alarmInfo.peak,
            valueColor: Color.fromRGBO(250, 204, 21, 1),
          ),
          SizedBox(height: 25.h),
          _buildInfoRow(
            '关联联动',
            alarmInfo.linkAction,
            valueColor: Color.fromRGBO(73, 149, 234, 1),
          ),
        ],
      ),
    );
  }

  String _truncateText(String text, {int maxLength = 20}) {
    if (text.length <= maxLength) return text;
    return '${text.substring(0, maxLength)}...';
  }

  Widget _buildInfoRow(String label, String value, {Color? valueColor}) {
    final displayValue = _truncateText(value);
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          label,
          style: TextStyle(
            fontSize: 24.sp,
            color: Color.fromRGBO(100, 116, 139, 1),
          ),
        ),
        Text(
          displayValue,
          style: TextStyle(
            fontSize: 24.sp,
            color: valueColor ?? Color.fromRGBO(100, 116, 139, 1),
          ),
        ),
      ],
    );
  }
}