import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_animations/flutter_staggered_animations.dart'
    show AnimationConfiguration, SlideAnimation, FadeInAnimation;
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/app_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/enums.dart';
import 'package:hmg_patient_app_new/core/utils/debouncer.dart';
import 'package:hmg_patient_app_new/core/utils/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/emergency_services/emergency_services_view_model.dart';
import 'package:hmg_patient_app_new/features/location/GeocodeResponse.dart';
import 'package:hmg_patient_app_new/features/location/PlacePrediction.dart';
import 'package:hmg_patient_app_new/features/location/location_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/widgets/input_widget.dart';
import 'package:provider/provider.dart';

import '../../../theme/colors.dart';

class LocationInputBottomSheet extends StatelessWidget {
  final Debouncer debouncer = Debouncer(milliseconds: 500);
  final Function(Location)? moveCameraController;

   LocationInputBottomSheet({super.key, this.moveCameraController});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        TextInputWidget(
          labelText: LocaleKeys.search.tr(),
          hintText: LocaleKeys.selectLocation.tr(context: context),
          controller: TextEditingController(),
          onChange: (value){
            debouncer.run(() {
              context.read<LocationViewModel>().updateSearchQuery(value);
            });
          },
          isEnable: true,
          prefix: null,
          autoFocus: false,
          isBorderAllowed: false,
          keyboardType: TextInputType.text,
          isAllowLeadingIcon: true,
          selectionType: SelectionTypeEnum.search,
          padding: EdgeInsets.symmetric(
            vertical: 10.h,
            horizontal: 15.h,
          ),
          fontFamily: "Poppins",
        ),
        SizedBox(height: 24.h),
        Selector<LocationViewModel, List<PlacePrediction>>(
          selector: (_, vm) => vm.predictions ?? <PlacePrediction>[],
          builder: (context, predictions, _) {
            if (predictions.isEmpty) return SizedBox.shrink();
            return Expanded(
              child: ListView.separated(
                separatorBuilder: (_,__)=>SizedBox(height: 16.h,),
                itemCount: predictions.length,
                itemBuilder: (_, index) {
                  final prediction = predictions[index];
              
                  return 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: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Expanded(
                                child: prediction.description.toText14(
                                    color: AppColors.textColor,
                                    weight: FontWeight.w600),
                              ),
                              Transform.flip(
                                flipX: getIt.get<AppState>().isArabic(),
                                child: Utils.buildSvgWithAssets(
                                  icon: AppAssets.forward_arrow_icon_small,
                                  iconColor: AppColors.blackColor,
                                width: 20.h,
                                height: 15.h,
                                fit: BoxFit.contain,
                                ),
                              )
                            ],
                          ).paddingAll(24.h),),
                      ),
                    ),
                  ).onPress(() async {
                    await context.read<LocationViewModel>().selectPlacePrediction(prediction);
                    Navigator.of(context).pop();
                    var location = context.read<LocationViewModel>().placeDetails;
                    if(location != null) {
                      moveCameraController?.call(
                          Location(lat: location.lat, lng: location.lng));
                    }
                  });
              
                },
              ),
            );
          },
        )
      ],
    );
  }
}
