import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';

// import 'package:url_launcher/url_launcher.dart';

import '../../../../core/constants/app_assets.dart';
import '../../../../core/constants/app_contants.dart';
import '../../../../core/constants/translation_keys.dart';
import '../../../../core/utils/SessionCache.dart';
import '../../../../custom_widgets/custom_dialog.dart';
import '../../../../custom_widgets/custom_label.dart';
import '../../../../custom_widgets/custom_toasty.dart';
import '../../../../custom_widgets/responsive_widget.dart';
import '../../../../res/app_colors.dart';
import '../../../../routes/app_pages.dart';
import '../../../user_profile/controllers/user_profile_controller.dart';
import '../../controllers/dashboard_screen_controller.dart';

class DrawerData extends GetView<DashboardScreenController> {
  final Function()? toggleAnimation;

   DrawerData({super.key, this.toggleAnimation});
   UserProfileController userProfileController = Get.put(UserProfileController());

  @override
  Widget build(BuildContext context) {
    List<DrawerList> drawerList = DrawerList.getItemsList(context);
    return SingleChildScrollView(
      child: Padding(
        padding: const EdgeInsets.all(25),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                InkWell(
                  onTap: () {
                    // dp("Profile Pictures========", profilePic);
                  },
                  child: Obx(() {
                    return CircleAvatar(
                      radius: 30,
                      backgroundColor: AppColors.colorPrimary,
                      child: Padding(
                        padding: const EdgeInsets.all(8), // Border radius
                        child: userProfileController.profilePic.isNotEmpty
                            ? ClipOval(
                          child: Image.memory(
                            base64Decode(userProfileController.profilePic.value),
                            height: 40,
                            width: 40,
                            fit: BoxFit.cover,
                          ),
                        )
                            : const Icon(Icons.person, color: AppColors.white),
                      ),
                    );
                  }),
                ),
                const SizedBox(width: 15),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Text(SessionCache.instance.userInfo.name, style: Theme.of(context).textTheme.titleLarge),
                      const SizedBox(height: 2),
                      Text(SessionCache.instance.userInfo.cmpCustlastlogin != null ? SessionCache.instance.userInfo.cmpCustlastlogin : "N/A",
                          /*TranslationKeys.makeTranslation(
                              TranslationKeys.textAccountSession),*/
                          overflow: TextOverflow.ellipsis,
                          style: Theme.of(context).textTheme.bodyMedium),
                    ],
                  ),
                ),
              ],
            ),
            const SizedBox(height: 20),
            ListView.builder(
                itemCount: drawerList.length,
                physics: const NeverScrollableScrollPhysics(),
                shrinkWrap: true,
                itemBuilder: (ctx, index) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Padding(
                        padding: const EdgeInsets.symmetric(vertical: 10),
                        child: InkWell(onTap: () {}, child: Text(drawerList[index].label, style: Theme.of(context).textTheme.titleSmall?.copyWith(fontSize: 14, fontWeight: FontWeight.w600))),
                      ),
                      ListView.builder(
                          physics: const NeverScrollableScrollPhysics(),
                          itemCount: drawerList[index].drawerList.length,
                          shrinkWrap: true,
                          itemBuilder: (ctx, innerIndex) {
                            return Padding(padding: const EdgeInsets.symmetric(vertical: 0), child: getDrawerWidget(drawerList[index].drawerList[innerIndex], context));
                          }),
                      const SizedBox(height: 10)
                    ],
                  );
                }),
            const SizedBox(height: 20),
          ],
        ),
      ),
    );
  }

  Widget getDrawerWidget(DrawerItem item, BuildContext context) {
    return InkWell(
      onTap: () {
        switch (item.id) {
          // case AppConstants.ID_MY_ACCOUNT:
          //   Get.toNamed(Routes.MY_ACCOUNT);
          //   break;
          case AppConstants.ID_BILL_MANAGEMENT:
            Get.toNamed(Routes.BILL_MANAGEMENT);
            break;
          case AppConstants.ID_CHEQUE_MANAGEMENT:
            Get.toNamed(Routes.CHEQUE_MANAGEMENT);
            break;
          case AppConstants.ID_INTERNAL_TRANSACTION:
            Get.toNamed(Routes.INTERNAL_ACCOUNT_TRANS);
            break;

          case AppConstants.ID_BENEFICIARY_MANAGEMENT:
            Get.toNamed(Routes.BENEFICIARY_MANAGEMENT);
            break;
          case AppConstants.ID_AGENT_LOCATION:
            if (Platform.isIOS) {
              Toasty.error("Coming soon");
            } else {
              Get.toNamed(Routes.AGENT_LOCATION);
            }

            break;
          case AppConstants.ID_ACCOUNT_STATEMENT:
            Get.toNamed(Routes.ACCOUNT_STATEMENT);
            break;
          case AppConstants.ID_SUPPORT:
            showCutomBottomSheet(supportBottomSheet(context));
            break;
          case AppConstants.ID_USER_PROFILE:
            Get.toNamed(Routes.USER_PROFILE);
            break;
          case AppConstants.ID_PRIVACY_POLICY:
            Get.toNamed(Routes.PRIVACY_POLICY);
            break;
          case AppConstants.ID_USER_ACTIVITY:
            Get.toNamed(Routes.USER_ACTIVITY);
            break;
          case AppConstants.ID_APP_SETTING:
            Get.toNamed(Routes.APP_SETTING);
            break;
          case AppConstants.ID_ABOUT_US:
            Get.toNamed(Routes.ABOUT_US);
            break;
          case AppConstants.ID_CAREER:
            Get.toNamed(Routes.CAREER);
            break;
          case AppConstants.ID_LOGOUT:
            {
              CustomDialog.showLogoutDialog(
                context: context,
                onTapPositive: () {
                  Navigator.of(context, rootNavigator: true).pop();
                  Get.offAllNamed(Routes.LOGIN_SCREEN);
                },
                onTapNegative: () {
                  Navigator.of(context, rootNavigator: true).pop();
                },
              );
            }
            break;
          default:
            print("Print thisi is isiisisisisisisis");
        }
      },
      child: Container(
        padding: const EdgeInsets.all(8),
        child: Row(
          children: [
            SvgPicture.asset(
              item.icon,
              height: 20,
              width: 20,
              color: AppColors.colorPrimary,
            ),
            const SizedBox(width: 15),
            Expanded(child: Text(item.title, style: Theme.of(context).textTheme.titleSmall?.copyWith(fontSize: 15, fontWeight: FontWeight.w400))),
          ],
        ),
      ),
    );
  }

  /// Drawer Support Bottom Sheet Widget

  Container supportBottomSheet(BuildContext context) {
    List<SupportBottomSheet> supportBottomSheetList = <SupportBottomSheet>[
      SupportBottomSheet(
          image: AppAssets.ic_email,
          title: TranslationKeys.makeTranslation(TranslationKeys.textEmail),
          onTap: () {
            // launch("mailto:info@mfsys.com.pk");
          }),
      SupportBottomSheet(
          image: AppAssets.ic_phone,
          title: TranslationKeys.makeTranslation(TranslationKeys.textPhone),
          onTap: () async {
            // launch("tel:021213123123");
          }),
    ];
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(color: Colors.grey, width: 1),
        borderRadius: const BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10)),
      ),
      margin: const EdgeInsets.symmetric(horizontal: 20),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 15),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  TranslationKeys.makeTranslation(TranslationKeys.textGetInTouch),
                  style: Theme.of(context).textTheme.titleSmall,
                ),
                InkWell(
                  onTap: () {
                    Navigator.pop(context);
                  },
                  child: SvgPicture.asset(AppAssets.ic_cross, height: 15, width: 15),
                ),
              ],
            ),
          ),
          Container(
            padding: const EdgeInsets.all(10),
            child: GridView.builder(
              itemCount: supportBottomSheetList.length,
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                crossAxisSpacing: 15,
                mainAxisSpacing: 15,
              ),
              itemBuilder: (context, index) {
                return InkWell(
                  customBorder: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10),
                  ),
                  onTap: supportBottomSheetList[index].onTap,
                  child: Container(

                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      border: Border.all(color: AppColors.colorText, width: 1),
                      borderRadius: const BorderRadius.all(Radius.circular(10)),
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        SvgPicture.asset("${supportBottomSheetList[index].image}", height: 50, width: 50),
                        const SizedBox(height: 20),
                        Center(
                          child: LabelText('${supportBottomSheetList[index].title}', style: const TextStyle(fontWeight: FontWeight.w600, fontSize: 14)),
                        ),
                      ],
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}

