import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/shared/navigate_home_grid_view.dart';
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_bloc.dart';
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_event.dart';
import 'package:syncrow_web/pages/spaces_management/structure_selector/bloc/center_body_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_event.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_state.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/widgets/loaded_space_widget.dart';
import 'package:syncrow_web/pages/spaces_management/structure_selector/view/center_body_widget.dart';
import 'package:syncrow_web/services/product_api.dart';
import 'package:syncrow_web/services/space_mana_api.dart';
import 'package:syncrow_web/services/space_model_mang_api.dart';
import 'package:syncrow_web/utils/theme/responsive_text_theme.dart';
import 'package:syncrow_web/web_layout/web_scaffold.dart';

class SpaceManagementPage extends StatefulWidget {
  const SpaceManagementPage({super.key});

  @override
  SpaceManagementPageState createState() => SpaceManagementPageState();
}

class SpaceManagementPageState extends State<SpaceManagementPage> {
  final CommunitySpaceManagementApi _api = CommunitySpaceManagementApi();
  final ProductApi _productApi = ProductApi();
  final SpaceModelManagementApi _spaceModelApi = SpaceModelManagementApi();

  @override
  void initState() {
    context.read<SpaceTreeBloc>().add(InitialEvent());
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider(
          create: (context) => SpaceManagementBloc(
            _api,
            _productApi,
            _spaceModelApi,
            BlocProvider.of<SpaceTreeBloc>(context),
          )..add(LoadCommunityAndSpacesEvent(this.context)),
        ),
        BlocProvider(
          create: (context) => CenterBodyBloc(),
        ),
      ],
      child: WebScaffold(
        appBarTitle: Text(
          'Space Management',
          style: ResponsiveTextTheme.of(context).deviceManagementTitle,
        ),
        enableMenuSidebar: false,
        centerBody: CenterBodyWidget(),
        rightBody: const NavigateHomeGridView(),
        scaffoldBody: BlocBuilder<SpaceManagementBloc, SpaceManagementState>(
          builder: (context, state) {
            if (state is SpaceManagementLoading) {
              return const Center(child: CircularProgressIndicator());
            }
            if (state is SpaceManagementInitial) {
              return const Center(child: CircularProgressIndicator());
            } else if (state is BlankState) {
              return LoadedSpaceView(
                communities: state.communities,
                selectedCommunity: null,
                selectedSpace: null,
                products: state.products,
                shouldNavigateToSpaceModelPage: false,
                projectTags: state.allTags,
              );
            } else if (state is SpaceManagementLoaded) {
              return LoadedSpaceView(
                communities: state.communities,
                selectedCommunity: state.selectedCommunity,
                selectedSpace: state.selectedSpace,
                products: state.products,
                spaceModels: state.spaceModels,
                shouldNavigateToSpaceModelPage: false,
                projectTags: state.allTags,
              );
            } else if (state is SpaceModelLoaded) {
              return LoadedSpaceView(
                communities: state.communities,
                products: state.products,
                spaceModels: state.spaceModels,
                shouldNavigateToSpaceModelPage: true,
                projectTags: state.allTags,
              );
            } else if (state is SpaceManagementError) {
              return Center(child: Text('Error: ${state.errorMessage}'));
            }
            return Container();
          },
        ),
      ),
    );
  }
}
