import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:tanami_app/core/utils/secure/secure_storage_service.dart';

import '../../../../core/routes/route_name.dart';
import '../../../../core/routes/routes.dart';
import '../../../../core/styles/app_images.dart';
import '../../../../shared/components/device_locked_dialog.dart';
import '../../bloc/biometric_bloc.dart';
import '../../bloc/biometric_event.dart';
import '../../bloc/biometric_state.dart';

class BiometricLayout extends StatelessWidget {
  const BiometricLayout({super.key});

  @override
  Widget build(BuildContext context) {
    final SecureStorageService secureStorageService = SecureStorageService();
    String biometricImage = "";
    if (Platform.isIOS) {
      biometricImage = AppImages.biometricFace;
    } else {
      biometricImage = AppImages.biometricFingerprint;
    }
    return Scaffold(
      backgroundColor: Colors.white,
      body: BlocConsumer<BiometricBloc, BiometricState>(
        listener: (context, state) async {
          if (state is BiometricChecked && state.canAuthenticate) {
            context.read<BiometricBloc>().add(AuthenticateBiometricEvent());
          } else if (state is BiometricFailed) {
            deviceLockedDialog(context);
          } else if (state is BiometricAuthenticated) {
            await secureStorageService.write('isLoginedIn', "true");
            goRouter.goNamed(RouteName.mainScreen);
          }
        },
        builder: (context, state) {
          return SizedBox(
            width: 1.sw,
            height: 1.sh,
            child: Stack(
              children: [
                Positioned.fill(
                  child: SvgPicture.asset(
                    height: 1.sh,
                    width: 1.sw,
                    AppImages.biometricBg,
                    fit: BoxFit.cover,
                  ),
                ),
                Positioned.fill(
                  child: Align(
                    alignment: Alignment.center,
                    child: Image.asset(
                      biometricImage,
                      width: 133,
                      height: 155,
                    ),
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}
