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;

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

  @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: 20.h,
        crossAxisSpacing: 16.w,
        children: rooms.map((room) => _RoomItem(room: room)).toList(),
      ),
    );
  }
}

class _RoomItem extends StatelessWidget {
  final RoomGridBO room;

  const _RoomItem({required this.room});

  Color get _bgColor {
    switch (room.statusColor) {
      case 'occupied':
        return const Color.fromRGBO(219, 234, 254, 1);
      case 'alarm':
        return const Color.fromRGBO(254, 243, 199, 1);
      case 'reserved':
        return const Color.fromRGBO(219, 234, 254, 1);
      case 'dnd':
        return const Color.fromRGBO(254, 243, 199, 1);
      case 'checkout':
      case 'idle':
      default:
        return const Color.fromRGBO(229, 231, 235, 1);
    }
  }

  Color get _textColor {
    switch (room.statusColor) {
      case 'occupied':
        return const Color.fromRGBO(59, 130, 246, 1);
      case 'alarm':
        return const Color.fromRGBO(245, 158, 11, 1);
      case 'reserved':
        return const Color.fromRGBO(59, 130, 246, 1);
      case 'dnd':
        return const Color.fromRGBO(245, 158, 11, 1);
      case 'checkout':
      case 'idle':
      default:
        return const Color.fromRGBO(156, 163, 175, 1);
    }
  }

  Color? get _borderColor {
    switch (room.statusColor) {
      case 'idle':
      case 'checkout':
        return null;
      case 'occupied':
      case 'reserved':
        return const Color.fromRGBO(59, 130, 246, 1);
      case 'alarm':
      case 'dnd':
        return const Color.fromRGBO(245, 158, 11, 1);
      default:
        return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 12.h, horizontal: 8.w),
      decoration: BoxDecoration(
        color: _bgColor,
        borderRadius: BorderRadius.circular(16.r),
        border: _borderColor != null
            ? Border.all(color: _borderColor!, width: 2.w)
            : null,
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            room.roomNumber,
            style: TextStyle(
              fontSize: 28.sp,
              fontWeight: FontWeight.bold,
              color: _textColor,
            ),
          ),
          SizedBox(height: 4.h),
          if (room.hasAlarmIcon)
            Icon(Icons.warning_amber_outlined, color: _textColor, size: 20.sp),
          if (!room.hasAlarmIcon)
            Text(
              room.statusText,
              style: TextStyle(
                fontSize: 20.sp,
                color: _textColor,
              ),
            ),
        ],
      ),
    );
  }
}
