import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import '../../../product/constant/enums/app_route_enums.dart';
import 'groups_model.dart';
import '../inter_family_bloc.dart';
import '../inter_family_widget.dart';
import '../../../product/base/bloc/base_bloc.dart';
import '../../../product/constant/app/app_constants.dart';
import '../../../product/constant/icon/icon_constants.dart';
import '../../../product/extention/context_extention.dart';
import '../../../product/services/language_services.dart';

import 'groups_widget.dart';

class GroupsScreen extends StatefulWidget {
  const GroupsScreen({super.key, required this.role});
  final String role;
  @override
  State<GroupsScreen> createState() => _GroupsScreenState();
}

class _GroupsScreenState extends State<GroupsScreen> {
  late InterFamilyBloc interFamilyBloc;

  @override
  void initState() {
    super.initState();
    interFamilyBloc = BlocProvider.of(context);
    // interFamilyBloc.getAllGroup(widget.role);
  }

  @override
  Widget build(BuildContext context) {
    if (widget.role == ApplicationConstants.OWNER_GROUP ||
        widget.role == ApplicationConstants.PARTICIPANT_GROUP) {
      interFamilyBloc.getAllGroup(widget.role);
      return StreamBuilder<List<Group>>(
          stream: interFamilyBloc.streamCurrentGroups,
          builder: (context, groupsSnapshot) {
            return Scaffold(
              body: groupsSnapshot.data?.isEmpty ?? true
                  ? const Center(
                      child: CircularProgressIndicator(),
                    )
                  : ListView.builder(
                      itemCount: groupsSnapshot.data!.length,
                      itemBuilder: (context, index) {
                        return ListTile(
                          onTap: () {
                            context.pushNamed(AppRoutes.GROUP_DETAIL.name,
                                pathParameters: {
                                  "groupId": groupsSnapshot.data![index].id!
                                },
                                extra: widget.role);
                          },
                          leading: IconConstants.instance
                              .getMaterialIcon(Icons.diversity_2),
                          title: Text(
                            groupsSnapshot.data![index].name ?? '',
                            style: const TextStyle(fontWeight: FontWeight.bold),
                          ),
                          subtitle: Text(
                              groupsSnapshot.data![index].description ?? ""),
                          trailing:
                              widget.role == ApplicationConstants.OWNER_GROUP
                                  ? PopupMenuButton(
                                      shape: const RoundedRectangleBorder(
                                        borderRadius: BorderRadius.only(
                                          bottomLeft: Radius.circular(8.0),
                                          bottomRight: Radius.circular(8.0),
                                          topLeft: Radius.circular(8.0),
                                          topRight: Radius.circular(8.0),
                                        ),
                                      ),
                                      itemBuilder: (ctx) => [
                                        _buildPopupMenuItem(
                                            groupsSnapshot.data![index],
                                            context,
                                            appLocalization(context)
                                                .share_group_title,
                                            Icons.share,
                                            4),
                                        _buildPopupMenuItem(
                                            groupsSnapshot.data![index],
                                            context,
                                            appLocalization(context)
                                                .change_group_infomation_title,
                                            Icons.settings_backup_restore,
                                            2),
                                        _buildPopupMenuItem(
                                            groupsSnapshot.data![index],
                                            context,
                                            appLocalization(context)
                                                .delete_group_title,
                                            Icons.delete_forever_rounded,
                                            3),
                                      ],
                                      icon: const Icon(Icons.more_horiz),
                                    )
                                  : const SizedBox.shrink(),
                        );
                      },
                    ),
            );
          });
    } else {
      return const SizedBox.shrink();
    }
  }

  PopupMenuItem _buildPopupMenuItem(Group group, BuildContext context,
      String title, IconData iconData, int position) {
    return PopupMenuItem(
      onTap: () {
        if (title == appLocalization(context).share_group_title) {
          Future.delayed(context.lowDuration, () {
            shareGroup(context, group);
          });
        } else if (title ==
            appLocalization(context).change_group_infomation_title) {
          Future.delayed(context.lowDuration, () {
            createOrJoinGroupDialog(
                context,
                interFamilyBloc,
                widget.role,
                appLocalization(context).change_group_infomation_content,
                appLocalization(context).group_name_title,
                group.name!,
                false,
                group.id!,
                appLocalization(context).description_group,
                group.description ?? "");
          });
        } else if (title == appLocalization(context).delete_group_title) {
          Future.delayed(context.lowDuration, () {
            showActionDialog(
                context,
                widget.role,
                interFamilyBloc,
                appLocalization(context).delete_group_title,
                appLocalization(context).delete_group_content,
                group);
          });
        } else {}
      },
      value: position,
      child: Row(
        // mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Icon(
            iconData,
            color: Colors.black,
          ),
          const SizedBox(width: 10),
          Text(title),
        ],
      ),
    );
  }
}
