import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gap/gap.dart';
import 'package:tanami_app/Api_Helper/base_manager.dart';
import 'package:tanami_app/core/styles/app_color.dart';
import 'package:tanami_app/core/utils/device_info/device_info_data.dart';
import 'package:tanami_app/shared/components/loader.dart';

import '../../core/routes/route_name.dart';
import '../../core/routes/routes.dart';
import '../../core/styles/app_text.dart';
import '../../core/utils/language/localizations_delegate.dart';
import '../../core/utils/secure/secure_storage_service.dart';
import '../../features/register/Repository/RegisterApi.dart';
import 'text_widget.dart';
import 'toast_message.dart';

permissionDialog(
  context,
) {
  var localizations = AppLocalizations.of(context);
  final SecureStorageService secureStorageService = SecureStorageService();

  return showDialog(
    barrierDismissible: true,
    context: context,
    builder: (context) => WillPopScope(
      onWillPop: () async {
        return true;
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          AlertDialog(
            contentPadding: const EdgeInsets.all(0),
            // insetPadding: const EdgeInsets.all(0),
            backgroundColor: AppColor.plainWhite,
            shape: const RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(14)),
              side: BorderSide(
                color: AppColor.plainWhite,
              ),
            ),
            content: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 16),
                  child: Column(
                    children: [
                      const Gap(20),
                      Align(
                        alignment: Alignment.center,
                        child: Container(
                          margin: const EdgeInsets.symmetric(horizontal: 30),
                          child: TextWidget().text17W600(
                            localizations.translate(AppText.biometricText),
                            clr: AppColor.plainBlack,
                          ),
                        ),
                      ),
                      const Gap(5),
                      Align(
                        alignment: Alignment.center,
                        child: Container(
                          //  margin: const EdgeInsets.symmetric(horizontal: 30),
                          child: TextWidget().text13W400(
                            localizations
                                .translate(AppText.appAskPermissionText),
                            txtAlign: TextAlign.center,
                            clr: AppColor.plainBlack,
                          ),
                        ),
                      ),
                      Gap(21.h),
                    ],
                  ),
                ),
                Container(
                  width: 1.sw,
                  height: 1,
                  decoration: const BoxDecoration(color: Color(0x5B3C3C43)),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    GestureDetector(
                      onTap: () async {
                        Loader.loader(context);
                        Map<String, dynamic> biometricdata = {
                          "code": await secureStorageService.read("temp_token"),
                          "is_2FA_on": false,
                          "deviceId": await DeviceInfoData().getDeviceId(),
                          "biometric_type":
                              Platform.isIOS ? "face" : "fingerprint"
                        };

                        ResponseData response = await RegisterAPIService()
                            .BiometricUpdate(biometricdata);
                        if (response.status == ResponseStatus.SUCCESS) {
                          goRouter.pop();

                          await secureStorageService.write("biometric", 'of');

                          goRouter.pop();

                          goRouter
                              .goNamed(RouteName.pinScreen, pathParameters: {
                            "fromScreen": "register",
                          });
                        }
                      },
                      child: SizedBox(
                        height: 48.h,
                        width: 140.w,
                        child: Center(
                          child: TextWidget().text17W600(
                            localizations.translate(AppText.declineText),
                            clr: AppColor.permissionTextClr,
                          ),
                        ),
                      ),
                    ),
                    Container(
                      width: 1,
                      height: 50,
                      decoration: const BoxDecoration(color: Color(0x5B3C3C43)),
                    ),
                    GestureDetector(
                      onTap: () async {
                        Loader.loader(context);
                        Map<String, dynamic> biometricdata = {
                          "code": await secureStorageService.read("temp_token"),
                          "is_2FA_on": true,
                          "deviceId": await DeviceInfoData().getDeviceId(),
                          "biometric_type":
                              Platform.isIOS ? "face" : "fingerprint"
                        };

                        ResponseData response = await RegisterAPIService()
                            .BiometricUpdate(biometricdata);
                        if (response.status == ResponseStatus.SUCCESS) {
                          goRouter.pop();

                          await secureStorageService.write("biometric", 'on');
                          successToastMessage(context,
                              "Biometric/Face Id Enabled Sucessfully !");

                          goRouter.pop();

                          goRouter
                              .goNamed(RouteName.pinScreen, pathParameters: {
                            "fromScreen": "register",
                          });
                        }
                      },
                      child: SizedBox(
                        height: 48.h,
                        width: 140.w,
                        child: Center(
                          child: TextWidget().text17W600(
                            localizations.translate(AppText.allowText),
                            clr: AppColor.permissionTextClr,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    ),
  );
}
