import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/app_export.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/int_extensions.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/habib_wallet/habib_wallet_view_model.dart';
import 'package:hmg_patient_app_new/features/profile_settings/profile_settings_view_model.dart';
import 'package:hmg_patient_app_new/presentation/habib_wallet/habib_wallet_page.dart';
import 'package:hmg_patient_app_new/presentation/habib_wallet/recharge_wallet_page.dart';
import 'package:hmg_patient_app_new/presentation/lab/collapsing_list_view.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/app_language_change.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/chip/app_custom_chip_widget.dart';
import 'package:hmg_patient_app_new/widgets/common_bottom_sheet.dart';
import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart';
import 'package:hmg_patient_app_new/widgets/transitions/fade_page.dart';
import 'package:provider/provider.dart';

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

  @override
  _ProfileSettingsState createState() {
    return _ProfileSettingsState();
  }
}

class _ProfileSettingsState extends State<ProfileSettings> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  int length = 3;
  final SwiperController _controller = SwiperController();

  int _index = 0;

  @override
  Widget build(BuildContext context) {
    return CollapsingListView(
      title: "Profile & Settings".needTranslation,
      logout: () {},
      isClose: true,
      child: SingleChildScrollView(
        padding: EdgeInsets.only(top: 24, bottom: 24),
        physics: NeverScrollableScrollPhysics(),
        child: Consumer<ProfileSettingsViewModel>(
          builder: (context, model, child) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Swiper(
                  itemCount: length,
                  layout: SwiperLayout.STACK,
                  loop: true,
                  itemWidth: MediaQuery.of(context).size.width - 42,
                  indicatorLayout: PageIndicatorLayout.COLOR,
                  axisDirection: AxisDirection.right,
                  controller: _controller,
                  itemHeight: 210 + 16,
                  pagination: const SwiperPagination(
                    alignment: Alignment.bottomCenter,
                    margin: EdgeInsets.only(top: 210 + 8 + 24),
                    builder: DotSwiperPaginationBuilder(color: Color(0xffD9D9D9), activeColor: AppColors.blackBgColor),
                  ),
                  itemBuilder: (BuildContext context, int index) {
                    return FamilyCardWidget().paddingOnly(right: 16);
                  },
                ),
                GridView(
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, crossAxisSpacing: 9, mainAxisSpacing: 9),
                  physics: const NeverScrollableScrollPhysics(),
                  padding: const EdgeInsets.all(24),
                  shrinkWrap: true,
                  children: [
                    Container(
                      padding: EdgeInsets.all(16),
                      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                        color: AppColors.whiteColor,
                        borderRadius: 20.h,
                        hasShadow: true,
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        spacing: 4.h,
                        children: [
                          Row(
                            spacing: 8.h,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Utils.buildSvgWithAssets(icon: AppAssets.wallet, width: 40.h, height: 40.h),
                              "Habib Wallet".needTranslation.toText14(weight: FontWeight.w600, maxlines: 2).expanded,
                              Utils.buildSvgWithAssets(icon: AppAssets.arrow_forward),
                            ],
                          ),
                          Spacer(),
                          Consumer<HabibWalletViewModel>(builder: (context, habibWalletVM, child) {
                            return Utils.getPaymentAmountWithSymbol2(habibWalletVM.habibWalletAmount, isExpanded: false)
                                .toShimmer2(isShow: habibWalletVM.isWalletAmountLoading, radius: 12.h, width: 80.h, height: 24.h);
                          }),
                          CustomButton(
                            height: 40.h,
                            icon: AppAssets.recharge_icon,
                            iconSize: 24.h,
                            iconColor: AppColors.infoColor,
                            textColor: AppColors.infoColor,
                            text: "Recharge".needTranslation,
                            borderWidth: 0.h,
                            fontWeight: FontWeight.w500,
                            borderColor: Colors.transparent,
                            backgroundColor: Color(0xff45A2F8).withValues(alpha: 0.08),
                            padding: EdgeInsets.all(8.h),
                            fontSize: 14,
                            onPressed: () {
                              Navigator.of(context).push(CustomPageRoute(page: RechargeWalletPage()));
                            },
                          ),
                        ],
                      ).onPress(() {
                        Navigator.of(context).push(CustomPageRoute(page: HabibWalletPage()));
                      }),
                    )
                  ],
                ),
                "Quick Actions".needTranslation.toText18(weight: FontWeight.w600, textOverflow: TextOverflow.ellipsis, maxlines: 1).paddingOnly(left: 24, right: 24),
                Container(
                  margin: EdgeInsets.only(left: 24, right: 24, top: 16, bottom: 24),
                  padding: EdgeInsets.only(top: 4, bottom: 4),
                  decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.h, hasShadow: true),
                  child: Column(
                    children: [
                      actionItem(AppAssets.language_change, "Language".needTranslation, () {
                        showCommonBottomSheetWithoutHeight(context, title: "Application Language".needTranslation, child: AppLanguageChange(), callBackFunc: () {}, isFullScreen: false);
                      }, trailingLabel: Utils.appState.isArabic() ? "العربية".needTranslation : "English".needTranslation),
                      1.divider,
                      actionItem(AppAssets.accessibility, "Accessibility".needTranslation, () {}),
                      1.divider,
                      actionItem(AppAssets.bell, "Notifications Settings".needTranslation, () {}),
                      1.divider,
                      actionItem(AppAssets.touch_face_id, "Touch ID / Face ID Services".needTranslation, () {}, switchValue: true),
                    ],
                  ),
                ),
                "Personal Information".needTranslation.toText18(weight: FontWeight.w600, textOverflow: TextOverflow.ellipsis, maxlines: 1).paddingOnly(left: 24, right: 24),
                Container(
                  margin: EdgeInsets.only(left: 24, right: 24, top: 16, bottom: 24),
                  padding: EdgeInsets.only(top: 4, bottom: 4),
                  decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.h, hasShadow: true),
                  child: Column(
                    children: [
                      actionItem(AppAssets.email_transparent, "Update Email Address".needTranslation, () {}),
                      1.divider,
                      actionItem(AppAssets.smart_phone_fill, "Phone Number".needTranslation, () {}),
                      1.divider,
                      actionItem(AppAssets.my_address, "My Addresses".needTranslation, () {}),
                      1.divider,
                      actionItem(AppAssets.emergency, "Emergency Contact".needTranslation, () {}),
                    ],
                  ),
                ),
                "Help & Support".needTranslation.toText18(weight: FontWeight.w600, textOverflow: TextOverflow.ellipsis, maxlines: 1).paddingOnly(left: 24, right: 24),
                Container(
                  margin: EdgeInsets.only(left: 24, right: 24, top: 16),
                  padding: EdgeInsets.only(top: 4, bottom: 4),
                  decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.h, hasShadow: true),
                  child: Column(
                    children: [
                      actionItem(AppAssets.call_fill, "Contact Us".needTranslation, () {}, trailingLabel: "9200666666"),
                      1.divider,
                      actionItem(AppAssets.permission, "Permissions".needTranslation, () {}, trailingLabel: "Location, Camera"),
                      1.divider,
                      actionItem(AppAssets.rate, "Rate Our App".needTranslation, () {}, isExternalLink: true),
                      1.divider,
                      actionItem(AppAssets.privacy_terms, "Privacy Policy".needTranslation, () {}, isExternalLink: true),
                      1.divider,
                      actionItem(AppAssets.privacy_terms, "Terms & Conditions".needTranslation, () {}, isExternalLink: true),
                    ],
                  ),
                ),
                CustomButton(icon: AppAssets.minus, text: "Deactivate account".needTranslation, onPressed: () {}).paddingAll(24),
              ],
            );
          },
        ),
      ),
    );
  }

  Widget actionItem(String icon, String label, VoidCallback onPress, {String trailingLabel = "", bool? switchValue, bool isExternalLink = false}) {
    return SizedBox(
      height: 56,
      child: Row(
        spacing: 8.h,
        children: [
          Utils.buildSvgWithAssets(icon: icon, iconColor: AppColors.greyTextColor),
          label.toText14(weight: FontWeight.w500, textOverflow: TextOverflow.ellipsis, maxlines: 1).expanded,
          if (trailingLabel.isNotEmpty) trailingLabel.toText14(color: AppColors.greyTextColor, weight: FontWeight.w500, textOverflow: TextOverflow.ellipsis, maxlines: 1),
          switchValue != null
              ? Switch(
                  value: switchValue,
                  onChanged: (value) {},
                  activeColor: AppColors.successColor,
                  activeTrackColor: AppColors.successColor.withValues(alpha: .15),
                )
              : Transform.scale(
                  scaleX: Utils.appState.isArabic() ? -1 : 1,
                  child: Utils.buildSvgWithAssets(icon: isExternalLink ? AppAssets.externalLink : AppAssets.arrow_forward),
                )
        ],
      ).paddingOnly(left: 16, right: 16).onPress(onPress),
    );
  }
}

