import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/communities_tree_selection_bloc/communities_tree_selection_bloc.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/widgets/community_tile.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/widgets/space_management_communities_tree_space_tile.dart';

class SpaceManagementCommunitiesTreeCommunityTile extends StatelessWidget {
  const SpaceManagementCommunitiesTreeCommunityTile({
    required this.community,
    super.key,
  });

  final CommunityModel community;

  @override
  Widget build(BuildContext context) {
    final spaces = community.spaces
        .map(
          (space) => SpaceManagementCommunitiesTreeSpaceTile(
            space: space,
            community: community,
          ),
        )
        .toList();
    return CommunityTile(
      title: community.name,
      key: ValueKey(community.uuid),
      isSelected: context
              .watch<CommunitiesTreeSelectionBloc>()
              .state
              .selectedCommunity
              ?.uuid ==
          community.uuid,
      isExpanded: false,
      onItemSelected: () {
        context.read<CommunitiesTreeSelectionBloc>().add(
              SelectCommunityEvent(community: community),
            );
      },
      onExpansionChanged: (title, expanded) {},
      children: spaces,
    );
  }
}
