// ignore_for_file: deprecated_member_use_from_same_package, use_build_context_synchronously

import 'dart:async';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:hoshan/core/gen/assets.gen.dart';
import 'package:hoshan/core/routes/route_generator.dart';
import 'package:hoshan/core/services/api/dio_service.dart';
import 'package:hoshan/ui/screens/setting/cubit/ad_remaining_cubit.dart';
import 'package:hoshan/ui/screens/splash/cubit/user_info_cubit.dart';
import 'package:hoshan/ui/theme/colors.dart';
import 'package:hoshan/ui/theme/cubit/theme_mode_cubit.dart';
import 'package:hoshan/ui/theme/responsive.dart';
import 'package:hoshan/ui/theme/text.dart';
import 'package:hoshan/ui/widgets/components/animations/animated_visibility.dart';
import 'package:hoshan/ui/widgets/components/dialog/dialog_handler.dart';
import 'package:hoshan/ui/widgets/components/dropdown/more_popup_menu.dart';
import 'package:hoshan/ui/widgets/components/image/network_image.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:persian_number_utility/persian_number_utility.dart';
import 'package:share_plus/share_plus.dart';
import 'package:url_launcher/url_launcher.dart';

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

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  final ValueNotifier<bool> isDark = ValueNotifier(false);
  final ValueNotifier<bool> showCredit = ValueNotifier(false);
  final GlobalKey containerKey = GlobalKey();

  Timer? timer;
  late final ValueNotifier<int> countdown = ValueNotifier(3600);
  late final ValueNotifier<int> maxSeconds = ValueNotifier(3600);
  late final ValueNotifier<int> coinsForAd = ValueNotifier(0);
  late final ValueNotifier<bool> enableAd = ValueNotifier(true);
  late final ValueNotifier<bool> loadingAdShow = ValueNotifier(false);

  void startCountdown() {
    timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      if (countdown.value > 0) {
        countdown.value -= 1;
      } else {
        timer.cancel();
      }
    });
  }

  @override
  void dispose() {
    timer?.cancel();
    countdown.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: ReversibleAppbar(
      //   context,
      //   title: Row(
      //     mainAxisAlignment: MainAxisAlignment.end,
      //     children: [
      //       Text(
      //         'تنظیمات',
      //         style: AppTextStyles.body3
      //             .copyWith(color: Theme.of(context).colorScheme.onSurface),
      //       ),
      //       const SizedBox(
      //         width: 8,
      //       ),
      //       Assets.icon.outline.setting.svg(
      //           width: Responsive(context).isMobile() ? null : 32,
      //           color: Theme.of(context).colorScheme.onSurface),
      //     ],
      //   ),
      // ),
      // persistentFooterAlignment: AlignmentDirectional.center,
      body: Responsive(context).maxWidthInDesktop(
        child: (contxet, maxWidth) => RefreshIndicator(
          onRefresh: () async {
            context.read<AdRemainingCubit>().getRemainingAd();
          },
          child: SingleChildScrollView(
            physics: const BouncingScrollPhysics(),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                BlocBuilder<UserInfoCubit, UserInfoState>(
                  builder: (context, state) {
                    return Container(
                      margin: const EdgeInsets.fromLTRB(16, 32, 16, 0),
                      padding: const EdgeInsets.all(16),
                      decoration: BoxDecoration(
                          color: Theme.of(context).colorScheme.surface,
                          borderRadius: BorderRadius.circular(18),
                          border:
                              Border.all(color: AppColors.gray.defaultShade)),
                      child: Directionality(
                        textDirection: TextDirection.rtl,
                        child: Row(
                          children: [
                            Container(
                              width: 78,
                              height: 78,
                              decoration: BoxDecoration(
                                  color: context.read<ThemeModeCubit>().state ==
                                          ThemeMode.dark
                                      ? Theme.of(context).colorScheme.onSurface
                                      : AppColors.gray[300],
                                  shape: BoxShape.circle,
                                  boxShadow: [
                                    BoxShadow(
                                        blurRadius: 6,
                                        color: const Color(0xff4d4d4d)
                                            .withValues(alpha: 0.4))
                                  ]),
                              child: ClipOval(
                                child: UserInfoCubit.userInfoModel.image != null
                                    ? ImageNetwork(
                                        url: DioService.baseURL +
                                            UserInfoCubit.userInfoModel.image!,
                                        showHero: true,
                                      )
                                    : Padding(
                                        padding: const EdgeInsets.all(12),
                                        child:
                                            Assets.icon.outline.profile.svg(),
                                      ),
                              ),
                            ),
                            const SizedBox(
                              width: 12,
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                if (UserInfoCubit.userInfoModel.username !=
                                    null)
                                  Text(
                                    UserInfoCubit.userInfoModel.username!,
                                    style: AppTextStyles.body4.copyWith(
                                        color: Theme.of(context)
                                            .colorScheme
                                            .onSurface),
                                  ),
                                if (UserInfoCubit.userInfoModel.mobileNumber !=
                                    null)
                                  Text(
                                    UserInfoCubit.userInfoModel.mobileNumber!,
                                    style: AppTextStyles.body4.copyWith(
                                        color: Theme.of(context)
                                            .colorScheme
                                            .onSurface),
                                  ),
                                const SizedBox(
                                  height: 8,
                                ),
                                GestureDetector(
                                  onTap: () {
                                    context.go(Routes.editProfile);
                                  },
                                  child: Row(
                                    children: [
                                      Icon(
                                        Icons.edit_outlined,
                                        color:
                                            AppColors.primaryColor.defaultShade,
                                        size: 18,
                                      ),
                                      Text(
                                        'ویرایش',
                                        style: AppTextStyles.body5.copyWith(
                                            color: Theme.of(context)
                                                .colorScheme
                                                .primary,
                                            fontWeight: FontWeight.bold),
                                      ),
                                    ],
                                  ),
                                )
                              ],
                            ),
                          ],
                        ),
                      ),
                    );
                  },
                ),
                const SizedBox(
                  height: 12,
                ),
                // BlocConsumer<AdRemainingCubit, AdRemainingState>(
                //   listener: (context, state) {
                //     if (state is AdRemainingSuccess) {
                //       maxSeconds.value =
                //           int.tryParse(state.data['ads_cooldown'] ?? '0') ?? 0;
                //       countdown.value = int.tryParse(
                //               state.data['remaining_seconds'] ?? '0') ??
                //           0;
                //       coinsForAd.value =
                //           int.tryParse(state.data['coins_per_video'] ?? '0') ??
                //               0;
                //       enableAd.value = state.data['ads_enabled'] == 'true';
                //       startCountdown();
                //     }
                //   },
                //   builder: (context, state) {
                //     if (state is AdRemainingFail) {
                //       return const SizedBox.shrink();
                //     }
                //     return DefaultPlaceHolder(
                //       enabled: state is AdRemainingLoading,
                //       child: Container(
                //         margin: const EdgeInsets.fromLTRB(16, 0, 16, 0),
                //         padding: const EdgeInsets.all(16),
                //         decoration: BoxDecoration(
                //             color: Theme.of(context).colorScheme.surface,
                //             borderRadius: BorderRadius.circular(18),
                //             border:
                //                 Border.all(color: AppColors.gray.defaultShade)),
                //         child: Directionality(
                //           textDirection: TextDirection.rtl,
                //           child: ValueListenableBuilder<bool>(
                //               valueListenable: enableAd,
                //               builder: (context, enabled, _) {
                //                 return Column(
                //                   children: [
                //                     Row(
                //                       children: [
                //                         Container(
                //                           width: 78,
                //                           height: 78,
                //                           decoration: BoxDecoration(
                //                               color: context
                //                                           .read<
                //                                               ThemeModeCubit>()
                //                                           .state ==
                //                                       ThemeMode.dark
                //                                   ? Theme.of(context)
                //                                       .colorScheme
                //                                       .onSurface
                //                                   : AppColors.gray[300],
                //                               shape: BoxShape.circle,
                //                               boxShadow: [
                //                                 BoxShadow(
                //                                     blurRadius: 6,
                //                                     color:
                //                                         const Color(0xff4d4d4d)
                //                                             .withValues(
                //                                                 alpha: 0.4))
                //                               ]),
                //                           child: ClipOval(
                //                             child: Padding(
                //                               padding: const EdgeInsets.all(12),
                //                               child: Assets.icon.bulk.coin
                //                                   .image(
                //                                       color: enabled
                //                                           ? null
                //                                           : AppColors.gray
                //                                               .defaultShade,
                //                                       colorBlendMode:
                //                                           BlendMode.color),
                //                             ),
                //                           ),
                //                         ),
                //                         const SizedBox(
                //                           width: 12,
                //                         ),
                //                         Expanded(
                //                           child: Column(
                //                             crossAxisAlignment:
                //                                 CrossAxisAlignment.start,
                //                             children: [
                //                               Text(
                //                                 'شکار سکه‌ها',
                //                                 style: AppTextStyles.body3
                //                                     .copyWith(
                //                                         fontWeight:
                //                                             FontWeight.bold,
                //                                         color: enabled
                //                                             ? Theme.of(context)
                //                                                 .colorScheme
                //                                                 .primary
                //                                             : AppColors.gray
                //                                                 .defaultShade),
                //                               ),
                //                               Text(
                //                                 'کلیک کن و به ازای تماشای ویدیو، سکه رایگان بگیر',
                //                                 style: AppTextStyles.body4
                //                                     .copyWith(
                //                                         color: enabled
                //                                             ? Theme.of(context)
                //                                                 .colorScheme
                //                                                 .primary
                //                                             : AppColors.gray
                //                                                 .defaultShade),
                //                               ),
                //                             ],
                //                           ),
                //                         ),
                //                       ],
                //                     ),
                //                     const SizedBox(
                //                       height: 8,
                //                     ),
                //                     Divider(),
                //                     const SizedBox(
                //                       height: 8,
                //                     ),
                //                     enabled
                //                         ? Row(
                //                             crossAxisAlignment:
                //                                 CrossAxisAlignment.start,
                //                             children: [
                //                               Column(
                //                                 children: [
                //                                   CircleIconBtn(
                //                                     icon: Assets
                //                                         .icon.outline.coin,
                //                                     iconColor: AppColors
                //                                         .secondryColor
                //                                         .defaultShade,
                //                                     color: AppColors
                //                                         .secondryColor[50],
                //                                   ),
                //                                   SizedBox(
                //                                     height: 4,
                //                                   ),
                //                                   ValueListenableBuilder(
                //                                       valueListenable:
                //                                           coinsForAd,
                //                                       builder:
                //                                           (context, coins, _) {
                //                                         return Text(
                //                                           '$coins سکه هدیه',
                //                                           style: AppTextStyles
                //                                               .body5
                //                                               .copyWith(
                //                                                   color: Theme.of(
                //                                                           context)
                //                                                       .colorScheme
                //                                                       .onSurface),
                //                                         );
                //                                       })
                //                                 ],
                //                               ),
                //                               SizedBox(
                //                                 width: 12,
                //                               ),
                //                               Expanded(
                //                                 flex: 2,
                //                                 child:
                //                                     ValueListenableBuilder<int>(
                //                                         valueListenable:
                //                                             countdown,
                //                                         builder: (context,
                //                                             value, child) {
                //                                           if (value == 0) {
                //                                             return ValueListenableBuilder(
                //                                                 valueListenable:
                //                                                     loadingAdShow,
                //                                                 builder:
                //                                                     (context,
                //                                                         loading,
                //                                                         _) {
                //                                                   return LoadingButton(
                //                                                       loading:
                //                                                           loading,
                //                                                       onPressed:
                //                                                           () async {
                //                                                         loadingAdShow.value =
                //                                                             true;
                //                                                         await TapsellService.showAdRewarded(
                //                                                             context);
                //                                                         loadingAdShow.value =
                //                                                             false;
                //                                                       },
                //                                                       child:
                //                                                           Center(
                //                                                         child:
                //                                                             Text(
                //                                                           'نمایش تبلیغ',
                //                                                           style: AppTextStyles.body3.copyWith(
                //                                                               color: Colors.white,
                //                                                               fontWeight: FontWeight.bold),
                //                                                         ),
                //                                                       ));
                //                                                 });
                //                                           }
                //                                           return Column(
                //                                             crossAxisAlignment:
                //                                                 CrossAxisAlignment
                //                                                     .end,
                //                                             children: [
                //                                               SizedBox(
                //                                                 height: 32,
                //                                                 child: Center(
                //                                                   child:
                //                                                       Directionality(
                //                                                     textDirection:
                //                                                         TextDirection
                //                                                             .ltr,
                //                                                     child: ValueListenableBuilder(
                //                                                         valueListenable: maxSeconds,
                //                                                         builder: (context, max, _) {
                //                                                           return LinearProgressIndicator(
                //                                                             borderRadius:
                //                                                                 BorderRadius.circular(16),
                //                                                             color:
                //                                                                 AppColors.primaryColor.defaultShade,
                //                                                             backgroundColor:
                //                                                                 AppColors.gray[200],
                //                                                             value:
                //                                                                 ((max - value) / max), // Reverse the progress value
                //                                                           );
                //                                                         }),
                //                                                   ),
                //                                                 ),
                //                                               ),
                //                                               SizedBox(
                //                                                 height: 4,
                //                                               ),
                //                                               Text(
                //                                                 '${DateTimeUtils.getFormattedTimeFromSeconds(value)} زمان باقی‌مانده تا تبلیغ بعدی',
                //                                                 style: AppTextStyles
                //                                                     .body5
                //                                                     .copyWith(
                //                                                         color: Theme.of(context)
                //                                                             .colorScheme
                //                                                             .onSurface),
                //                                               )
                //                                             ],
                //                                           );
                //                                         }),
                //                               )
                //                             ],
                //                           )
                //                         : Text(
                //                             'تا اطلاع ثانوی این سرویس در دسترس نیست.',
                //                             style: AppTextStyles.body5.copyWith(
                //                                 color:
                //                                     AppColors.red.defaultShade),
                //                           )
                //                   ],
                //                 );
                //               }),
                //         ),
                //       ),
                //     );
                //   },
                // ),

                // const SizedBox(
                //   height: 4,
                // ),
                // BlocBuilder<UserInfoCubit, UserInfoState>(
                //   builder: (context, state) {
                //     return UserInfoCubit.userInfoModel.cardNumber == null
                //         ? const SizedBox.shrink()
                //         : cardContainer();
                //   },
                // ),

                // const SizedBox(
                //   height: 4,
                // ),

                // GestureDetector(
                //   onTap: () {
                //     context.go(Routes.income);
                //   },
                //   child: Padding(
                //     padding:
                //         const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
                //     child: CustomeBanner(
                //         BannerModel(
                //             child: Padding(
                //               padding: const EdgeInsets.only(left: 32.0),
                //               child: Row(
                //                 mainAxisAlignment: MainAxisAlignment.start,
                //                 crossAxisAlignment: CrossAxisAlignment.center,
                //                 children: [
                //                   Text(
                //                     'فعال سازی درآمد \nاز طریق ساخت دستیار',
                //                     textDirection: TextDirection.rtl,
                //                     textAlign: TextAlign.center,
                //                     style: AppTextStyles.body4.copyWith(
                //                         fontWeight: FontWeight.bold,
                //                         color: Colors.white),
                //                   ),
                //                 ],
                //               ),
                //             ),
                //             imageUrl: '/banner/setting.jpeg',
                //             colors: [
                //               AppColors.secondryColor[200].withAlpha(50),
                //               AppColors.secondryColor[200].withAlpha(80),
                //               AppColors.secondryColor[300],
                //               AppColors.secondryColor[400],
                //               AppColors.secondryColor.defaultShade
                //             ]),
                //         width: double.infinity,
                //         height: Responsive(context).isMobile() ? 120 : 180),
                //   ),
                // ),

                SizedBox(
                  height: 4,
                ),
                // settingContainer(
                //   title: 'افزایش اعتبار',
                //   icon: Assets.icon.outline.crown,
                //   onPressed: () {
                //     context.go(Routes.purchase);
                //   },
                // ),
                if (UserInfoCubit.userInfoModel.code != null)
                  // settingContainer(
                  //   title: 'کد معرف',
                  //   icon: Assets.icon.outline.profileUserDoual,
                  //   widget: Text(UserInfoCubit.userInfoModel.code!,
                  //       style: AppTextStyles.body4.copyWith(
                  //           color: Theme.of(context).colorScheme.onSurface)),
                  //   onPressed: () async {
                  //     await Clipboard.setData(ClipboardData(
                  //         text: UserInfoCubit.userInfoModel.code!));
                  //     Future.delayed(
                  //         Duration.zero,
                  //         () => SnackBarManager(context, id: 'Copy').show(
                  //             status: SnackBarStatus.info,
                  //             message: 'متن کپی شد 😃'));
                  //   },
                  // ),
                  settingContainer(
                    title: 'گزارش میزان مصرف',
                    icon: Assets.icon.outline.chart,
                    onPressed: () {
                      context.go(Routes.utilizationReport);
                    },
                  ),
                settingContainer(
                  title: 'حساب من',
                  icon: Assets.icon.outline.cardPos,
                  onPressed: () {
                    context.go(Routes.myAccount);
                  },
                ),
                GestureDetector(
                  onTap: () {
                    final themeState = context.read<ThemeModeCubit>().state;
                    MorePopupMenuHandler(context: context).showMorePopupMenu(
                        containerKey: containerKey,
                        color: themeState == ThemeMode.dark
                            ? AppColors.black[500]
                            : Colors.white,
                        items: [
                          PopUpMenuItemModel(
                            popupMenuItem: PopupMenuItem<int>(
                              value: 0,
                              child: MorePopupMenuHandler.morePopUpItem(
                                  icon: Assets.icon.outline.moon,
                                  title: 'تیره',
                                  color:
                                      Theme.of(context).colorScheme.onSurface),
                            ),
                            click: () async {
                              context.read<ThemeModeCubit>().setDarkMode();
                            },
                          ),
                          PopUpMenuItemModel(
                            popupMenuItem: PopupMenuItem<int>(
                              value: 1,
                              child: MorePopupMenuHandler.morePopUpItem(
                                  icon: Assets.icon.outline.sun,
                                  title: 'روشن',
                                  color:
                                      Theme.of(context).colorScheme.onSurface),
                            ),
                            click: () async {
                              context.read<ThemeModeCubit>().setLightkMode();
                            },
                          ),
                          PopUpMenuItemModel(
                            popupMenuItem: PopupMenuItem<int>(
                              value: 2,
                              child: MorePopupMenuHandler.morePopUpItem(
                                  icon: Assets.icon.outline.setting,
                                  title: 'سیستم',
                                  color:
                                      Theme.of(context).colorScheme.onSurface),
                            ),
                            click: () async {
                              context.read<ThemeModeCubit>().setDefaultSystem();
                            },
                          ),
                        ]);
                  },
                  child: Container(
                    key: containerKey,
                    child: settingContainer(
                        title: 'حالت نمایش',
                        icon: Assets.icon.outline.brush,
                        widget: BlocBuilder<ThemeModeCubit, ThemeMode>(
                          builder: (context, state) {
                            final mode = context
                                .read<ThemeModeCubit>()
                                .whatIsThemeMode();
                            return Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                (mode == 'dark'
                                        ? Assets.icon.outline.moon
                                        : mode == 'light'
                                            ? Assets.icon.outline.sun
                                            : Assets.icon.outline.setting)
                                    .svg(
                                        color: Theme.of(context)
                                            .colorScheme
                                            .onSurface),
                                const SizedBox(
                                  width: 8,
                                ),
                                Text(
                                  mode == 'dark'
                                      ? 'تیره'
                                      : mode == 'light'
                                          ? 'روشن'
                                          : 'سیستم',
                                  style: AppTextStyles.body4.copyWith(
                                      color: Theme.of(context)
                                          .colorScheme
                                          .onSurface),
                                ),
                              ],
                            );
                          },
                        )),
                  ),
                ),

                // Stack(
                //   children: [
                //     settingContainer(
                //         title: 'دریافت اعلانات',
                //         icon: Assets.icon.outline.notificationBing,
                //         widget: const SizedBox()),
                //     Positioned(
                //         left: 32,
                //         top: 16,
                //         bottom: 16,
                //         child: LiteRollingSwitch(
                //           value: false,
                //           textOn: '',
                //           textOff: '',
                //           colorOn: AppColors.primaryColor.defaultShade,
                //           colorOff: AppColors.gray.defaultShade,
                //           onChanged: (bool state) {
                //             //Use it to manage the different states
                //             isDark.value = !state;
                //           },
                //         ))
                //   ],
                // ),

                settingContainer(
                    title: 'قوانین و حریم خصوصی',
                    icon: Assets.icon.outline.shieldTick,
                    onPressed: () async {
                      await launchUrl(
                              Uri.parse(
                                  'https://houshan.ai/%D9%82%D9%88%D8%A7%D9%86%DB%8C%D9%86-%D9%88-%D9%85%D9%82%D8%B1%D8%A7%D8%B1%D8%A7%D8%AA-%D8%AD%D9%81%D8%B8-%D8%AD%D8%B1%DB%8C%D9%85-%D8%AE%D8%B5%D9%88%D8%B5%DB%8C-%DA%A9%D8%A7%D8%B1%D8%A8%D8%B1%D8%A7%D9%86/'),
                              mode: LaunchMode.externalApplication)
                          .onError(
                        (error, stackTrace) {
                          if (kDebugMode) {
                            print('error open Link is: $error');
                          }
                          return false;
                        },
                      );
                    }),

                // animatedSettingContainer(
                //     title: 'درباره ما',
                //     icon: Assets.icon.outline.more,
                //     childrens: [
                //       const SizedBox(
                //         height: 24,
                //       ),
                //       settingContainer(
                //         title: 'سایر محصولات',
                //         icon: Assets.icon.outline.mobile,
                //         notBorder: true,
                //         notMargin: true,
                //         notPadding: true,
                //         onPressed: () {
                //           context.go(Routes.otherProducts);
                //         },
                //       ),
                //       const SizedBox(
                //         height: 8,
                //       ),
                //       Divider(
                //         color: AppColors.gray.defaultShade,
                //       ),
                //       const SizedBox(
                //         height: 12,
                //       ),
                //       settingContainer(
                //           title: 'تازه‌ترین‌های هوشان',
                //           icon: Assets.icon.outline.lampCharge,
                //           notBorder: true,
                //           notMargin: true,
                //           onPressed: () async {
                //             await launchUrl(Uri.parse('https://Houshan.ai'),
                //                     mode: LaunchMode.externalApplication)
                //                 .onError(
                //               (error, stackTrace) {
                //                 if (kDebugMode) {
                //                   print('error open Link is: $error');
                //                 }
                //                 return false;
                //               },
                //             );
                //           },
                //           notPadding: true),
                //     ]),

                settingContainer(
                  title: 'ارسال تیکت به پشتیبانی',
                  icon: Assets.icon.outline.messages,
                  onPressed: () {
                    context.go(Routes.ticket);
                  },
                ),

                // settingContainer(
                //   title: 'نمایش تبلیغ',
                //   icon: Assets.icon.outline.messages,
                //   onPressed: () async {
                //     await showDialog(
                //         context: context,
                //         builder: (context) => Dialog(
                //               insetPadding: EdgeInsets.all(16),
                //               child: Column(
                //                 mainAxisSize: MainAxisSize.min,
                //                 children: [
                //                   SizedBox(
                //                     height: 16,
                //                   ),
                //                   ElevatedButton(
                //                       onPressed: () async {
                //                         await TapsellService.showAdInterstitial();
                //                       },
                //                       child: Text('INTERSTITIAL')),
                //                   SizedBox(
                //                     height: 16,
                //                   ),
                //                   ElevatedButton(
                //                       onPressed: () async {
                //                         await TapsellService.showAdRewarded(context);
                //                       },
                //                       child: Text('REWARDED')),
                //                   SizedBox(
                //                     height: 16,
                //                   ),
                //                   ElevatedButton(
                //                       onPressed: () async {
                //                         await TapsellService.showAdBanner(
                //                             context);
                //                       },
                //                       child: Text('AD')),
                //                   SizedBox(
                //                     height: 16,
                //                   ),
                //                 ],
                //               ),
                //             ));
                //   },
                // ),

                settingContainer(
                    title: 'سوالات متداول',
                    icon: Assets.icon.outline.messageQuestion,
                    onPressed: () => context.go(Routes.faq)),
                const SizedBox(
                  height: 16,
                ),
                GestureDetector(
                  onTap: () async {
                    try {
                      await Share.share('هوشان؛ دوست هوش مصنوعی تو\n'
                          'هر سوالی داری، هر تصمیمی که می‌خوای بگیری یا هر ایده‌ی خلاقانه‌ای که داری و می‌خوای به واقعیت تبدیلش کنی، هوشان همیشه هست تا کمکت کنه!\n'
                          'کد معرف: ${UserInfoCubit.userInfoModel.code}\n'
                          'https://houshan.ai/');
                    } catch (e) {
                      if (kDebugMode) {
                        print('Error in share Text: $e');
                      }
                    }
                  },
                  child: Padding(
                    padding: const EdgeInsets.fromLTRB(32, 16, 32, 16),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Text(
                          'اشتراک گذاری',
                          style: AppTextStyles.body4.copyWith(
                              color: Theme.of(context).colorScheme.primary),
                        ),
                        const SizedBox(
                          width: 8,
                        ),
                        Assets.icon.outline.share
                            .svg(color: Theme.of(context).colorScheme.primary),
                      ],
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    DialogHandler(context: context).showLogOut();
                  },
                  child: Padding(
                    padding: const EdgeInsets.fromLTRB(32, 16, 32, 16),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Text(
                          'خروج از حساب کاربری',
                          style: AppTextStyles.body4.copyWith(
                              color: Theme.of(context).colorScheme.secondary),
                        ),
                        const SizedBox(
                          width: 8,
                        ),
                        Assets.icon.outline.login.svg(
                            color: Theme.of(context).colorScheme.secondary),
                      ],
                    ),
                  ),
                ),
                const SizedBox(
                  height: 16,
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 32),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      // InkWell(
                      //   onTap: () async {
                      //     await launchUrl(
                      //             Uri.parse('https://t.me/houshanAiBot'),
                      //             mode: LaunchMode.externalApplication)
                      //         .onError(
                      //       (error, stackTrace) {
                      //         if (kDebugMode) {
                      //           print('error open Link is: $error');
                      //         }
                      //         return false;
                      //       },
                      //     );
                      //   },
                      //   child: Assets.icon.social.bold.telegram.svg(
                      //       width: 32,
                      //       height: 32,
                      //       color: Theme.of(context).colorScheme.primary),
                      // ),
                      const SizedBox(
                        width: 16,
                      ),
                      InkWell(
                        onTap: () async {
                          await launchUrl(
                                  Uri.parse(
                                      'https://www.instagram.com/Houshan.ai'),
                                  mode: LaunchMode.externalApplication)
                              .onError(
                            (error, stackTrace) {
                              if (kDebugMode) {
                                print('error open Link is: $error');
                              }
                              return false;
                            },
                          );
                        },
                        child: Assets.icon.social.bold.instagram.svg(
                            width: 32,
                            height: 32,
                            color: Theme.of(context).colorScheme.primary),
                      ),
                      const SizedBox(
                        width: 16,
                      ),
                      InkWell(
                        onTap: () async {
                          await launchUrl(Uri.parse('https://houshan.ai/'),
                                  mode: LaunchMode.externalApplication)
                              .onError(
                            (error, stackTrace) {
                              if (kDebugMode) {
                                print('error open Link is: $error');
                              }
                              return false;
                            },
                          );
                        },
                        child: Assets.icon.social.bold.site.svg(
                            width: 32,
                            height: 32,
                            color: Theme.of(context).colorScheme.primary),
                      ),
                    ],
                  ),
                ),
                const SizedBox(
                  height: 16,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    FutureBuilder<PackageInfo>(
                        future: PackageInfo.fromPlatform(),
                        builder: (context, snapshot) {
                          String version = '...';
                          if (snapshot.hasData && snapshot.data != null) {
                            version = snapshot.data!.version;
                          }
                          return Text(
                            'ورژن: $version',
                            style: AppTextStyles.body4.copyWith(
                                color: Theme.of(context).colorScheme.onSurface),
                          );
                        })
                  ],
                ),
                const SizedBox(
                  height: 16,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget cardContainer() {
    return BlocBuilder<UserInfoCubit, UserInfoState>(
      builder: (context, state) {
        return ValueListenableBuilder(
            valueListenable: showCredit,
            builder: (context, show, child) {
              return Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
                child: Column(
                  children: [
                    Container(
                      padding: const EdgeInsets.all(16),
                      decoration: BoxDecoration(
                          color: Theme.of(context).colorScheme.surface,
                          borderRadius: const BorderRadius.only(
                              topLeft: Radius.circular(16),
                              topRight: Radius.circular(16)),
                          border: Border.all(
                              color: Theme.of(context).colorScheme.primary)),
                      child: Column(
                        children: [
                          const SizedBox(
                            height: 12,
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text(
                                show
                                    ? UserInfoCubit.userInfoModel.cardNumber!
                                        .charRagham(separator: ' - ')
                                    : '**** - **** - **** - ****',
                                style: AppTextStyles.body3.copyWith(
                                    color:
                                        Theme.of(context).colorScheme.primary,
                                    fontWeight: FontWeight.bold),
                              ),
                              Text(
                                show
                                    ? UserInfoCubit.userInfoModel.cardNumber!
                                        .replaceAll(' ', '')
                                        .getBankNameFromCardNumber()
                                    : '**** **** **',
                                style: AppTextStyles.body4.copyWith(
                                    color: Theme.of(context)
                                        .colorScheme
                                        .onSurface),
                              )
                            ],
                          ),
                          const SizedBox(
                            height: 12,
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Row(
                                children: [
                                  Text('سکه',
                                      style: AppTextStyles.headline6.copyWith(
                                          color: Theme.of(context)
                                              .colorScheme
                                              .primary)),
                                  const SizedBox(
                                    width: 4,
                                  ),
                                  Text(
                                      show
                                          ? UserInfoCubit.userInfoModel.income
                                              .toString()
                                              .seRagham()
                                          : '*****',
                                      style: AppTextStyles.headline6.copyWith(
                                          color: Theme.of(context)
                                              .colorScheme
                                              .secondary)),
                                ],
                              ),
                              GestureDetector(
                                onTap: () => showCredit.value = !show,
                                child: Row(
                                  children: [
                                    Text('موجودی',
                                        style: AppTextStyles.headline6.copyWith(
                                            color: Theme.of(context)
                                                .colorScheme
                                                .primary)),
                                    const SizedBox(
                                      width: 8,
                                    ),
                                    Padding(
                                      padding:
                                          const EdgeInsets.only(bottom: 4.0),
                                      child: Icon(
                                        show
                                            ? CupertinoIcons.eye
                                            : CupertinoIcons.eye_slash,
                                        size: 20,
                                        color: Theme.of(context)
                                            .colorScheme
                                            .secondary,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          )
                        ],
                      ),
                    ),
                    Container(
                      padding: const EdgeInsets.all(8),
                      decoration: BoxDecoration(
                          color: Theme.of(context).colorScheme.primary,
                          border: Border.all(
                              color: Theme.of(context).colorScheme.primary),
                          borderRadius: const BorderRadius.only(
                              bottomLeft: Radius.circular(16),
                              bottomRight: Radius.circular(16))),
                      child: Center(
                        child: GestureDetector(
                          onTap: () => DialogHandler(context: context).showCoin(
                            onSuccess: () {
                              final user = UserInfoCubit.userInfoModel;
                              user.income = 0;
                              context.read<UserInfoCubit>().changeUser(user);
                            },
                          ),
                          child: Text(
                            'تسویه حساب',
                            style: AppTextStyles.headline6
                                .copyWith(color: Colors.white),
                          ),
                        ),
                      ),
                    )
                  ],
                ),
              );
            });
      },
    );
  }

  Widget settingContainer({
    required final String title,
    required final SvgGenImage icon,
    final Widget? widget,
    final Function()? onPressed,
    final bool notBorder = false,
    final bool notMargin = false,
    final bool notPadding = false,
  }) {
    return GestureDetector(
      onTap: onPressed,
      child: Container(
        margin: notMargin
            ? EdgeInsets.zero
            : const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
        padding: notPadding
            ? EdgeInsets.zero
            : const EdgeInsets.symmetric(horizontal: 16, vertical: 18),
        decoration: BoxDecoration(
            color: Theme.of(context).colorScheme.surface,
            border: notBorder
                ? null
                : Border.all(color: AppColors.gray.defaultShade),
            borderRadius: BorderRadius.circular(18)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            widget ??
                Icon(
                  Icons.arrow_back_ios_new_rounded,
                  size: 18,
                  color: AppColors.secondryColor.defaultShade,
                ),
            Row(
              children: [
                Text(
                  title,
                  style: AppTextStyles.body4
                      .copyWith(color: Theme.of(context).colorScheme.onSurface),
                ),
                const SizedBox(
                  width: 8,
                ),
                icon.svg(color: Theme.of(context).colorScheme.primary),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget animatedSettingContainer(
      {required final String title,
      required final SvgGenImage icon,
      required final List<Widget> childrens}) {
    final ValueNotifier<bool> show = ValueNotifier(false);
    return GestureDetector(
      onTap: () {
        show.value = !show.value;
      },
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 18),
        decoration: BoxDecoration(
            color: Theme.of(context).colorScheme.surface,
            border: Border.all(color: AppColors.gray.defaultShade),
            borderRadius: BorderRadius.circular(18)),
        child: ValueListenableBuilder(
          valueListenable: show,
          builder: (context, isVisible, child) => Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Transform.rotate(
                    angle: (isVisible ? 90 : -90) * pi / 180,
                    child: Icon(
                      Icons.arrow_back_ios_new_rounded,
                      size: 18,
                      color: AppColors.secondryColor.defaultShade,
                    ),
                  ),
                  Row(
                    children: [
                      Text(
                        title,
                        style: AppTextStyles.body4.copyWith(
                            color: Theme.of(context).colorScheme.onSurface),
                      ),
                      const SizedBox(
                        width: 8,
                      ),
                      icon.svg(color: AppColors.primaryColor.defaultShade),
                    ],
                  ),
                ],
              ),
              AnimatedVisibility(
                  isVisible: isVisible,
                  duration: const Duration(milliseconds: 300),
                  child: Column(
                    children: childrens,
                  ))
            ],
          ),
        ),
      ),
    );
  }
}