class FamilyCardWidget extends StatelessWidget {
  FamilyCardWidget();

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
        color: AppColors.whiteColor,
        borderRadius: 20.h,
        hasShadow: true,
      ),
      child: Column(
        children: [
          Column(
            spacing: 8.h,
            children: [
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                spacing: 8.h,
                children: [
                  Image.asset(true ? AppAssets.male_img : AppAssets.femaleImg, width: 56.h, height: 56.h),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    spacing: 0.h,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      "Mahmoud Shrouf Shrouf".toText18(isBold: true, weight: FontWeight.w600, textOverflow: TextOverflow.ellipsis, maxlines: 1),
                      AppCustomChipWidget(
                        icon: AppAssets.file_icon,
                        labelText: "File no: 3423443",
                        iconSize: 14,
                      ),
                    ],
                  ).expanded,
                  Icon(Icons.qr_code, size: 56)
                ],
              ).expanded,
              SizedBox(
                width: double.infinity,
                child: Wrap(
                  alignment: WrapAlignment.start,
                  spacing: 8.h,
                  children: [
                    AppCustomChipWidget(labelText: "35 Years Old"),
                    AppCustomChipWidget(labelText: "Blood: A+"),
                    AppCustomChipWidget(
                      icon: AppAssets.insurance_active_icon,
                      labelText: "Insurance Active",
                      iconColor: AppColors.bgGreenColor,
                      iconSize: 14,
                      backgroundColor: AppColors.bgGreenColor.withValues(alpha: 0.15),
                    ),
                  ],
                ),
              ),
            ],
          ).paddingOnly(top: 16, right: 16, left: 16, bottom: 12).expanded,
          1.divider,
          CustomButton(icon: AppAssets.add_family, text: "Add a new family member".needTranslation, onPressed: () {}).paddingOnly(top: 12, right: 16, left: 16, bottom: 16),
        ],
      ),
    );
  }
}
