import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:smart_hotel_app/models/bo/room_bo.dart';

class RoomDetailCard extends StatelessWidget {
  final RoomDetailListBO room;

  const RoomDetailCard({super.key, required this.room});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(28.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(24.r),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                width: 60.w,
                height: 60.h,
                decoration: BoxDecoration(
                  color: const Color.fromRGBO(219, 234, 254, 1),
                  borderRadius: BorderRadius.circular(12.r),
                ),
                child: Icon(
                  Icons.meeting_room_outlined,
                  color: const Color.fromRGBO(59, 130, 246, 1),
                  size: 32.sp,
                ),
              ),
              SizedBox(width: 16.w),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Text(
                        room.roomBasicInfo.roomNumber,
                        style: TextStyle(
                          fontSize: 36.sp,
                          fontWeight: FontWeight.bold,
                          color: const Color.fromRGBO(31, 41, 55, 1),
                        ),
                      ),
                      SizedBox(width: 16.w),
                      Text(
                        room.roomBasicInfo.roomStatusText,
                        style: TextStyle(
                          fontSize: 28.sp,
                          color: const Color.fromRGBO(59, 130, 246, 1),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 12.h),
                  Row(
                    children: [
                      _StatusTag(
                        label: '有人',
                        isActive: room.statusTags.isOccupied,
                      ),
                      SizedBox(width: 12.w),
                      _StatusTag(
                        label: '通电中',
                        isActive: room.statusTags.powerSupplyStatus,
                      ),
                      SizedBox(width: 12.w),
                      _StatusTag(
                        label: 'wifi正常',
                        isActive: room.statusTags.wifiNormalStatus,
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
          SizedBox(height: 24.h),
          Divider(
            height: 1.h,
            color: const Color.fromRGBO(229, 231, 235, 1),
          ),
          SizedBox(height: 24.h),
          GridView.count(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            crossAxisCount: 2,
            mainAxisSpacing: 16.h,
            crossAxisSpacing: 16.w,
            children: room.deviceList.map((device) => _DeviceControl(device: device)).toList(),
          ),
        ],
      ),
    );
  }
}

class _StatusTag extends StatelessWidget {
  final String label;
  final bool isActive;

  const _StatusTag({required this.label, required this.isActive});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
      decoration: BoxDecoration(
        color: isActive
            ? const Color.fromRGBO(167, 243, 208, 1)
            : const Color.fromRGBO(229, 231, 235, 1),
        borderRadius: BorderRadius.circular(12.r),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (isActive)
            Icon(
              Icons.check,
              size: 20.sp,
              color: const Color.fromRGBO(16, 185, 129, 1),
            ),
          if (isActive)
            SizedBox(width: 4.w),
          Text(
            label,
            style: TextStyle(
              fontSize: 22.sp,
              color: isActive
                  ? const Color.fromRGBO(16, 185, 129, 1)
                  : const Color.fromRGBO(156, 163, 175, 1),
            ),
          ),
        ],
      ),
    );
  }
}

class _DeviceControl extends StatelessWidget {
  final DeviceItemBO device;

  const _DeviceControl({required this.device});

  IconData _icon() {
    switch (device.deviceTypeIcon) {
      case 'ac':
        return Icons.air_outlined;
      case 'light':
        return Icons.light_outlined;
      case 'tv':
        return Icons.tv_outlined;
      case 'curtain':
        return Icons.curtains_outlined;
      default:
        return Icons.devices_other;
    }
  }

  @override
  Widget build(BuildContext context) {
    final isOn = device.powerStatus == '1';
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 24.w, vertical: 24.h),
      decoration: BoxDecoration(
        color: const Color.fromRGBO(239, 246, 255, 1),
        borderRadius: BorderRadius.circular(24.r),
      ),
      child: Row(
        children: [
          Icon(
            _icon(),
            color: const Color.fromRGBO(59, 130, 246, 1),
            size: 40.sp,
          ),
          SizedBox(width: 16.w),
          Text(
            device.deviceName,
            style: TextStyle(
              fontSize: 28.sp,
              color: const Color.fromRGBO(59, 130, 246, 1),
              fontWeight: FontWeight.w500,
            ),
          ),
          const Spacer(),
          Text(
            device.paramDisplayText.isNotEmpty ? device.paramDisplayText : device.powerStatusText,
            style: TextStyle(
              fontSize: 26.sp,
              color: isOn
                  ? const Color.fromRGBO(16, 185, 129, 1)
                  : const Color.fromRGBO(156, 163, 175, 1),
            ),
          ),
        ],
      ),
    );
  }
}
