import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
import 'package:syncrow_app/features/menu/bloc/manage_unit_bloc/manage_unit_bloc.dart';
import 'package:syncrow_app/features/menu/bloc/manage_unit_bloc/manage_unit_event.dart';
import 'package:syncrow_app/features/menu/bloc/manage_unit_bloc/manage_unit_state.dart';
import 'package:syncrow_app/features/menu/view/widgets/manage_home/assign_devices.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';

class RoomsView extends StatelessWidget {
  final SpaceModel unit;
  const RoomsView({super.key, required this.unit});

  @override
  Widget build(BuildContext context) {
    TextEditingController textEditingController = TextEditingController();
    return BlocProvider(
        create: (context) => ManageUnitBloc()..add(FetchRoomsEvent(unit:unit )),
        child: BlocConsumer<ManageUnitBloc, ManageUnitState>(
            listener: (context, state) {},
            builder: (context, state) {
              return DefaultScaffold(
                title: 'Space Management',
                child: state is LoadingState
                    ? const Center(child: RefreshProgressIndicator())
                    : SizedBox(
                        width: MediaQuery.sizeOf(context).width,
                        height: MediaQuery.sizeOf(context).height,
                        child: ListView(
                          children: [
                            const SizedBox(
                              height: 32,
                            ),
                            if (state is FetchRoomsState)
                              Container(
                                decoration: const ShapeDecoration(
                                  color: Colors.white,
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.all(Radius.circular(20)),
                                  ),
                                ),
                                child: Column(
                                  children: [
                                    if (state.roomsList.isNotEmpty)
                                      ListView.separated(
                                        physics: const NeverScrollableScrollPhysics(),
                                        shrinkWrap: true,
                                        separatorBuilder: (context, index) => const Divider(
                                          color: ColorsManager.greyColor,
                                        ),
                                        itemCount: state.roomsList.length,
                                        itemBuilder: (context, index) {
                                          return GestureDetector(
                                            onTap: () {
                                              // BlocProvider.of<ManageUnitBloc>(context).add(
                                              //     FetchDevicesByRoomIdEvent(
                                              //         roomId: state.roomsList[index].id ?? ''));
                                              Navigator.of(context).push(
                                                MaterialPageRoute(
                                                    builder: (context) => AssignDeviceView(
                                                          roomId: state.roomsList[index].id ?? '',
                                                          unitId: unit.id,
                                                          unit: unit,
                                                        )),
                                              );
                                            },
                                            child: Container(
                                              height: 50,
                                              padding: const EdgeInsets.symmetric(
                                                  vertical: 16, horizontal: 25),
                                              child: Text(
                                                state.roomsList[index].name ?? '',
                                                style: const TextStyle(
                                                  color: Color(0xFF5D5D5D),
                                                  fontSize: 15,
                                                  fontWeight: FontWeight.w400,
                                                ),
                                              ),
                                            ),
                                          );
                                        },
                                      ),
                                    const Divider(
                                      color: ColorsManager.greyColor,
                                    ),
                                    Container(
                                      padding:
                                          const EdgeInsets.symmetric(horizontal: 25, vertical: 5),
                                      child: Row(
                                        children: [
                                          TextButton(
                                            onPressed: () {
                                              if (textEditingController.text.isEmpty) {
                                                CustomSnackBar.displaySnackBar(
                                                    'Please add the room name');
                                                return;
                                              }
                                              BlocProvider.of<ManageUnitBloc>(context).add(
                                                  AddNewRoom(
                                                      roomName: textEditingController.text,
                                                      unit: unit));
                                              textEditingController.clear();
                                            },
                                            child: const Row(
                                              mainAxisAlignment: MainAxisAlignment.start,
                                              children: [
                                                BodyMedium(
                                                  text: 'Add Room',
                                                  fontColor: ColorsManager.primaryColor,
                                                ),
                                              ],
                                            ),
                                          ),
                                          Flexible(
                                            child: TextField(
                                              textAlign: TextAlign.end,
                                              controller: textEditingController,
                                              decoration: InputDecoration(
                                                hintText: 'Set',
                                                hintStyle:
                                                    context.bodyMedium.copyWith(color: Colors.grey),
                                                border: InputBorder.none,
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              )
                          ],
                        ),
                      ),
              );
            }));
  }
}