class SupportBottomSheet {
  String? image;
  String? title;
  Function()? onTap;

  SupportBottomSheet({this.image, this.title, this.onTap});
}

class DrawerList {
  final String label;
  final List<DrawerItem> drawerList;

  DrawerList(this.label, this.drawerList);

  static List<DrawerList> getItemsList(BuildContext context) {
    return [
      DrawerList(TranslationKeys.makeTranslation(TranslationKeys.textManage), [
        // DrawerItem(
        //     AppConstants.ID_MY_ACCOUNT,
        //     AppAssets.ic_user,
        //     TranslationKeys.makeTranslation(TranslationKeys.textMyAccount),
        //     () => Routes.USER_ACTIVITY),
        DrawerItem(AppConstants.ID_BILL_MANAGEMENT, AppAssets.ic_bill, TranslationKeys.makeTranslation(TranslationKeys.textBillManagement), () => Routes.BILL_MANAGEMENT),
        DrawerItem(AppConstants.ID_CHEQUE_MANAGEMENT, AppAssets.ic_cheque_management, TranslationKeys.makeTranslation(TranslationKeys.textChequeManagement), () => Routes.CHEQUE_MANAGEMENT),
        DrawerItem(AppConstants.ID_INTERNAL_TRANSACTION, AppAssets.ic_u_cash, TranslationKeys.makeTranslation(TranslationKeys.textInternalAccountTransaction), () => Routes.U_SEND),
        DrawerItem(AppConstants.ID_BENEFICIARY_MANAGEMENT, AppAssets.ic_u_recive, TranslationKeys.makeTranslation(TranslationKeys.textBeneficiaryManagement), () => Routes.U_RECEIVED),
        DrawerItem(AppConstants.ID_AGENT_LOCATION, AppAssets.ic_location, TranslationKeys.makeTranslation(TranslationKeys.textAgentLocation), () => Routes.AGENT_LOCATION),
        DrawerItem(AppConstants.ID_ACCOUNT_STATEMENT, AppAssets.ic_money_management, TranslationKeys.makeTranslation(TranslationKeys.textAccountStatement), () => Routes.ACCOUNT_STATEMENT),
      ]),
      DrawerList(TranslationKeys.makeTranslation(TranslationKeys.textSupport), [
        DrawerItem(AppConstants.ID_SUPPORT, AppAssets.ic_customer_service, TranslationKeys.makeTranslation(TranslationKeys.textSupport), () {}),
        DrawerItem(AppConstants.ID_USER_PROFILE, AppAssets.ic_user, TranslationKeys.makeTranslation(TranslationKeys.textUserProfile), () => Routes.USER_PROFILE),
        DrawerItem(AppConstants.ID_USER_ACTIVITY, AppAssets.ic_activity, TranslationKeys.makeTranslation(TranslationKeys.textUserActivity), () => Routes.USER_ACTIVITY),
        DrawerItem(AppConstants.ID_ABOUT_US, AppAssets.ic_user, TranslationKeys.makeTranslation(TranslationKeys.textAboutUs), () => Routes.ABOUT_US),
        DrawerItem(AppConstants.ID_APP_SETTING, AppAssets.ic_setting, TranslationKeys.makeTranslation(TranslationKeys.textAppSetting), () => Routes.APP_SETTING),
        DrawerItem(AppConstants.ID_CAREER, AppAssets.ic_sub_accounts, TranslationKeys.makeTranslation(TranslationKeys.textCareer), () => Routes.CAREER),
      ]),
      DrawerList(TranslationKeys.makeTranslation(TranslationKeys.textLogOut), [
        DrawerItem(
          AppConstants.ID_LOGOUT,
          AppAssets.ic_logout,
          TranslationKeys.makeTranslation(TranslationKeys.textLogout),
          () {},
        ),
      ]),
    ];
  }
}

class DrawerItem {
  final String id;
  final String icon;
  final String title;
  Function()? onTap;

  DrawerItem(this.id, this.icon, this.title, this.onTap);
}
