import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:smart_hotel_app/utils/deal_utils.dart';

class ReportMetricsBlock extends StatelessWidget {
  final Map<String, dynamic> reportMetrics;

  const ReportMetricsBlock({super.key, required this.reportMetrics});

  @override
  Widget build(BuildContext context) {
    final powerUsage = reportMetrics.containsKey('powerUsage') &&
        reportMetrics['powerUsage'] != null
        ? Map<String, dynamic>.from(reportMetrics['powerUsage']) : {};
    final pendingAlerts = reportMetrics.containsKey('pendingAlerts') &&
        reportMetrics['pendingAlerts'] != null
        ? Map<String, dynamic>.from(reportMetrics['pendingAlerts']) : {};

    return Row(
      children: [
        Expanded(
          child: _buildMetricCard(
            title: '今日用电',
            value: DealUtils.cleanNumber(powerUsage.containsKey('value') ?
                powerUsage['value'] as String : ''),
            unit: powerUsage['unit'] as String?,
            changeText: powerUsage['changeText'] as String?,
            changeColor: const Color.fromRGBO(20, 184, 166, 1),
            valueColor: const Color.fromRGBO(73, 149, 234, 1),
            progress: powerUsage['progress'] as double?,
            progressColor: const Color.fromRGBO(73, 149, 234, 1),
          ),
        ),
        SizedBox(width: 20.w),
        Expanded(
          child: _buildMetricCard(
            title: '待处理告警',
            value: pendingAlerts['value'] as String?,
            unit: pendingAlerts['unit'] as String?,
            changeText: pendingAlerts['changeText'] as String?,
            changeColor: const Color.fromRGBO(250, 204, 21, 1),
            valueColor: const Color.fromRGBO(250, 204, 21, 1),
            progress: pendingAlerts['progress'] as double?,
            progressColor: const Color.fromRGBO(250, 204, 21, 1),
          ),
        ),
      ],
    );
  }

  Widget _buildMetricCard({
    required String title,
    required String? value,
    required String? unit,
    required String? changeText,
    required Color changeColor,
    required Color valueColor,
    required double? progress,
    required Color progressColor,
  }) {
    return Container(
      padding: EdgeInsets.all(20.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(16.r),
        color: Colors.white,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                title,
                style: TextStyle(
                  fontSize: 24.sp,
                  color: const Color.fromRGBO(107, 114, 128, 1),
                ),
              ),
              Text(
                changeText ?? '',
                style: TextStyle(
                  fontSize: 24.sp,
                  color: changeColor,
                ),
              ),
            ],
          ),
          SizedBox(height: 12.h),
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text(
                value ?? '',
                style: TextStyle(
                  fontSize: 40.sp,
                  fontWeight: FontWeight.bold,
                  color: valueColor,
                ),
              ),
              SizedBox(width: 4.w),
              Padding(
                padding: EdgeInsets.only(bottom: 6.h),
                child: Text(
                  unit ?? '',
                  style: TextStyle(
                    fontSize: 20.sp,
                    color: const Color.fromRGBO(107, 114, 128, 1),
                  ),
                ),
              ),
            ],
          ),
          SizedBox(height: 12.h),
          ClipRRect(
            borderRadius: BorderRadius.circular(4.r),
            child: LinearProgressIndicator(
              value: progress,
              backgroundColor: const Color.fromRGBO(229, 231, 235, 1),
              valueColor: AlwaysStoppedAnimation<Color>(progressColor),
              minHeight: 6.h,
            ),
          ),
        ],
      ),
    );
  }
}