import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_switch/flutter_switch.dart';
import 'package:smart_hotel_app/views/service/room/cubit/service_room_state.dart';

class DeviceControlCard extends StatelessWidget {
  final bool mainPowerOn;
  final List<ControlDevice> controlDevices;
  final Function(bool) onMainPowerToggle;
  final Function(int, bool) onDeviceToggle;
  final Function(int, ControlDevice)? onDeviceTap;

  const DeviceControlCard({
    super.key,
    required this.mainPowerOn,
    required this.controlDevices,
    required this.onMainPowerToggle,
    required this.onDeviceToggle,
    this.onDeviceTap,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(24.r),
      ),
      padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 24.h),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '设备控制',
            style: TextStyle(
              fontSize: 32.sp,
              fontWeight: FontWeight.bold,
              color: const Color.fromRGBO(10, 13, 20, 1.0),
            ),
          ),
          SizedBox(height: 20.h),
          _buildMainPowerControl(),
          SizedBox(height: 24.h),
          Text(
            '分设备控制',
            style: TextStyle(
              fontSize: 32.sp,
              fontWeight: FontWeight.bold,
              color: const Color.fromRGBO(10, 13, 20, 1.0),
            ),
          ),
          SizedBox(height: 20.h),
          ...controlDevices.asMap().entries.map((entry) {
            final index = entry.key;
            final device = entry.value;
            return Padding(
              padding: EdgeInsets.only(bottom: index < controlDevices.length - 1 ? 20.h : 0),
              child: _buildDeviceControl(index, device),
            );
          }).toList(),
        ],
      ),
    );
  }

  Widget _buildMainPowerControl() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Container(
                  width: 64.w,
                  height: 64.h,
                  decoration: BoxDecoration(
                    color: const Color.fromRGBO(235, 244, 255, 1.0),
                    borderRadius: BorderRadius.circular(12.r),
                  ),
                  child: Icon(
                    Icons.access_time_outlined,
                    color: const Color.fromRGBO(66, 165, 245, 1.0),
                    size: 36.sp,
                  ),
                ),
                SizedBox(width: 16.w),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '房间总电源',
                      style: TextStyle(
                        fontSize: 28.sp,
                        fontWeight: FontWeight.bold,
                        color: const Color.fromRGBO(10, 13, 20, 1.0),
                      ),
                    ),
                    Text(
                      '控制整个房间供电',
                      style: TextStyle(
                        fontSize: 24.sp,
                        color: const Color.fromRGBO(100, 116, 139, 1.0),
                      ),
                    ),
                  ],
                ),
              ],
            ),
            FlutterSwitch(
              width: 48.0,
              height: 25.0,
              value: mainPowerOn,
              showOnOff: false,
              onToggle: onMainPowerToggle
            ),
            // FlutterSwitch(
            //   value: mainPowerOn,
            //   onToggle: onMainPowerToggle,
            //   activeColor: const Color.fromRGBO(59, 130, 246, 1),
            //   inactiveColor: const Color.fromRGBO(209, 213, 219, 1),
            //   width: 50.w,
            //   height: 28.h,
            //   // toggleSize: 26,
            // ),
          ],
        ),
        SizedBox(height: 20.h),
        Divider(
          color: const Color.fromRGBO(242, 243, 245, 1.0),
          thickness: 1.h,
        ),
      ],
    );
  }

  Widget _buildDeviceControl(int index, ControlDevice device) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: Material(
                color: Colors.transparent,
                child: InkWell(
                  onTap: onDeviceTap != null ? () => onDeviceTap!(index, device) : null,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          Icon(
                            device.icon,
                            color: const Color.fromRGBO(66, 165, 245, 1.0),
                            size: 36.sp,
                          ),
                          SizedBox(width: 8.w),
                          Text(
                            device.name,
                            style: TextStyle(
                              fontSize: 28.sp,
                              fontWeight: FontWeight.bold,
                              color: const Color.fromRGBO(10, 13, 20, 1.0),
                            ),
                          ),
                        ],
                      ),
                      if (device.detailText != null) ...[
                        SizedBox(height: 8.h),
                        Text(
                          device.detailText!,
                          style: TextStyle(
                            fontSize: 24.sp,
                            color: const Color.fromRGBO(100, 116, 139, 1.0),
                          ),
                        ),
                      ],
                    ],
                  ),
                ),
              ),
            ),
            FlutterSwitch(
              width: 48.0,
              height: 25.0,
              value: device.isOn,
              showOnOff: false,
              onToggle: (value) => onDeviceToggle(index, value),
            ),
            // FlutterSwitch(
            //   value: device.isOn,
            //   onToggle: (value) => onDeviceToggle(index, value),
            //   activeColor: const Color.fromRGBO(66, 165, 245, 1.0),
            //   inactiveColor: const Color.fromRGBO(200, 208, 220, 1.0),
            //   width: 60.w,
            //   height: 30.h,
            //   toggleSize: 26,
            // ),
          ],
        ),
        if (index < controlDevices.length - 1) ...[
          SizedBox(height: 20.h),
          Divider(
            color: const Color.fromRGBO(242, 243, 245, 1.0),
            thickness: 1.h,
          ),
        ],
      ],
    );
  }
}
