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/generated/locale_keys.g.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/widgets/chat_app_bar_widge.dart';
import 'package:provider/provider.dart';

class ManageGroupScreen extends StatefulWidget {
  const ManageGroupScreen({
    Key? key,
  }) : super(key: key);

  @override
  State<ManageGroupScreen> createState() => _ManageGroupScreenState();
}

class _ManageGroupScreenState extends State<ManageGroupScreen> {
  late ChatProviderModel provider;
  GroupResponse? groupDetails;

  @override
  void initState() {
    super.initState();
    provider = Provider.of<ChatProviderModel>(context, listen: false);
  }

  @override
  Widget build(BuildContext context) {
    groupDetails = ModalRoute.of(context)!.settings.arguments as GroupResponse;
    return Scaffold(
        backgroundColor: MyColors.white,
        appBar: ChatAppBarWidget(
          context,
          title: LocaleKeys.manageGroup.tr(),
          showHomeButton: false,
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            LocaleKeys.admin
                .tr()
                .toText14(color: MyColors.darkTextColor)
                .paddingOnly(right: 25)!,
            groupDetails!.groupUserList!.isNotEmpty
                ? ListView.separated(
                    itemCount: groupDetails!.groupUserList!.length,
                    shrinkWrap: true,
                    physics: const ClampingScrollPhysics(),
                    padding: const EdgeInsets.only(bottom: 5.0),
                    itemBuilder: (BuildContext context, int index) {
                      return SizedBox(
                        height: 55,
                        child: Row(
                          children: [
                            Stack(
                              children: <Widget>[
                                if (groupDetails!.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: groupDetails!.groupUserList![index]
                                                  .userStatus ==
                                              1
                                          ? MyColors.green2DColor
                                          : Colors.red,
                                    ),
                                  ).circle(10),
                                )
                              ],
                            ),
                            Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                (groupDetails!
                                            .groupUserList![index].userName! ??
                                        "")
                                    .toText14(color: MyColors.darkTextColor)
                                    .paddingOnly(left: 11, top: 13),
                              ],
                            ).expanded,
                            Row(
                              children: [
                                Switch(
                                  value: groupDetails!
                                      .groupUserList![index].isAdmin!,
                                  onChanged: groupDetails!
                                              .groupUserList![index].id ==
                                          AppState().chatDetails!.response!.id
                                      ? null
                                      : (value) {
                                          setState(() {
                                            groupDetails!.groupUserList![index]
                                                .isAdmin = value;
                                            updateGroupAdmin(
                                                groupDetails!.groupUserList!,
                                                groupDetails!.groupId);
                                          });
                                        },
                                )
                              ],
                            )
                          ],
                        ),
                      );
                    },
                    separatorBuilder: (BuildContext context, int index) =>
                        const Divider(color: MyColors.lightGreyE5Color)
                            .paddingOnly(left: 70),
                  ).paddingAll(10)
                : Column(
                    children: <Widget>[
                      Utils.getNoDataWidget(context).expanded,
                    ],
                  )
          ],
        ));
  }

  void updateGroupAdmin(List<GroupUserList> groupUserList, int? groupId) async {
    //Group id need to be updated..
    provider.updateGroupAdmin(groupId, groupUserList);
  }
}
