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

class EnrollFace extends StatelessWidget {
  const EnrollFace(
      {super.key,
      required this.licenseKey,
      required this.onFinish,
      this.templateCount = 3,
      this.topMessage = 'Please touch screen',
      this.imageStreamMilliSecond = 100});

  final String licenseKey;
  final void Function(List<TemplateModel>) onFinish;
  final int templateCount;
  final String topMessage;
  final int imageStreamMilliSecond;

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

    return Obx(() => con.isCameraReady.value
        ? GestureDetector(
            onTap: con.getCurrentImage,
            child: Transform.scale(
              scale: con.previewScale,
              child: CameraPreview(
                con.camController!,
                child: Stack(
                  children: [
                    con.isFoundFace.value
                        ? CustomPaint(
                            size: Size(
                                con.camController!.value.previewSize!.height,
                                con.camController!.value.previewSize!.width),
                            painter: RectanglePainter(
                                positions: [con.facePosition.value],
                                paperWidth: con
                                    .camController!.value.previewSize!.height,
                                paperHeight:
                                    con.camController!.value.previewSize!.width,
                                topMessage: [topMessage]),
                          )
                        : Container(),
                    Positioned.fill(
                        bottom: 24,
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.end,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            ...con.templates.map((e) => Avatar(image: e.image)),
                          ],
                        ))
                  ],
                ),
              ),
            ),
          )
        : const CameraLoading());
  }
}
