import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'index.dart';
import 'widgets/widgets.dart';

class FacialRecognitionPage extends GetView<FacialRecognitionController> {
  const FacialRecognitionPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FacialRecognitionController>(
      init: FacialRecognitionController(),
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: const Text("人脸识别")),
          body: SafeArea(
            child: Obx(
              () => controller.state.isCameraReady
                  ? _buildCameraArea()
                  : const Center(
                      child: CircularProgressIndicator(
                        valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
                      ),
                    ),
            ),
          ),
        );
      },
    );
  }

  Widget _buildCameraArea() {
    return Row(
      children: [
        Expanded(
          child: ClipRRect(
            child: LayoutBuilder(builder: (context, constraints) {
              return Stack(
                children: <Widget>[
                  OverflowBox(
                    maxHeight: constraints.maxHeight,
                    maxWidth: 2000,
                    child: Container(
                      color: Colors.black,
                      child: Center(
                        child: _cameraPreviewWidget(),
                      ),
                    ),
                  ),
                  const Center(child: CameraForFace()),
                  Align(
                    alignment: Alignment.bottomRight,
                    child: _switchCameraLens(),
                  ),
                ],
              );
            }),
          ),
        ),
      ],
    );
  }

  /// 相机预览
  Widget _cameraPreviewWidget() {
    final CameraController? cameraController = controller.kCameraController;
    if (cameraController == null || !cameraController.value.isInitialized) {
      /// 旋转loading
      return const Center(
        child: CircularProgressIndicator(),
      );
    } else {
      return Listener(
        onPointerDown: (_) => controller.pointers++,
        onPointerUp: (_) => controller.pointers--,
        child: CameraPreview(
          cameraController,
          child: LayoutBuilder(
            builder: (BuildContext context, BoxConstraints constraints) {
              return GestureDetector(
                behavior: HitTestBehavior.opaque,
                onScaleStart: controller.handleScaleStart,
                onScaleUpdate: controller.handleScaleUpdate,
                onTapDown: (TapDownDetails details) =>
                    controller.onViewFinderTap(details, constraints),
                child: const FaceBoundingBox(),
              );
            },
          ),
        ),
      );
    }
  }

  Widget _switchCameraLens() {
    return Obx(
      () => GestureDetector(
        onTap: () {
          if (controller.state.isUsingFrontCamera) {
            controller.openBackCamera();
          } else {
            controller.openFrontCamera();
          }
        },
        child: Container(
          padding: const EdgeInsets.all(10.0),
          // padding: const EdgeInsets.all(10),
          decoration: BoxDecoration(
            color: Colors.black.withOpacity(0.2),
            borderRadius: BorderRadius.circular(10),
          ),
          margin: const EdgeInsets.symmetric(horizontal: 40.0, vertical: 20.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const Icon(
                Icons.camera_rear,
                size: 30,
                color: Colors.white,
              ),
              const SizedBox(height: 10),
              Text(
                controller.state.isUsingFrontCamera ? "切换为后置" : "切换为前置",
                style: const TextStyle(color: Colors.white),
              )
            ],
          ),
        ),
      ),
    );
  }
}
