import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/app_layout/model/community_model.dart';
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
import 'package:syncrow_app/features/scene/bloc/scene_bloc/scene_bloc.dart';
import 'package:syncrow_app/features/scene/bloc/scene_bloc/scene_event.dart';

abstract final class SceneBlocFactory {
  static SceneBloc create({
    required bool pageType,
    required HomeCubit homeCubit,
  }) {
    final selectedSpace = homeCubit.selectedSpace;
    final defaultSpace = SpaceModel(
      id: '-1',
      name: '',
      community: Community(
        uuid: '-1',
        name: '',
      ),
    );

    final spaceId = selectedSpace?.id ?? defaultSpace.id;
    final space = selectedSpace ?? defaultSpace;
    final communityUuid =
        selectedSpace?.community.uuid ?? defaultSpace.community.uuid;

    final sceneBloc = SceneBloc();

    sceneBloc.add(
      LoadScenes(
        spaceId,
        space,
        showInDevice: pageType,
      ),
    );

    if (!pageType) {
      sceneBloc.add(
        LoadAutomation(
          spaceId,
          communityUuid,
        ),
      );
    }

    return sceneBloc;
  }
}
