import 'dart:math';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flyinsono/lab/color/lab_light_colors.dart';
import 'package:flyinsono/lab/pages/lab_image/urm_data_processor/urm_enums.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_trace/controller.dart';
import 'package:get/get.dart';

class TraceCanvasLayer extends GetView<ImageTraceOverlayController> {
  @override
  String? get tag => tabHashCode;

  final String tabHashCode;

  const TraceCanvasLayer({
    Key? key,
    required this.tabHashCode,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ImageTraceOverlayController>(
      tag: tabHashCode,
      id: ImageTraceOverlayController.imageTraceCanavsId,
      builder: (controller) {
        return ClipRect(
          child: SizedBox.expand(
            child: RepaintBoundary(
              child: CustomPaint(
                painter: _TracePainter(
                  roiViewType: controller.isSyncDisplay
                      ? controller.roiViewType
                      : RoiViewType.nomal,
                  points: controller.points,
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}

class _TracePainter extends CustomPainter {
  final RoiViewType roiViewType;
  final List<Point<double>> points;
  _TracePainter({
    this.roiViewType = RoiViewType.nomal,
    this.points = const [],
  });

  @override
  void paint(Canvas canvas, Size size) {
    switch (roiViewType) {
      case RoiViewType.nomal:
        paintNomalRoi(canvas, size);
        break;
      case RoiViewType.leftRightHalfHalf:
        paintLeftRightHalfHalfRoi(canvas, size);
        break;
    }
  }

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

  void paintLeftRightHalfHalfRoi(Canvas canvas, Size size) {
    if (points.isEmpty) {
      return;
    }
    final paint = Paint()
      ..color = LabColorConfig.base100
      ..strokeWidth = 6.0
      ..strokeCap = StrokeCap.round; // 设置点的样式为圆形

    final pointMode = PointMode.points;

    final leftPoints = points
        .map((point) => Offset(point.x * size.width / 2, point.y * size.height))
        .toList();
    final rightPoints = points
        .map((point) => Offset(
            point.x * size.width / 2 + size.width / 2, point.y * size.height))
        .toList();

    canvas.drawPoints(pointMode, leftPoints, paint);
    canvas.drawPoints(pointMode, rightPoints, paint);
  }

  void paintNomalRoi(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = LabColorConfig.base100
      ..strokeWidth = 6.0
      ..strokeCap = StrokeCap.round; // 设置点的样式为圆形

    final pointMode = PointMode.points;
    final offsetPoints = points
        .map((point) => Offset(point.x * size.width, point.y * size.height))
        .toList();

    canvas.drawPoints(pointMode, offsetPoints, paint);
  }
}
