import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:smart_hotel_app/views/report/room/cubit/room_report_state.dart';

class RoomDetailCard extends StatelessWidget {
  final RoomInfo 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.roomNumber,
                        style: TextStyle(
                          fontSize: 36.sp,
                          fontWeight: FontWeight.bold,
                          color: const Color.fromRGBO(31, 41, 55, 1),
                        ),
                      ),
                      SizedBox(width: 16.w),
                      Text(
                        '入住中',
                        style: TextStyle(
                          fontSize: 28.sp,
                          color: const Color.fromRGBO(59, 130, 246, 1),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 12.h),
                  Row(
                    children: [
                      _StatusTag(
                        label: '有人',
                        isActive: room.deviceStatus.hasPerson,
                      ),
                      SizedBox(width: 12.w),
                      _StatusTag(
                        label: '通电中',
                        isActive: room.deviceStatus.powerOn,
                      ),
                      SizedBox(width: 12.w),
                      _StatusTag(
                        label: 'wifi正常',
                        isActive: room.deviceStatus.wifiNormal,
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
          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: [
              _DeviceControl(
                icon: Icons.air_outlined,
                label: '空调',
                value: '${room.deviceStatus.acTemperature}°C·${room.deviceStatus.acOn ? '开' : '关'}',
                isOn: room.deviceStatus.acOn,
              ),
              _DeviceControl(
                icon: Icons.light_outlined,
                label: '照明',
                value: room.deviceStatus.lightOn ? '开' : '关',
                isOn: room.deviceStatus.lightOn,
              ),
              _DeviceControl(
                icon: Icons.tv_outlined,
                label: '电视',
                value: room.deviceStatus.tvOn ? '开' : '关',
                isOn: room.deviceStatus.tvOn,
              ),
              _DeviceControl(
                icon: Icons.curtains_outlined,
                label: '窗帘',
                value: room.deviceStatus.curtainOn ? '开' : '关',
                isOn: room.deviceStatus.curtainOn,
              ),
            ],
          ),
        ],
      ),
    );
  }
}

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 IconData icon;
  final String label;
  final String value;
  final bool isOn;

  const _DeviceControl({
    required this.icon,
    required this.label,
    required this.value,
    required this.isOn,
  });

  @override
  Widget build(BuildContext context) {
    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(
            label,
            style: TextStyle(
              fontSize: 28.sp,
              color: const Color.fromRGBO(59, 130, 246, 1),
              fontWeight: FontWeight.w500,
            ),
          ),
          const Spacer(),
          Text(
            value,
            style: TextStyle(
              fontSize: 26.sp,
              color: isOn
                  ? const Color.fromRGBO(16, 185, 129, 1)
                  : const Color.fromRGBO(156, 163, 175, 1),
            ),
          ),
        ],
      ),
    );
  }
}
