import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:smart_hotel_app/repositories/room_repository.dart';
import 'package:smart_hotel_app/services/room_service.dart';
import 'package:smart_hotel_app/views/report/room/cubit/room_report_cubit.dart';
import 'package:smart_hotel_app/views/report/room/cubit/room_report_state.dart';
import 'package:smart_hotel_app/views/report/room/widget/floor_selector.dart';
import 'package:smart_hotel_app/views/report/room/widget/room_grid.dart';
import 'package:smart_hotel_app/views/report/room/widget/status_stats.dart';
import 'package:smart_hotel_app/views/report/room/widget/room_detail_card.dart';

@RoutePage()
class ReportRoomDetailView extends StatelessWidget {
  const ReportRoomDetailView({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => RoomReportCubit(
        service: RoomService(repository: RoomRepository()),
      ),
      child: BlocBuilder<RoomReportCubit, RoomReportState>(
        builder: (context, state) {
          return Scaffold(
            backgroundColor: const Color.fromRGBO(242, 243, 245, 1),
            appBar: AppBar(
              backgroundColor: Colors.white,
              elevation: 0,
              leading: IconButton(
                icon: const Icon(Icons.arrow_back_ios, color: Colors.black),
                onPressed: () => context.maybePop(),
              ),
              title: Text(
                '客房状态总览',
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 32.sp,
                  fontWeight: FontWeight.bold,
                ),
              ),
              centerTitle: true,
            ),
            body: _buildBody(context, state),
          );
        },
      ),
    );
  }

  Widget _buildBody(BuildContext context, RoomReportState state) {
    if (state.isLoading && state.floors.isEmpty) {
      return const Center(child: CircularProgressIndicator(color: Color.fromRGBO(66, 165, 245, 1)));
    }

    if (state.error != null && state.floors.isEmpty) {
      return Center(child: Text('加载失败: ${state.error}'));
    }

    return SingleChildScrollView(
      child: Padding(
        padding: EdgeInsets.symmetric(horizontal: 28.w),
        child: Column(
          children: [
            SizedBox(height: 10.h),
            Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(24.r),
              ),
              child: Column(
                children: [
                  if (state.floors.isNotEmpty)
                    FloorSelector(
                      floorNames: state.floors.map((f) => f.floorName).toList(),
                      selectedIndex: state.selectedFloorIndex,
                    ),
                  if (state.floors.isNotEmpty && state.selectedFloorIndex < state.floors.length)
                    Padding(
                      padding: EdgeInsets.only(bottom: 28.h),
                      child: RoomGrid(
                        rooms: state.roomGrid,
                        selectedRoomId: state.selectedRoomId,
                        onRoomTap: (roomId) {
                          context.read<RoomReportCubit>().selectRoom(roomId);
                        },
                      ),
                    ),
                  Divider(
                    height: 1.h,
                    thickness: 0.5,
                    color: const Color.fromRGBO(229, 231, 235, 1),
                    indent: 28.w,
                    endIndent: 28.w,
                  ),
                  StatusStats(summary: state.summary),
                  if (state.isLoading)
                    const Padding(
                      padding: EdgeInsets.all(16.0),
                      child: Center(child: CircularProgressIndicator(color: Color.fromRGBO(66, 165, 245, 1.0))),
                    ),
                  ...state.detailRooms
                      .where((room) =>
                          state.selectedRoomId == null ||
                          room.roomBasicInfo.roomId == state.selectedRoomId)
                      .map((room) {
                    return Padding(
                      padding: EdgeInsets.only(bottom: 10.h),
                      child: RoomDetailCard(room: room),
                    );
                  }),
                ],
              ),
            ),
            SizedBox(height: 10.h),
          ],
        ),
      ),
    );
  }

}