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 TemperatureBlock extends StatelessWidget {
  final AlarmInfo alarmInfo;

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

  @override
  Widget build(BuildContext context) {
    return 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: Column(
        spacing: 5.h,
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            spacing: 20.w,
            children: [
              _buildIcon(),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  spacing: 5.h,
                  children: [
                    Row(
                      children: [
                        Expanded(
                            child: Text(
                              alarmInfo.alarmType,
                              style: TextStyle(
                                fontSize: 28.sp,
                                color: Colors.black,
                                fontWeight: FontWeight.bold,
                              ),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                        ),
                        Text(
                          alarmInfo.alertLevelText == '3' ? '高警' :
                          alarmInfo.alertLevelText == '2' ? '中警' :
                          alarmInfo.alertLevelText == '1' ? '低警' : '',
                          style: TextStyle(
                            fontSize: 24.sp,
                            color: const Color.fromRGBO(255, 100, 101, 1),
                          ),
                        ),
                        if (alarmInfo.alertLevelText == '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(
                      alarmInfo.deviceInfo,
                      style: TextStyle(
                        fontSize: 24.sp,
                        color: const Color.fromRGBO(100, 116, 139, 1),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
          Row(
            children: [
              SizedBox(width: 85.w),
              Text(
                '告警数量：',
                style: TextStyle(
                  fontSize: 24.sp,
                  color: const Color.fromRGBO(100, 116, 139, 1),
                ),
              ),
              Text(
                alarmInfo.alertCount.toString(),
                style: TextStyle(
                  fontSize: 24.sp,
                  color: const Color.fromRGBO(255, 100, 101, 1),
                ),
              ),
            ],
          ),
        ],
      )
    );
  }

  Widget _buildIcon() {
    Color? bgColor;
    String icon = '';
    double iconW = 0;
    double iconH = 0;
    switch (alarmInfo.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: 64.w,
      height: 53.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 _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),
        ),
      ),
    );
  }

}