// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_measure_overlay/controller.dart';
import 'package:fis_measure/interfaces/process/items/item_feature.dart';
import 'package:get/get.dart';
import 'package:fis_measure/process/workspace/urm/application.dart';
import 'package:fis_measure/process/primitives/trace.dart';
import 'package:fis_measure/process/primitives/ellipse.dart';
import 'package:fis_measure/process/primitives/urm_measure/urm_rect_measure.dart';

class RecordCanvasLayer extends GetView<ImageMeasureOverlayController> {
  RecordCanvasLayer({
    Key? key,
    required this.tabHashCode,
    this.isURMImage = false,
  }) : super(key: key);

  @override
  String? get tag => tabHashCode;

  final String tabHashCode;

  final bool isURMImage;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ImageMeasureOverlayController>(
      tag: tabHashCode,
      id: ImageMeasureOverlayController.recordCanvasLayerId,
      builder: (_) {
        if (controller.app == null) return const SizedBox();
        final List<IMeasureItemFeature> measureFeatures = controller
            .app!.measureItems
            .expand((item) => item.measuredFeatures)
            .toList();

        final hasNoData = measureFeatures.isEmpty;

        return LayoutBuilder(
          builder: (context, constraints) {
            controller.updateImageWidth(constraints.maxWidth);
            if (hasNoData) {
              return const SizedBox();
            }
            // 如果 urm app 下需要判断是否需要对 measureFeatures 执行 视图区域的 zoom
            if (controller.app! is URMApplication) {
              Rect? zoomRect = (controller.app as URMApplication)
                  .urmDataProcessor
                  .measureAreaInFullVisual;
              measureFeatures.forEach((feature) {
                feature.setZoomRect(zoomRect);
              });
            }
            return ClipRect(
              child: SizedBox(
                width: constraints.maxWidth,
                height: constraints.maxHeight,
                child: RepaintBoundary(
                  child: CustomPaint(
                    painter: _PanelPainter(
                      measureFeatures,
                      isURMImage,
                    ),
                  ),
                ),
              ),
            );
          },
        );
      },
    );
  }
}

class _PanelPainter extends CustomPainter {
  final List<IMeasureItemFeature> measureFeatures;

  /// 是否是URM 图
  final bool isURMImage;
  _PanelPainter(
    this.measureFeatures,
    this.isURMImage,
  );

  @override
  void paint(Canvas canvas, Size size) {
    for (final feature in measureFeatures) {
      if (feature.checkCanPaint()) {
        /// 灌注图的绘制
        if (isURMImage &&
            (feature is TraceFeature ||
                feature is EllipseFeature ||
                feature is URMRectFeature)) {
          feature.paintPerfusion(canvas, size);
        }
        feature.paint(canvas, size);
      }
    }
  }

  @override
  bool shouldRepaint(covariant _PanelPainter oldDelegate) {
    return oldDelegate.hashCode != hashCode;
  }
}
