import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class EnergyStatsCardBlock extends StatelessWidget {
  final double todayEnergy;
  final double todayEnergyChange;
  final double monthEnergy;
  final double monthEnergyChange;
  final double monthBill;

  const EnergyStatsCardBlock({
    super.key,
    required this.todayEnergy,
    required this.todayEnergyChange,
    required this.monthEnergy,
    required this.monthEnergyChange,
    required this.monthBill,
  });

  String _fmt(double d) {
    if (d == d.truncateToDouble()) return d.truncate().toString();
    return d.toStringAsFixed(2)
        .replaceAll(RegExp(r'0+$'), '')
        .replaceAll(RegExp(r'\.$'), '');
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: Row(
        children: [
          Expanded(
            child: _buildCard(
              _fmt(todayEnergy),
              '今日(kwh)',
              '${_fmt(todayEnergyChange)}%',
              true,
              const Color.fromRGBO(59, 130, 246, 1),
            ),
          ),
          SizedBox(width: 12.w),
          Expanded(
            child: _buildCard(
              _fmt(monthEnergy),
              '本月(kWh)',
              '${_fmt(monthEnergyChange)}%',
              true,
              const Color.fromRGBO(59, 130, 246, 1),
            ),
          ),
          SizedBox(width: 12.w),
          Expanded(
            child: _buildCard(
              '¥${_fmt(monthBill)}',
              '本月电费预估',
              null,
              null,
              const Color.fromRGBO(16, 185, 129, 1),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildCard(String value, String label, String? change, bool? isDown, Color valueColor) {
    return Container(
      height: 128.h,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20.r),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        spacing: 4.h,
        children: [
          Text(
            value,
            style: TextStyle(
              color: valueColor,
              fontSize: 36.sp,
              fontWeight: FontWeight.w500,
            ),
            textAlign: TextAlign.center,
          ),
          Text(
            label,
            style: TextStyle(
              color: Colors.black54,
              fontSize: 24.sp,
            ),
          ),
          if (change != null)
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(
                  isDown! ? Icons.arrow_downward : Icons.arrow_upward,
                  color: isDown
                      ? const Color.fromRGBO(16, 185, 129, 1)
                      : const Color.fromRGBO(239, 68, 68, 1),
                  size: 24.sp,
                ),
                Text(
                  change,
                  style: TextStyle(
                    color: isDown
                        ? const Color.fromRGBO(16, 185, 129, 1)
                        : const Color.fromRGBO(239, 68, 68, 1),
                    fontSize: 24.sp,
                  ),
                ),
              ],
            )
        ],
      ),
    );
  }
}