import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_zoom_preview/widgets/zoom_plugin_painter.dart';
import 'package:get/get.dart';

import 'index.dart';

class ImageZoomPreview extends GetView<ImageZoomPreviewController> {
  const ImageZoomPreview({
    Key? key,
    required this.initController,
    required this.tabHashCode,
  }) : super(key: key);

  final String tabHashCode;
  final ImageZoomPreviewController? initController;

  @override
  String? get tag => tabHashCode;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ImageZoomPreviewController>(
      tag: tabHashCode,
      init: initController ?? ImageZoomPreviewController(),
      id: ImageZoomPreviewController.imageZoomPreviewId,
      builder: (_) {
        if (!controller.isZoomEnabled) {
          return SizedBox.shrink();
        }
        return GestureDetector(
          onPanUpdate: (details) {
            controller.updateZoomCenter(details);
          },
          onPanEnd: (details) {
            controller.updateZoomCenterFinish();
          },
          child: Align(
            alignment: Alignment.bottomLeft,
            child: Container(
              constraints: BoxConstraints(
                maxWidth: controller.maxPreviewSize,
                maxHeight: controller.maxPreviewSize,
              ),
              margin: const EdgeInsets.only(left: 10),
              decoration: BoxDecoration(
                color: Colors.black.withOpacity(0.9),
                border: Border.all(
                  color: LabColors.base200,
                  width: 1,
                ),
              ),
              child: MouseRegion(
                cursor: SystemMouseCursors.move,
                child: ClipRect(
                  child: AspectRatio(
                    aspectRatio: controller.previewAspectRatio,
                    child: Stack(
                      children: [
                        Center(
                          child: SizedBox.expand(
                            child: CustomPaint(
                              foregroundPainter: ZoomPluginPainter(
                                zoomScale: controller.scale,
                                zoomCenter: controller.zoomCenter,
                              ),
                              painter: ZoomBackgroundPainter(
                                coordinateRect: controller.bgCoordinateRect,
                                backgroundImage: controller.backgroundImage,
                                urmPreviewImage: controller.urmPreviewImage,
                                isShowBlackBackground:
                                    controller.isShowBlackBackground,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
