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

class RoomGrid extends StatelessWidget {
  final List<RoomGridBO> rooms;
  final int? selectedRoomId;
  final Function(int) onRoomTap;

  const RoomGrid({
    super.key,
    required this.rooms,
    required this.selectedRoomId,
    required this.onRoomTap,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.symmetric(horizontal: 28.w),
      child: GridView.count(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        crossAxisCount: 5,
        mainAxisSpacing: 16.h,
        crossAxisSpacing: 16.w,
        children: rooms.map((room) => _RoomItem(
          room: room,
          isSelected: room.roomId == selectedRoomId,
          onTap: () => onRoomTap(room.roomId),
        )).toList(),
      ),
    );
  }
}

class _RoomItem extends StatelessWidget {
  final RoomGridBO room;
  final bool isSelected;
  final VoidCallback onTap;

  const _RoomItem({
    required this.room,
    required this.isSelected,
    required this.onTap,
  });

  Color get _bgColor {
    switch (room.statusColor) {
      case 'blue':
        return const Color.fromRGBO(229, 241, 255, 1);
      case 'yellow':
        return const Color.fromRGBO(255, 251, 237, 1);
      case 'default':
      case 'gray':
      default:
        return const Color.fromRGBO(238, 238, 238, 1);
    }
  }

  Color get _textColor {
    switch (room.statusColor) {
      case 'blue':
        return const Color.fromRGBO(74, 149, 234, 1);
      case 'yellow':
        return const Color.fromRGBO(250, 204, 21, 1);
      case 'default':
      case 'gray':
      default:
        return const Color.fromRGBO(100, 116, 139, 1);
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 12.h),
        decoration: BoxDecoration(
          color: _bgColor,
          borderRadius: BorderRadius.circular(16.r),
          border: isSelected
              ? Border.all(color: const Color.fromRGBO(80, 162, 255, 1), width: 1.w)
              : null,
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          spacing: 4.h,
          children: [
            Text(
              room.roomNumber,
              style: TextStyle(
                fontSize: 28.sp,
                fontWeight: FontWeight.w500,
                color: _textColor,
              ),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                if (room.hasAlarmIcon)
                  Icon(Icons.warning_amber_outlined, color: _textColor, size: 24.sp),
                Flexible(
                  fit: FlexFit.loose,  //让文本只占实际需要的宽度
                  child: Text(
                    room.statusText,
                    style: TextStyle(
                      fontSize: 24.sp,
                      color: _textColor,
                    ),
                    textAlign: TextAlign.center,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

}
