import 'dart:io';

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:flutter_camera_overlay_new/flutter_camera_overlay.dart';
import 'package:flutter_camera_overlay_new/model.dart';
import 'package:get/get.dart';
import 'package:uco_mobile_poc/app/core/utils/logs_utils.dart';

import '../../controllers/sign_up_screen_step3_controller.dart';

class CameraOverlayScreen extends StatelessWidget {
  final bool isFront;
  final signDocTypeScreen docType;

  CameraOverlayScreen({super.key, required this.isFront, required this.docType});

  SignUpScreenStep3Controller controller = Get.find<SignUpScreenStep3Controller>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: FutureBuilder<List<CameraDescription>?>(
        future: availableCameras(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            if (snapshot.data == null) {
              return const Align(
                  alignment: Alignment.center,
                  child: Text(
                    'No camera found',
                    style: TextStyle(color: Colors.black),
                  ));
            }

            return CameraOverlay(
              snapshot.data!.first,
              CardOverlay.byFormat(OverlayFormat.cardID1),
                  (XFile file) {
                showDialog(
                  context: context,
                  barrierColor: Colors.black,
                  builder: (context) {
                    CardOverlay overlay = CardOverlay.byFormat(OverlayFormat.cardID1);
                    return AlertDialog(
                      actionsAlignment: MainAxisAlignment.center,
                      backgroundColor: Colors.black,
                      title: const Text('Capture', style: TextStyle(color: Colors.white), textAlign: TextAlign.center),
                      actions: [
                        OutlinedButton(onPressed: () => Navigator.of(context).pop(), child: const Icon(Icons.close)),
                        OutlinedButton(
                            onPressed: () {
                              Get.put(SignUpScreenStep3Controller()).updateDocs(file.path, isFront, docType);
                              Navigator.of(context).pop();
                              Navigator.pop(context);
                            },
                            child: const Icon(Icons.done)),
                      ],
                      content: SizedBox(
                        width: double.infinity,
                        child: AspectRatio(
                          aspectRatio: 1.5,
                          child: Container(
                            decoration: BoxDecoration(
                                image: DecorationImage(
                                  fit: BoxFit.fitWidth,
                                  alignment: FractionalOffset.center,
                                  image: FileImage(
                                    File(file.path),
                                  ),
                                )),
                          ),
                        ),
                      ),
                    );
                  },
                );
              },
              info: 'Position your ID card within the rectangle and ensure the image is perfectly readable.',
              label: 'Scanning ID Card',
            );
          } else {
            return const Align(
                alignment: Alignment.center,
                child: Text(
                  'Fetching cameras',
                  style: TextStyle(color: Colors.black),
                ));
          }
        },
      ),
    );
  }
}
