import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:laki_icu_app/models/bo/monitoring_bo.dart';
import 'package:laki_icu_app/views/monitoring/index/cubit/monitoring_index_cubit.dart';

class MonitoringAlertCard extends StatelessWidget {
  final List<AlertInfoBO> alerts;

  const MonitoringAlertCard({
    super.key,
    required this.alerts,
  });

  @override
  Widget build(BuildContext context) {
    final hasEmpty = alerts.isEmpty; // alerts.any((item) => item.status.contains('待处理'));
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 35.w, vertical: 30.h),
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage(
            hasEmpty
                ? 'lib/assets/monitoring/alert_box_bg_1.png'
                : 'lib/assets/monitoring/alert_box_bg_2.png',
          ),
          fit: BoxFit.fill,
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '告警信息',
            style: TextStyle(
              color: Colors.white,
              fontSize: 28.sp,
              fontWeight: FontWeight.bold,
            ),
          ),
          SizedBox(height: 24.h),
          Expanded(
            child: hasEmpty ? _buildEmpty() : _buildList(),
          ),
        ],
      ),
    );
  }

  Widget _buildEmpty() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            'lib/assets/monitoring/cat_stand.png',
            width: 160.w,
            height: 163.h,
          ),
          SizedBox(height: 40.h),
          Text(
            '设备运行正常',
            style: TextStyle(color: Colors.white, fontSize: 32.sp),
          ),
        ],
      ),
    );
  }

  Widget _buildList() {
    return ListView.separated(
      // padding: EdgeInsets.symmetric(vertical: 6.h),
      itemCount: alerts.length,
      separatorBuilder: (_, __) => SizedBox(height: 32.h),
      itemBuilder: (context, index) {
        final item = alerts[index];
        final pending = item.status.contains('待处理');
        return _buildAlertItem(
          context,
          item,
          pending,
          key: ValueKey('${item.title}_${item.time}_$index'),
        );
      },
    );
  }

  Widget _buildAlertItem(
    BuildContext context,
    AlertInfoBO item,
    bool pending, {
    Key? key,
  }) {
    return InkWell(
      key: key,
      onTap: () => context.read<MonitoringIndexCubit>().selectAlert(item),
      borderRadius: BorderRadius.circular(16.r),
      child: IntrinsicHeight(
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildIconBlock(pending),
            SizedBox(width: 20.w),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Text(
                    item.title,
                    style: TextStyle(
                      color: pending ? const Color(0xFFF26522) : const Color(0xFFFFC14A),
                      fontSize: 28.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(height: 8.h),
                  Text(
                    item.description,
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 23.sp,
                    ),
                  ),
                  // SizedBox(height: 10.h),
                  Text(
                    item.time,
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 20.sp,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildIconBlock(bool pending) {
    return Container(
      width: 196.w,
      height: 148.h,
      alignment: Alignment.topLeft,
      child: Stack(
        children: [
          Container( 
            decoration: BoxDecoration(
              color: const Color(0xFF2A2E3B),
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(18.r),
                topRight: Radius.circular(18.r),
                bottomLeft: Radius.circular(18.r),
                bottomRight: Radius.circular(18.r),
              ),
            ),
          ),
          Container(
            // width: 83.w,
            // height: 34.h,
            padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 6.h),
            decoration: BoxDecoration(
              color: pending ? const Color(0xFFFBB03B) : const Color(0xFF6FA8DC),
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(18.r),
                bottomRight: Radius.circular(18.r),
              ),
            ),
            child: Text(
              pending ? '待处理' : '已处理',
              style: TextStyle(
                color: Colors.white,
                fontSize: 16.sp,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
