import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:myhr_facescan/active_liveness/controller.dart';
import 'package:myhr_facescan/camera_loading.dart';
import 'package:myhr_facescan/models/template.dart';

class ActiveLivenessVerifier extends StatelessWidget {
  const ActiveLivenessVerifier(
      {super.key,
      required this.licenseKey,
      required this.onFinish,
      this.imageStreamMilliSecond = 200,
      this.failAcceptableInSecond = 3});

  final String licenseKey;
  final void Function(bool, TemplateModel?) onFinish;
  final int imageStreamMilliSecond;
  final int failAcceptableInSecond;

  @override
  Widget build(BuildContext context) {
    var con = Get.put(ActiveLivenessController(
      licenseKey: licenseKey,
      onFinish: onFinish,
      imageStreamMilliSecond: imageStreamMilliSecond,
      failAcceptableInSecond: failAcceptableInSecond,
    ));

    return Obx(() => con.isCameraReady.value
        ? Transform.scale(
            scale: con.previewScale,
            child: CameraPreview(
              con.camController!,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Container(
                    margin: const EdgeInsets.only(bottom: 24),
                    padding: const EdgeInsets.all(8),
                    width: double.infinity,
                    decoration:
                        BoxDecoration(color: Colors.black.withOpacity(0.3)),
                    child: Center(
                      child: Text(
                          con.isWaitNextCommand.value ? '' : con.commandName,
                          style: context.textTheme.headlineMedium!.copyWith(
                            color: Colors.white,
                          )),
                    ),
                  )
                ],
              ),
            ),
          )
        : const CameraLoading());
  }
}
