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: [
          _buildMainPowerControl(),
          SizedBox(height: 30.h),
          Text(
            '分设备控制',
            style: TextStyle(
              fontSize: 32.sp,
              fontWeight: FontWeight.bold,
              color: const Color.fromRGBO(10, 13, 20, 1.0),
            ),
          ),
          ...controlDevices.asMap().entries.map((entry) {
            final index = entry.key;
            final device = entry.value;
            return _buildDeviceControl(index, device);
          }).toList(),
        ],
      ),
    );
  }

  Widget _buildMainPowerControl() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              spacing: 16.w,
              children: [
                Container(
                  width: 64.w,
                  height: 53.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,
                  ),
                ),
                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: 80.w,
              height: 35.h,
              valueFontSize: 20.sp,
              toggleSize: 25.h,
              borderRadius: 20.r,
              padding: 4.w,
              value: mainPowerOn,
              activeColor: const Color.fromRGBO(80, 162, 255, 1),
              inactiveColor: const Color.fromRGBO(210, 213, 218, 1),
              onToggle: onMainPowerToggle,
            ),
          ],
        ),
        SizedBox(height: 30.h),
        Divider(
          height: 0.5.h,
          color: const Color.fromRGBO(100, 116, 139, 0.2),
          thickness: 0.5,
        ),
      ],
    );
  }

  Widget _buildDeviceControl(int index, ControlDevice device) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        SizedBox(
          height: 80.h,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(
                child: GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: onDeviceTap != null ? () => onDeviceTap!(index, device) : null,
                  child: Row(
                    spacing: 8.w,
                    children: [
                      Icon(
                        device.icon,
                        color: const Color.fromRGBO(66, 165, 245, 1.0),
                        size: 36.sp,
                      ),
                      Text(
                        device.name,
                        style: TextStyle(
                          fontSize: 28.sp,
                          fontWeight: FontWeight.bold,
                          color: const Color.fromRGBO(10, 13, 20, 1.0),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              FlutterSwitch(
                width: 80.w,
                height: 35.h,
                valueFontSize: 20.sp,
                toggleSize: 25.h,
                borderRadius: 20.r,
                padding: 4.w,
                value: device.isOn,
                activeColor: const Color.fromRGBO(80, 162, 255, 1),
                inactiveColor: const Color.fromRGBO(210, 213, 218, 1),
                onToggle: (value) => onDeviceToggle(index, value),
              ),
            ],
          ),
        ),
        if (index < controlDevices.length - 1) ...[
          Divider(
            height: 0.5.h,
            color: const Color.fromRGBO(100, 116, 139, 0.2),
            thickness: 0.5,
          ),
        ],
      ],
    );
  }
}
