import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:myhr_facescan/models/face_template.dart';
import 'package:myhr_facescan/models/template.dart';
import 'package:myhr_facescan_example/main.dart';
import 'package:myhr_facescan_example/template_controller.dart';

class FinishPage extends StatelessWidget {
  const FinishPage({super.key, required this.templates});

  final List<TemplateModel> templates;

  @override
  Widget build(BuildContext context) {
    var con = Get.put(Controller(templates: templates));

    return Scaffold(
        appBar: AppBar(
          title: const Text('Finish enroll'),
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              width: 72,
              height: 72,
              margin: const EdgeInsets.only(right: 8),
              decoration: BoxDecoration(
                color: const Color(0xff7c94b6),
                image: DecorationImage(
                  image: templates[0].image.image,
                  fit: BoxFit.cover,
                ),
                borderRadius: const BorderRadius.all(Radius.circular(50.0)),
              ),
            ),
            TextFormField(
              controller: con.tConName,
              decoration: const InputDecoration(hintText: 'Face Name'),
            ),
            ElevatedButton(onPressed: con.save, child: const Text('Save'))
          ],
        ));
  }
}

class Controller extends GetxController {
  Controller({required this.templates});

  var tConName = TextEditingController();

  final List<TemplateModel> templates;

  save() {
    var con = Get.find<TemplateController>();

    con.add(FaceTemplate(
        id: tConName.text, eye: templates[0].eye, face: templates[0].face));

    Get.snackbar('Success', 'Face & Eye tempalte saved in device.');
    Get.offAll(const HomePage());
  }
}
