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/match_template/controller.dart';
import 'package:myhr_facescan/models/face_template.dart';
import 'package:myhr_facescan/models/template.dart';
import 'package:myhr_facescan/rectangle_paint.dart';

class MatchTemplateScanner extends StatelessWidget {
  const MatchTemplateScanner({
    super.key,
    required this.licenseKey,
    required this.onFinish,
    this.imageStreamMilliSecond = 100,
    required this.templates,
  });

  final String licenseKey;
  final void Function(bool, TemplateModel?) onFinish;
  final int imageStreamMilliSecond;
  final List<FaceTemplate> templates;

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

    return Obx(() => con.isCameraReady.value
        ? Transform.scale(
            scale: con.previewScale,
            child: CameraPreview(
              con.camController!,
              child: CustomPaint(
                size: Size(con.camController!.value.previewSize!.height,
                    con.camController!.value.previewSize!.width),
                painter: RectanglePainter(
                    positions: con.faceMatchs.map((d) => d.position).toList(),
                    paperWidth: con.camController!.value.previewSize!.height,
                    paperHeight: con.camController!.value.previewSize!.width,
                    topMessage: con.faceMatchs
                        .map((d) => '${d.id}, similarity=${d.similarityFace}')
                        .toList(),
                    rectColor: Colors.white,
                    rectStrokeWidth: 2),
              ),
            ),
          )
        : const CameraLoading());
  }
}
