import 'package:aman_kassa_flutter/core/route_names.dart';
import 'package:aman_kassa_flutter/redux/actions/setting_actions.dart';
import 'package:aman_kassa_flutter/redux/store.dart';
import 'package:flutter/material.dart';
import 'package:flutter_lock_screen/flutter_lock_screen.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:aman_kassa_flutter/core/locator.dart';
import 'package:aman_kassa_flutter/core/services/navigator_service.dart';

class PassCodeScreen extends StatefulWidget {
  PassCodeScreen({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _PassCodeScreenState createState() => new _PassCodeScreenState();
}

class _PassCodeScreenState extends State<PassCodeScreen> {
  // bool isFingerprint = false;
  NavigatorService _navigatorService = locator<NavigatorService>();
  final backgroundedTimeKey = 'backgroundedTimeKey';


  // Future<Null> biometrics() async {
  //   final LocalAuthentication auth = new LocalAuthentication();
  //   bool authenticated = false;
  //
  //   try {
  //     authenticated = await auth.authenticateWithBiometrics(
  //         localizedReason: 'Scan your fingerprint to authenticate',
  //         useErrorDialogs: true,
  //         stickyAuth: false);
  //   } on PlatformException catch (e) {
  //     print(e);
  //   }
  //   if (!mounted) return;
  //   if (authenticated) {
  //     setState(() {
  //       isFingerprint = true;
  //     });
  //   }
  // }

  @override
  Widget build(BuildContext context) {
    var myPass = [];
    String _pinCode = Redux.store!.state.settingState!.pinCode!;
    for (var i = 0; i < _pinCode.length; i++) {
      myPass.add(int.parse(_pinCode[i]));
    }
    return LockScreen(
        title: "Введите ПИН-код",
        passLength: myPass.length,
        bgImage: "assets/images/secBg.jpg",
        // fingerPrintImage: "assets/images/finger.png",
        // showFingerPass: true,
        // fingerFunction: biometrics,
        // fingerVerify: isFingerprint,
        borderColor: Colors.white,
        showWrongPassDialog: true,
        wrongPassContent: "Неверный код, повторите попытку",
        wrongPassTitle: "Aman Kassa",
        wrongPassCancelButtonText: "Отмена",
        passCodeVerify: (passcode) async {
          for (int i = 0; i < myPass.length; i++) {
            if (passcode[i] != myPass[i]) {
              return false;
            }
          }

          return true;
        },
        onSuccess: () {
          Redux.store!.dispatch(changePinLockedFromSetting(false));
          _navigatorService.replace(HomeViewRoute);
        });
  }
}