// ZOOM 框 Painter
import 'dart:ui';

import 'package:flutter/material.dart' hide Image;
import 'package:flyinsono/lab/color/lab_colors.dart';

class ZoomPluginPainter extends CustomPainter {
  double zoomScale = 1.0;
  Offset zoomCenter = Offset(0, 0);
  ZoomPluginPainter(
      {this.zoomScale = 1.0, this.zoomCenter = const Offset(0, 0)});

  @override
  void paint(Canvas canvas, Size size) {
    // 根据 zoomScale 和 zoomCenter 绘制 zoom 框

    final paint = Paint()
      ..color = LabColors.green400
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2.0;

    final rect = Rect.fromCenter(
      center: Offset(size.width * zoomCenter.dx, size.height * zoomCenter.dy),
      width: size.width / zoomScale,
      height: size.height / zoomScale,
    );

    canvas.drawRect(rect, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}

/// zoom 背景绘制器
class ZoomBackgroundPainter extends CustomPainter {
  final Image? backgroundImage;
  final Image? urmPreviewImage;
  final Rect coordinateRect;
  final bool isShowBlackBackground;

  ZoomBackgroundPainter({
    required this.coordinateRect,
    required this.isShowBlackBackground,
    this.backgroundImage,
    this.urmPreviewImage,
  });

  @override
  void paint(Canvas canvas, Size size) {
    if (backgroundImage == null) {
      return;
    }
    final paint = Paint();
    final rect = Rect.fromLTWH(0, 0, size.width, size.height);
    if (isShowBlackBackground) {
      final paint = Paint()..color = Colors.black;
      canvas.drawRect(Rect.fromLTWH(0, 0, size.width, size.height), paint);
    } else {
      final imgRect = Rect.fromLTWH(
        coordinateRect.left * backgroundImage!.width,
        coordinateRect.top * backgroundImage!.height,
        coordinateRect.width * backgroundImage!.width,
        coordinateRect.height * backgroundImage!.height,
      );
      canvas.drawImageRect(backgroundImage!, imgRect, rect, paint);
    }
    if (urmPreviewImage != null) {
      final urmRect = Rect.fromLTWH(0, 0, urmPreviewImage!.width.toDouble(),
          urmPreviewImage!.height.toDouble());
      canvas.drawImageRect(urmPreviewImage!, urmRect, rect, paint);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}
