import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:traderscircuit/Utils/Common/CommonAppbar.dart';
import 'package:traderscircuit/Utils/Common/commonBotton.dart';
import 'package:traderscircuit/Utils/base_manager.dart';
import 'package:traderscircuit/Utils/text.dart';
import 'package:traderscircuit/Utils/utils.dart';
import 'package:traderscircuit/resources/routes/route_name.dart';
import 'package:traderscircuit/view/onBoarding/splashScreen1.dart';
import 'package:local_auth/local_auth.dart';
import 'package:traderscircuit/view_model/Login/secure_access_api.dart';

class Fingerprint extends StatefulWidget {
  const Fingerprint({super.key});

  @override
  State<Fingerprint> createState() => _FingerprintState();
}

class _FingerprintState extends State<Fingerprint> {
  Color primaryColor = Colors.transparent.withOpacity(0.2);
  Color secondaryColor = Colors.grey.shade800;

  bool? _hasBioSensor;
  LocalAuthentication authentication = LocalAuthentication();

  Future<void> _checkBio() async {
    try {
      _hasBioSensor = await authentication.canCheckBiometrics;
      // print(_hasBioSensor);
      if (_hasBioSensor!) {
        _getAuth();
      }
      // ignore: empty_catches
    } catch (e) {}
  }

  Future<void> _getAuth() async {
    bool isAuth = false;
    try {
      isAuth = await authentication.authenticate(
        localizedReason: 'localizedReason',
        options: const AuthenticationOptions(
          // biometricOnly: true,
          useErrorDialogs: true,
          stickyAuth: true,
        ),
      );
      if (isAuth) {
        Utils.loader();
        Map<String, dynamic> updata = {};
        final resp = await SecureAccessAPI(updata).secureaccessApi();
        if (resp.status == ResponseStatus.SUCCESS) {
          Get.back();
          Get.toNamed(RouteName.adddetails);
        } else if (resp.status == ResponseStatus.PRIVATE) {
          Get.back();
          String? message = resp.data['message'];
          Utils.showToast("$message");
        } else if (resp.status == ResponseStatus.ERROR) {
          Get.back();
          String? message = resp.data['message'];
          Utils.showToast("$message");
        } else {
          Get.back();
          String? message = resp.data['message'];
          Utils.showToast("$message");
        }

        // Get.toNamed(RouteName.adddetails);
      } else {
        Utils.showToast("Setting Fingerprint Failed!");
      }
      print(isAuth);
      // ignore: empty_catches
    } catch (e) {}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CommonAppbar(titleTxt: "Secure your access"),
      backgroundColor: Colors.black,
      extendBody: true,
      body: Stack(
        children: [
          CommonBlurLeft(),
          CommonBlurRight(),
          Stack(
            children: [
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 16, vertical: 16),
                child: ListView(
                  physics: BouncingScrollPhysics(),
                  // mainAxisAlignment: MainAxisAlignment.start,
                  // crossAxisAlignment: CrossAxisAlignment.start,

                  children: [
                    text18W400("Quickly set up Fingerprint for secure access."),
                    SizedBox(
                      height: 180.h,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Image.asset("assets/images/png/fingerprint.png"),
                      ],
                    ),
                    SizedBox(
                      height: 180.h,
                    ),
                    CommonBtn(
                      text: "Setup",
                      onTap: () {
                        _checkBio();
                        //  Get.toNamed(RouteName.adddetails);
                      },
                    )
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
