import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/location_util.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.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/contact_us/contact_us_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/contact_us/widgets/find_us_item_card.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.dart';
import 'package:hmg_patient_app_new/widgets/chip/app_custom_chip_widget.dart';
import 'package:hmg_patient_app_new/widgets/custom_tab_bar.dart';
import 'package:provider/provider.dart';

import '../../core/utils/utils.dart';

class FindUsPage extends StatelessWidget {
  FindUsPage({super.key});

  late AppState appState;
  late ContactUsViewModel contactUsViewModel;
  late LocationUtils locationUtils;

  @override
  Widget build(BuildContext context) {
    contactUsViewModel = Provider.of<ContactUsViewModel>(context);
    appState = getIt.get<AppState>();
    locationUtils = getIt.get<LocationUtils>();
    locationUtils.isShowConfirmDialog = true;
    return Scaffold(
      backgroundColor: AppColors.bgScaffoldColor,
      body: CollapsingListView(
        title: LocaleKeys.location.tr(),
        child: Consumer<ContactUsViewModel>(builder: (context, contactUsVM, child) {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(height: 16.h),
              contactUsVM.isHMGLocationsListLoading
                  ? SizedBox.shrink()
                  : CustomTabBar(
                      activeTextColor: AppColors.primaryRedColor,
                      activeBackgroundColor: AppColors.primaryRedColor.withValues(alpha: .1),
                      tabs: [
                        CustomTabBarModel(null, LocaleKeys.hmgHospitals.tr()),
                        CustomTabBarModel(null, LocaleKeys.hmgPharmacy.tr()),
                      ],
                      onTabChange: (index) {
                        contactUsVM.setHMGHospitalsListSelected(index == 0);
                      },
                    ).paddingSymmetrical(24.h, 0.h),
              Row(
                mainAxisSize: MainAxisSize.max,
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      LocaleKeys.sortByLocation.tr(context: context).toText14(isBold: true),
                      SizedBox(height: 4.h),
                      LocaleKeys.sortByNearestLocation.tr(context: context).toText11(color: AppColors.textColorLight, isBold: true),
                    ],
                  ),
                  const Spacer(),
                  Switch(
                    activeThumbColor: AppColors.successColor,
                    activeTrackColor: AppColors.successColor.withValues(alpha: .15),
                    value: contactUsVM.hasLocationEnabled,
                    onChanged: (newValue) async {
                      if (newValue) {
                        locationUtils.getCurrentLocation(
                          onSuccess: (value) {
                            // if (contactUsVM.hmgHospitalsLocationsList.isNotEmpty) {
                            //   contactUsVM.sortHMGLocations(true);
                            //   contactUsVM.setHasLocationEnabled(newValue);
                            // } else {
                              contactUsVM.initContactUsViewModel();
                              contactUsVM.setHasLocationEnabled(newValue);
                              contactUsVM.sortHMGLocations(true);
                            // }
                          },
                          onFailure: () {
                            locationUtils.onFailureDilouge();
                          },
                        );
                      } else {
                        contactUsVM.sortHMGLocations(false);
                        contactUsVM.setHasLocationEnabled(newValue);
                      }
                      // bookAppointmentsVM.setIsNearestAppointmentSelected(newValue);
                    },
                  ),
                ],
              ).paddingSymmetrical(24.h, 12.h),
              ListView.separated(
                padding: EdgeInsets.only(top: 4.h),
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                itemCount: contactUsVM.isHMGLocationsListLoading
                    ? 5
                    : contactUsVM.isHMGHospitalsListSelected
                        ? contactUsVM.hmgHospitalsLocationsList.length
                        : contactUsVM.hmgPharmacyLocationsList.length,
                itemBuilder: (context, index) {
                  return contactUsVM.isHMGLocationsListLoading
                      ? Container(
                          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.h, hasShadow: true),
                          child: Container(
                            decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                              color: AppColors.whiteColor,
                              borderRadius: 20.h,
                              hasShadow: true,
                            ),
                            child: Padding(
                              padding: EdgeInsets.all(14.h),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Row(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Utils.buildImgWithNetwork(
                                        url: "https://hmgwebservices.com/Images/MobileImages/DUBAI/unkown_female.png",
                                        iconColor: AppColors.transparent,
                                        width: 63.h,
                                        height: 63.h,
                                        fit: BoxFit.cover,
                                      ).circle(100).toShimmer2(isShow: true),
                                      SizedBox(width: 16.h),
                                      Expanded(
                                        child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            "Dr John Smith".toText16(isBold: true).toShimmer2(isShow: true),
                                            SizedBox(height: 8.h),
                                            Wrap(
                                              direction: Axis.horizontal,
                                              spacing: 3.h,
                                              runSpacing: 4.h,
                                              children: [
                                                AppCustomChipWidget(labelText: "").toShimmer2(isShow: true, width: 16.h),
                                                AppCustomChipWidget(labelText: "").toShimmer2(isShow: true, width: 16.h),
                                              ],
                                            ),
                                          ],
                                        ),
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ).paddingSymmetrical(24.h, 0.h)
                      : contactUsVM.isHMGHospitalsListSelected
                          // ? contactUsVM.hmgHospitalsLocationsList.isNotEmpty
                              ? AnimationConfiguration.staggeredList(
                                  position: index,
                                  duration: const Duration(milliseconds: 500),
                                  child: SlideAnimation(
                                    verticalOffset: 100.0,
                                    child: FadeInAnimation(
                                      child: AnimatedContainer(
                                        duration: Duration(milliseconds: 300),
                                        curve: Curves.easeInOut,
                                        decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.h, hasShadow: true),
                                        child: FindUsItemCard(
                                          getHMGLocationsModel: contactUsVM.hmgHospitalsLocationsList[index],
                                        ),
                                      ).paddingSymmetrical(24.h, 0.h),
                                    ),
                                  ),
                                )
                              : AnimationConfiguration.staggeredList(
                                  position: index,
                                  duration: const Duration(milliseconds: 500),
                                  child: SlideAnimation(
                                    verticalOffset: 100.0,
                                    child: FadeInAnimation(
                                      child: AnimatedContainer(
                                        duration: Duration(milliseconds: 300),
                                        curve: Curves.easeInOut,
                                        decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.h, hasShadow: true),
                                        child: FindUsItemCard(
                                          getHMGLocationsModel: contactUsVM.hmgPharmacyLocationsList[index],
                                        ),
                                      ).paddingSymmetrical(24.h, 0.h),
                                    ),
                                  ),
                                );
                          // : Utils.getNoDataWidget(
                          //     context,
                          //     noDataText: "No any locations yet.".needTranslation,
                          //   );
                },
                separatorBuilder: (BuildContext cxt, int index) => SizedBox(height: 16.h),
              ),
              SizedBox(height: 24.h),
              // FindUsItemCard(),
              // FindUsItemCard(),
              // FindUsItemCard(),
            ],
          );
        }),
      ),
    );
  }
}
