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

// import 'package:video_player/video_player.dart';

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

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

  Widget _buildCameraArea() {
    return Row(
      children: [
        const IdCardInfo(),
        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(),
                      ),
                    ),
                  ),
                  Center(
                    child: Stack(
                      children: [
                        Align(
                          alignment: Alignment.centerLeft,
                          child: _idCardInfoSwitch(),
                        ),
                        if (controller.state.isInIdCardScan)
                          const CameraForFace()
                        else
                          const CameraForIdCard(),
                      ],
                    ),
                  ),
                ],
              );
            }),
          ),
        ),
      ],
    );
  }

  /// 相机预览
  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 _idCardInfoSwitch() {
    return Obx(
      () {
        if (!controller.state.isShowIdCardInfoSwitch) {
          return Container();
        }
        return Container(
          height: 80,
          width: 40,
          color: Colors.black.withOpacity(0.5),
          child: IconButton(
            onPressed: () {
              controller.state.isIdCardInfoShow =
                  !controller.state.isIdCardInfoShow;
            },
            padding: const EdgeInsets.all(0),
            icon: Icon(
                controller.state.isIdCardInfoShow
                    ? Icons.keyboard_double_arrow_left
                    : Icons.keyboard_double_arrow_right,
                size: 30),
            color: Colors.white,
          ),
        );
      },
    );
  }
}
