import 'package:flutter/material.dart';

import 'groups/groups_screen.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';

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

  @override
  State<InterFamilyScreen> createState() => _InterFamilyScreenState();
}

class _InterFamilyScreenState extends State<InterFamilyScreen> {
  late InterFamilyBloc interFamilyBloc;
  String title = "";
  int _selectedIndex = 0;
  bool isLoading = false;
  @override
  void initState() {
    super.initState();
    interFamilyBloc = BlocProvider.of(context);
  }

  final _widgetOptions = <Widget>[
    BlocProvider(
      blocBuilder: () => InterFamilyBloc(),
      child: const GroupsScreen(
        role: ApplicationConstants.OWNER_GROUP,
      ),
    ),
    BlocProvider(
      blocBuilder: () => InterFamilyBloc(),
      child: const GroupsScreen(
        role: ApplicationConstants.PARTICIPANT_GROUP,
      ),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    checkTitle(_selectedIndex);
    return StreamBuilder<int>(
      stream: interFamilyBloc.streamSelectedScreen,
      initialData: _selectedIndex,
      builder: (context, selectSnapshot) {
        return Scaffold(
          appBar: AppBar(
            actions: [
              ElevatedButton(
                onPressed: () {
                  if (selectSnapshot.data == 0) {
                    createOrJoinGroupDialog(
                        context,
                        interFamilyBloc,
                        selectSnapshot.data! == 0
                            ? ApplicationConstants.OWNER_GROUP
                            : ApplicationConstants.PARTICIPANT_GROUP,
                        appLocalization(context).add_new_group,
                        appLocalization(context).group_name_title,
                        "",
                        false,
                        "",
                        "",
                        "");
                  } else {
                    createOrJoinGroupDialog(
                        context,
                        interFamilyBloc,
                        selectSnapshot.data! == 0
                            ? ApplicationConstants.OWNER_GROUP
                            : ApplicationConstants.PARTICIPANT_GROUP,
                        appLocalization(context).join_group,
                        appLocalization(context).group_id_title,
                        '',
                        true,
                        "",
                        appLocalization(context).group_name_title,
                        "");
                  }
                },
                style: ElevatedButton.styleFrom(
                  shape: const CircleBorder(),
                ),
                child: IconConstants.instance.getMaterialIcon(Icons.add),
              ),
            ],
            leading: Builder(
              builder: (context) {
                return IconButton(
                  onPressed: () {
                    Scaffold.of(context).openDrawer();
                  },
                  icon: const Icon(Icons.menu),
                );
              },
            ),
            title: StreamBuilder<String>(
              stream: interFamilyBloc.streamTitleScreen,
              initialData: title,
              builder: (context, titleSnapshot) {
                return Center(
                  child: Text(titleSnapshot.data ?? title),
                );
              },
            ),
          ),
          drawer: Drawer(
            width: context.dynamicWidth(0.4),
            child: ListView(
              padding: EdgeInsets.zero,
              children: [
                ListTile(
                  title: Text(appLocalization(context).my_group_title),
                  selected: _selectedIndex == 0,
                  onTap: () {
                    _onItemTapped(0);
                    title = appLocalization(context).my_group_title;
                    interFamilyBloc.sinkTitleScreen.add(title);
                    Navigator.pop(context);
                  },
                ),
                ListTile(
                  title: Text(appLocalization(context).invite_group),
                  selected: _selectedIndex == 1,
                  onTap: () {
                    _onItemTapped(1);
                    title = appLocalization(context).invite_group;
                    interFamilyBloc.sinkTitleScreen.add(title);
                    Navigator.pop(context);
                  },
                ),
              ],
            ),
          ),
          body: _widgetOptions[selectSnapshot.data ?? _selectedIndex],
        );
      },
    );
  }

  void checkTitle(int index) {
    if (index == 0) {
      title = appLocalization(context).my_group_title;
      interFamilyBloc.sinkTitleScreen.add(title);
    } else {
      title = appLocalization(context).invite_group;
      interFamilyBloc.sinkTitleScreen.add(title);
    }
  }

  void _onItemTapped(int index) {
    _selectedIndex = index;
    interFamilyBloc.sinkSelectedScreen.add(_selectedIndex);
    isLoading = false;
    interFamilyBloc.sinkIsLoading.add(isLoading);
  }
}
