import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/chat/get_search_user_chat_model.dart';
import 'package:mohem_flutter_app/models/chat/get_user_groups_by_id.dart';
import 'package:mohem_flutter_app/provider/chat_provider_model.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/ui/chat/chat_detailed_screen.dart';
import 'package:mohem_flutter_app/widgets/chat_app_bar_widge.dart';
import 'package:provider/provider.dart';
class GroupMembersScreen extends StatefulWidget {

  const GroupMembersScreen({Key? key,}) : super(key: key);

  @override
  State<GroupMembersScreen> createState() => _GroupMembersScreenState();
}

class _GroupMembersScreenState extends State<GroupMembersScreen> {
  late ChatProviderModel provider;
   late List<GroupUserList> groupUserList;
  @override
  void initState() {
    super.initState();
    provider = Provider.of<ChatProviderModel>(context, listen: false);

  }

  @override
  Widget build(BuildContext context) {
    groupUserList = ModalRoute.of(context)!.settings.arguments as List<GroupUserList>;
    return Scaffold(
        backgroundColor: MyColors.white,
        appBar: ChatAppBarWidget(
          context,
          title: LocaleKeys.groupMembers.tr(),
          showHomeButton: false,
        ),
        body:
        groupUserList!.isNotEmpty ? ListView.separated(
              itemCount: groupUserList!.length,
              shrinkWrap: true,
              physics: const ClampingScrollPhysics(),
              padding: const EdgeInsets.only(bottom: 5.0),
              itemBuilder: (BuildContext context, int index) {
                if(groupUserList![index].id != AppState().chatDetails!.response!.id) {
                  return SizedBox(
                  height: 55,
                  child: Row(
                    children: [

                      Stack(
                        children: <Widget>[
                          if (groupUserList![index].image == null)
                            SvgPicture.asset(
                              "assets/images/user.svg",
                              height: 48,
                              width: 48,
                            ),
                          Positioned(
                            right: 5,
                            bottom: 1,
                            child: Container(
                              width: 10,
                              height: 10,
                              decoration: BoxDecoration(
                                color:groupUserList![index].userStatus == 1
                                    ? MyColors.green2DColor
                                    : Colors.red,
                              ),
                            ).circle(10),
                          )
                        ],
                      ),
                      Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          (groupUserList![index].userName! ?? "").toText14(
                              color: MyColors.darkTextColor).paddingOnly(
                              left: 11, top: 13),
                        ],
                      ).expanded,


                      Row(
                        children: [
                         IconButton(onPressed: (){
                              goToChat(groupUserList![index]);
                         }, icon: Icon(Icons.chat))
                        ],
                      )
                    ],
                  ),
                );
                } else {
                  return const SizedBox();
                }
              },
              separatorBuilder: (BuildContext context, int index) =>
                  const Divider(color: MyColors.lightGreyE5Color).paddingOnly(
                      left: 70),
            ).paddingAll(10)
                : Column(
              children: <Widget>[
                Utils
                    .getNoDataWidget(context)
                    .expanded,
              ],
            )
           );
  }

  void goToChat(GroupUserList groupUser){

    ChatUser chatUser =  ChatUser.fromJson(groupUser.toJson());
    Navigator.pushNamed(context,
        AppRoutes.chatDetailed,
        arguments:
        ChatDetailedScreenParams(
          chatUser,
            false));
  }
}