import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_nfc_kit/flutter_nfc_kit.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/common_models/privilege/ProjectDetailListModel.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/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/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:barcode_scan2/barcode_scan2.dart';
import 'package:hmg_patient_app_new/widgets/common_bottom_sheet.dart';
import 'package:hmg_patient_app_new/widgets/nfc/nfc_reader_sheet.dart';

class ErOnlineCheckinSelectCheckinBottomSheet extends StatelessWidget {
  ErOnlineCheckinSelectCheckinBottomSheet({super.key, required this.projectID});

  bool _supportsNFC = false;
  int projectID = 0;

  late LocationUtils locationUtils;
  late AppState appState;
  ProjectDetailListModel projectDetailListModel = ProjectDetailListModel();

  @override
  Widget build(BuildContext context) {
    appState = getIt.get<AppState>();
    FlutterNfcKit.nfcAvailability.then((value) {
      _supportsNFC = (value == NFCAvailability.available);
    });
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        checkInOptionCard(
          AppAssets.checkin_location_icon,
          LocaleKeys.liveLocation.tr(context: context),
          LocaleKeys.verifyYourLocationAtHospital.tr(context: context),
        ).onPress(() {
          // locationUtils = LocationUtils(
          //   isShowConfirmDialog: false,
          //   navigationService: myAppointmentsViewModel.navigationService,
          //   appState: myAppointmentsViewModel.appState,
          // );
          locationUtils.getCurrentLocation(onSuccess: (value) {
            projectDetailListModel = Utils.getProjectDetailObj(appState, projectID);
            double dist = Utils.distance(value.latitude, value.longitude, double.parse(projectDetailListModel.latitude!), double.parse(projectDetailListModel.longitude!)).ceilToDouble() * 1000;
            print(dist);
            if (dist <= projectDetailListModel.geofenceRadius!) {
              sendCheckInRequest(projectDetailListModel.checkInQrCode!, context);
            } else {
              showCommonBottomSheetWithoutHeight(context,
                  title: LocaleKeys.error.tr(context: context),
                  child: Utils.getErrorWidget(loadingText: LocaleKeys.ensureWithinHospitalLocation.tr(context: context),), callBackFunc: () {
                    Navigator.of(context).pop();
                  }, isFullScreen: false);
            }
          });
        }),
        // SizedBox(height: 16.h),
        // checkInOptionCard(
        //   AppAssets.checkin_nfc_icon,
        //   LocaleKeys.nfcNearFieldCommunication.tr(context: context),
        //   LocaleKeys.scanPhoneViaNFC.tr(context: context),
        // ).onPress(() {
        //   Future.delayed(const Duration(milliseconds: 500), () {
        //     showNfcReader(context, onNcfScan: (String nfcId) {
        //       Future.delayed(const Duration(milliseconds: 100), () {
        //         sendCheckInRequest(nfcId, context);
        //       });
        //     }, onCancel: () {});
        //   });
        // }),
        SizedBox(height: 16.h),
        checkInOptionCard(
          AppAssets.checkin_qr_icon,
          LocaleKeys.qrCode.tr(context: context),
          LocaleKeys.scanQRCodeToCheckIn.tr(context: context),
        ).onPress(() async {
          String onlineCheckInQRCode = (await BarcodeScanner.scan().then((value) => value.rawContent));
          if (onlineCheckInQRCode != "") {
            sendCheckInRequest(onlineCheckInQRCode, context);
          } else {}
        }),
      ],
    );
  }

  Widget checkInOptionCard(String icon, String title, String subTitle) {
    return Container(
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
        color: AppColors.whiteColor,
        borderRadius: 20.h,
        hasShadow: false,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Utils.buildSvgWithAssets(icon: icon, width: 40.h, height: 40.h, fit: BoxFit.fill),
          SizedBox(height: 16.h),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    title.toText16(isBold: true, color: AppColors.textColor),
                    subTitle.toText12(isBold: true, color: AppColors.greyTextColor),
                  ],
                ),
              ),
              Transform.flip(
                flipX: appState.isArabic(),
                child: Utils.buildSvgWithAssets(
                  icon: AppAssets.forward_arrow_icon_small,
                  iconColor: AppColors.blackColor,
                  width: 18.h,
                  height: 13.h,
                  fit: BoxFit.contain,
                ),
              ),
            ],
          ),
        ],
      ).paddingAll(16.h),
    );
  }

  void sendCheckInRequest(String scannedCode, BuildContext context) async {
    showCommonBottomSheet(context,
        child: Utils.getLoadingWidget(), callBackFunc: (str) {}, title: "", height: ResponsiveExtension.screenHeight * 0.3, isCloseButtonVisible: false, isDismissible: false, isFullScreen: false);
    // await myAppointmentsViewModel.sendCheckInNfcRequest(
    //   patientAppointmentHistoryResponseModel: patientAppointmentHistoryResponseModel,
    //   scannedCode: scannedCode,
    //   checkInType: 2,
    //   onSuccess: (apiResponse) {
    //     Navigator.of(context).pop();
    //     showCommonBottomSheetWithoutHeight(context, title: "Success".needTranslation, child: Utils.getSuccessWidget(loadingText: LocaleKeys.success.tr()), callBackFunc: () {
    //       Navigator.of(context).pop();
    //       Navigator.pushAndRemoveUntil(
    //           context,
    //           CustomPageRoute(
    //             page: LandingNavigation(),
    //           ),
    //               (r) => false);
    //       Navigator.of(context).push(
    //         CustomPageRoute(page: MyAppointmentsPage()),
    //       );
    //     }, isFullScreen: false);
    //   },
    //   onError: (error) {
    //     Navigator.of(context).pop();
    //     showCommonBottomSheetWithoutHeight(context, title: "Error".needTranslation, child: Utils.getErrorWidget(loadingText: error), callBackFunc: () {
    //       Navigator.of(context).pop();
    //     }, isFullScreen: false);
    //   },
    // );
  }
}
