import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/title_medium.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
import 'package:syncrow_app/utils/resource_manager/strings_manager.dart';

class RoomsSlider extends StatelessWidget {
  const RoomsSlider({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<HomeCubit, HomeState>(
      builder: (context, state) {
        return SizedBox(
          height: 40,
          child: PageView(
            controller: HomeCubit.getInstance().roomsPageController,
            onPageChanged: (index) {
              HomeCubit.getInstance().roomSliderPageChanged(index);
            },
            children: [
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 15),
                child: InkWell(
                  onTap: () {
                    HomeCubit.getInstance().unselectRoom();
                  },
                  child: TitleMedium(
                    text: 'All Devices',
                    style: context.titleMedium.copyWith(
                      fontSize: 25,
                      color: ColorsManager.textPrimaryColor,
                    ),
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 15),
                child: InkWell(
                  onTap: () {
                    HomeCubit.getInstance().unselectRoom();
                  },
                  child: TitleMedium(
                    text: StringsManager.wizard,
                    style: context.titleMedium.copyWith(
                      fontSize: 25,
                      color: ColorsManager.textPrimaryColor,
                    ),
                  ),
                ),
              ),
              if (HomeCubit.getInstance().selectedSpace != null)
                ...HomeCubit.getInstance().selectedSpace!.subspaces.map(
                      (room) => InkWell(
                        onTap: () {
                          HomeCubit.getInstance().roomSliderPageChanged(
                              HomeCubit.getInstance()
                                  .selectedSpace!
                                  .subspaces
                                  .indexOf(room));
                        },
                        child: TitleMedium(
                          text: room.name!,
                          style: context.titleMedium.copyWith(
                            fontSize: 25,
                            color: HomeCubit.getInstance().selectedRoom == room
                                ? ColorsManager.textPrimaryColor
                                : ColorsManager.textPrimaryColor
                                    .withOpacity(.2),
                          ),
                        ),
                      ),
                    )
            ],
          ),
        );
      },
    );
  }
}
