import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/profile_api_client.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/get_employee_contacts.model.dart';
import 'package:mohem_flutter_app/ui/profile/dynamic_screens/dynamic_input_familyMembers_screen.dart';
import 'package:mohem_flutter_app/ui/screens/profile/profile_screen.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:mohem_flutter_app/widgets/button/default_button.dart';

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

  @override
  _FamilyMembersState createState() => _FamilyMembersState();
}

class _FamilyMembersState extends State<FamilyMembers> {
  List<GetEmployeeContactsList> getEmployeeContactsList = [];

  int? relationId;

  @override
  void initState() {
    super.initState();
    getEmployeeContacts();
  }

  void getEmployeeContacts() async {
    try {
      Utils.showLoading(context);
      getEmployeeContactsList = await ProfileApiClient().getEmployeeContacts();
      Utils.hideLoading(context);
      setState(() {});
    } catch (ex) {
      Utils.hideLoading(context);
      Utils.handleException(ex, context, null);
    }
  }

  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBarWidget(
          context,
          title: LocaleKeys.profile_familyDetails.tr(),
        ),
        backgroundColor: MyColors.backgroundColor,
        bottomSheet: footer(),
        body: Container(
          width: MediaQuery.of(context).size.width,
          child: getEmployeeContactsList.length != 0
              ? SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  child: Column(
                    children: <Widget>[
                      ListView.builder(
                          scrollDirection: Axis.vertical,
                          shrinkWrap: true,
                          physics: ScrollPhysics(),
                          itemCount: getEmployeeContactsList.length,
                          itemBuilder: (context, index) {
                            return Container(
                                child: Column(
                              children: [
                                Container(
                                  width: double.infinity,
                                  margin: EdgeInsets.only(
                                    top: 20,
                                    left: 21,
                                    right: 21,
                                  ),
                                  padding: EdgeInsets.only(
                                    left: 14,
                                    right: 14,
                                    top: 13,
                                  ),
                                  height: 110,
                                  decoration: BoxDecoration(
                                    boxShadow: [
                                      BoxShadow(
                                        color: Colors.grey.withOpacity(0.5),
                                        spreadRadius: 5,
                                        blurRadius: 26,
                                        offset: Offset(0, 3),
                                      ),
                                    ],
                                    color: Colors.white,
                                    borderRadius: BorderRadius.circular(10.0),
                                  ),
                                  child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                                    "${getEmployeeContactsList[index].cONTACTNAME}".toText16(color: MyColors.blackColor),
                                    "${getEmployeeContactsList[index].rELATIONSHIP}".toText11(isBold: true, color: MyColors.textMixColor),
                                    SizedBox(
                                      height: 5,
                                    ),
                                    Divider(
                                      color: MyColors.lightGreyEFColor,
                                      height: 20,
                                      thickness: 1,
                                      indent: 0,
                                      endIndent: 0,
                                    ),
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                      children: <Widget>[
                                        Container(
                                            child: InkWell(
                                          onTap: () {
                                            showUpdateAlertDialog(context);
                                          },
                                          child: RichText(
                                            text: TextSpan(
                                              children: [
                                                WidgetSpan(
                                                  child: Icon(
                                                    Icons.edit,
                                                    size: 15,
                                                    color: MyColors.grey67Color,
                                                  ),
                                                ),
                                                TextSpan(
                                                  text: LocaleKeys.update.tr(),
                                                  style: TextStyle(
                                                    color: MyColors.grey67Color,
                                                    fontSize: 12,
                                                    fontWeight: FontWeight.bold,
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),
                                        )),
                                        Padding(
                                          padding: const EdgeInsets.symmetric(horizontal: 8),
                                          child: SizedBox(
                                            child: Container(
                                              width: 3,
                                              color: MyColors.lightGreyEFColor,
                                            ),
                                          ),
                                        ),
                                        Container(
                                            child: InkWell(
                                          onTap: () {
                                            showRemoveAlertDialog(context, getEmployeeContactsList[index]!.cONTACTRELATIONSHIPID!.toInt());
                                          },
                                          child: RichText(
                                            text: TextSpan(
                                              children: [
                                                WidgetSpan(
                                                  child: Icon(
                                                    Icons.delete,
                                                    size: 15,
                                                    color: Color(0x99FF0000),
                                                  ),
                                                ),
                                                TextSpan(
                                                  text: LocaleKeys.remove.tr(),
                                                  style: TextStyle(
                                                    color: MyColors.DarkRedColor,
                                                    fontSize: 12,
                                                    fontWeight: FontWeight.bold,
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),
                                        )),
                                        // ElevatedButton.icon(
                                        //   icon: Icon(
                                        //     Icons.delete,
                                        //     size: 15,
                                        //     color: Color(0x99FF0000),
                                        //   ),
                                        //   style: ElevatedButton.styleFrom(
                                        //     shadowColor: Colors.white,
                                        //     primary: Colors.white,
                                        //   ),
                                        //   label:  "remove".toText12(color: MyColors.DarkRedColor),
                                        //   onPressed: (){},
                                        // ),
                                      ],
                                    ),
                                  ]),
                                ),
                              ],
                            ));
                          })
                    ],
                  ),
                )
              : Container(),
          // SizedBox(height: 20),
        ));
  }

  Widget footer() {
    return Container(
      decoration: BoxDecoration(
        // borderRadius: BorderRadius.circular(10),
        color: MyColors.white,
        boxShadow: [
          BoxShadow(color: MyColors.lightGreyEFColor, spreadRadius: 3),
        ],
      ),
      child: DefaultButton("Add New Family Member", () async {
        //     context.setLocale(const Locale("en", "US")); // to change Loacle
        ProfileScreen();
      }).insideContainer,
    );
  }

  void showUpdateAlertDialog(BuildContext context) {
    Widget cancelButton = TextButton(
      child: Text(
        LocaleKeys.cancel.tr(),
      ),
      onPressed: () {
        Navigator.pop(context);
      },
    );
    Widget continueButton = TextButton(
      child: Text(
        LocaleKeys.ok.tr(),
      ),
      onPressed: () {
        continueDynamicForms();
      },
    );
    AlertDialog alert = AlertDialog(
      title: Text(
        LocaleKeys.confirm.tr(),
      ),
      content: Text("Are You Sure You Want to Update this Member?"),
      actions: [
        cancelButton,
        continueButton,
      ],
    );
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return alert;
      },
    );
  }

  void showRemoveAlertDialog(BuildContext context, int relationId) {
    Widget cancelButton = TextButton(
      child: Text(
        LocaleKeys.cancel.tr(),
      ),
      onPressed: () {
        Navigator.pop(context);
      },
    );
    Widget continueButton = TextButton(
      child: Text(
        LocaleKeys.ok.tr(),
      ),
      onPressed: () {
        Navigator.pushNamed(context, AppRoutes.deleteFamilyMember);
        //  continueDynamicForms();
      },
    );
    AlertDialog alert = AlertDialog(
      title: Text(
        LocaleKeys.confirm.tr(),
      ),
      content: Text("Are You Sure You Want to Remove this Member?"),
      actions: [
        cancelButton,
        continueButton,
      ],
    );
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return alert;
      },
    );
  }

  void continueDynamicForms() {
    Navigator.pushNamed(context, AppRoutes.addDynamicInputProfile, arguments: DynamicFamilyMembersParams(LocaleKeys.profile_familyDetails.tr(), getEmployeeContactsList: getEmployeeContactsList));
  }
}
