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

/// ## 多功能帧指示器
///
/// 用于提示 AI 关键帧、测量值关键帧、起始帧、结束帧等
class FrameIndicator extends StatelessWidget {
  const FrameIndicator({
    super.key,
    required this.totalFrames,
    this.startFrame,
    this.endFrame,
    this.measureKeyFrame = const {},
    this.horizontalPadding = 10,
  });

  final double horizontalPadding; // 用于对齐进度条所需的左右间距
  final int totalFrames; // 总帧数
  final int? startFrame; // 起始帧
  final int? endFrame; // 结束帧
  final Set<int> measureKeyFrame; // 测量值关键帧

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      child: Container(
        child: SizedBox.expand(
          child: CustomPaint(
            painter: FrameIndicatorPainter(
              totalFrames: totalFrames,
              startFrame: startFrame,
              endFrame: endFrame,
              measureKeyFrame: measureKeyFrame,
              horizontalPadding: horizontalPadding,
            ),
          ),
        ),
      ),
    );
  }
}

class FrameIndicatorPainter extends CustomPainter {
  FrameIndicatorPainter({
    required this.totalFrames,
    required this.startFrame,
    required this.endFrame,
    required this.measureKeyFrame,
    required this.horizontalPadding,
  });

  final int totalFrames;
  final int? startFrame;
  final int? endFrame;
  final Set<int> measureKeyFrame;
  final double horizontalPadding;

  static const double strokeWidth = 4;
  static const double strokeHeight = 30;

  @override
  void paint(Canvas canvas, Size size) {
    final double width = size.width - horizontalPadding * 2;
    final double height = size.height;
    final double frameWidth = width / (totalFrames - 1);
    final double indicatorTop = (height - strokeHeight) / 2;
    final double indicatorBottom = (height + strokeHeight) / 2;

    final Paint startEndFramePaint = Paint()
      ..color = LabColors.base500
      ..strokeWidth = strokeWidth
      ..strokeCap = StrokeCap.round;

    final Paint measureKeyFramePaint = Paint()
      ..color = Colors.yellow
      ..strokeWidth = strokeWidth
      ..strokeCap = StrokeCap.round;

    // 绘制起始帧
    if (startFrame != null) {
      canvas.drawLine(
        Offset(horizontalPadding + startFrame! * frameWidth, indicatorTop),
        Offset(horizontalPadding + startFrame! * frameWidth, indicatorBottom),
        startEndFramePaint,
      );
    }

    // 绘制结束帧
    if (endFrame != null) {
      canvas.drawLine(
        Offset(horizontalPadding + endFrame! * frameWidth, indicatorTop),
        Offset(horizontalPadding + endFrame! * frameWidth, indicatorBottom),
        startEndFramePaint,
      );
    }

    // 绘制测量值关键帧
    for (final int keyFrame in measureKeyFrame) {
      canvas.drawLine(
        Offset(horizontalPadding + keyFrame * frameWidth, indicatorTop),
        Offset(horizontalPadding + keyFrame * frameWidth, indicatorBottom),
        measureKeyFramePaint,
      );
    }

    // 绘制测试线段
    // final Paint testPaint = Paint()
    //   ..color = Colors.red
    //   ..strokeWidth = strokeWidth
    //   ..strokeCap = StrokeCap.butt;
    // canvas.drawLine(
    //   Offset(horizontalPadding, 0),
    //   Offset(size.width - horizontalPadding, 0),
    //   testPaint,
    // );
  }

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