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/routes/app_router.gr.dart';
import 'package:smart_hotel_app/views/service/room/cubit/service_room_cubit.dart';
import 'package:smart_hotel_app/views/service/room/cubit/service_room_state.dart';
import 'package:smart_hotel_app/views/service/room/widget/room_overview_card.dart';
import 'package:smart_hotel_app/views/service/room/widget/room_device_status_card.dart';
import 'package:smart_hotel_app/views/service/room/widget/device_control_card.dart';
import 'package:smart_hotel_app/views/service/room/widget/room_bottom_buttons.dart';

@RoutePage()
class ServiceRoomDetailView extends StatelessWidget {
  final String? roomNumber;

  const ServiceRoomDetailView({super.key, this.roomNumber});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => ServiceRoomCubit(roomNumber: roomNumber),
      child: 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: Color.fromRGBO(100, 116, 139, 1)),
            onPressed: () {
              context.popRoute();
            },
          ),
          title: Text(
            '客房详情',
            style: TextStyle(
              color: const Color.fromRGBO(10, 13, 20, 1),
              fontSize: 32.sp,
              fontWeight: FontWeight.w200,
            ),
          ),
          centerTitle: true,
        ),
        body: SingleChildScrollView(
          padding: EdgeInsets.symmetric(horizontal: 28.w),
          child: BlocBuilder<ServiceRoomCubit, ServiceRoomState>(
            builder: (context, state) {
              final cubit = context.read<ServiceRoomCubit>();
              return Column(
                children: [
                  SizedBox(height: 20.h),
                  RoomOverviewCard(
                    roomNumber: state.roomNumber,
                    roomStatus: state.roomStatus,
                    statusTags: state.statusTags,
                  ),
                  SizedBox(height: 20.h),
                  RoomDeviceStatusCard(
                    devices: state.roomDevices,
                  ),
                  SizedBox(height: 20.h),
                  DeviceControlCard(
                    mainPowerOn: state.mainPowerOn,
                    controlDevices: state.controlDevices,
                    onMainPowerToggle: (value) => cubit.toggleMainPower(value),
                    onDeviceToggle: (index, value) => cubit.toggleControlDevice(index, value),
                    onDeviceTap: (index, device) {
                      if (device.name == '空调') {
                        context.pushRoute(const ServiceDeviceDetailRoute());
                      }
                    },
                  ),
                  SizedBox(height: 20.h),
                  const RoomBottomButtons(),
                  SizedBox(height: 20.h),
                ],
              );
            },
          ),
        ),
      ),
    );
  }
}