import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:smart_hotel_app/utils/deal_utils.dart';
import 'package:smart_hotel_app/views/home/device/cubit/device_state.dart';

class DeviceParmsBlock extends StatelessWidget {
  final DeviceInfo deviceInfo;

  const DeviceParmsBlock({super.key, required this.deviceInfo});

  @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: 20.h),
          Row(
            children: [
              Expanded(
                child: _buildParamCard(deviceInfo.voltage, '电压', 'V'),
              ),
              SizedBox(width: 15.w),
              Expanded(
                child: _buildParamCard(deviceInfo.current, '电流', 'A'),
              ),
            ],
          ),
          SizedBox(height: 15.h),
          Row(
            children: [
              Expanded(
                child: _buildParamCard(deviceInfo.power, '有公功率', 'W', isGreen: true),
              ),
              SizedBox(width: 15.w),
              Expanded(
                child: _buildParamCard(deviceInfo.temperature, '设备温度', '°C'),
              ),
            ],
          ),
        ],
      ),
    );
  }

  // String _formatValue(String raw) {
  //   final d = double.tryParse(raw);
  //   if (d == null) return raw;
  //   // 去掉末尾多余的 0，整数不显示小数点
  //   final formatted = d.toStringAsFixed(2);
  //   final parts = formatted.split('.');
  //   if (parts.length == 2 && parts[1] == '00') return parts[0];
  //   return formatted.replaceAll(RegExp(r'0+$'), '').replaceAll(RegExp(r'\.$'), '');
  // }

  Widget _buildParamCard(String value, String label, String unit, {bool isGreen = false}) {
    return Container(
      padding: EdgeInsets.all(25.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(25.r),
        color: const Color.fromRGBO(229, 241, 255, 1),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text.rich(
            TextSpan(
              children: [
                TextSpan(
                  text: DealUtils.cleanNumber(value),
                  style: TextStyle(
                    fontSize: 40.sp,
                    color: isGreen ? const Color.fromRGBO(20, 184, 166, 1)
                        : const Color.fromRGBO(73, 149, 234, 1),
                    fontWeight: FontWeight.bold,
                  ),
                ),
                TextSpan(
                  text: unit,
                  style: TextStyle(
                    fontSize: 40.sp,
                    color: isGreen ? const Color.fromRGBO(20, 184, 166, 1)
                        : const Color.fromRGBO(73, 149, 234, 1),
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ],
            ),
          ),
          SizedBox(height: 8.h),
          Text(
            label,
            style: TextStyle(
              fontSize: 24.sp,
              color: const Color.fromRGBO(100, 116, 139, 1),
            ),
          ),
        ],
      ),
    );
  }
}