import 'package:flutter/material.dart';

/// AppBar / 工具栏用的 K 线蜡烛图图标（合约、现货等交易页共用）
class KlineToolbarIcon extends StatelessWidget {
  const KlineToolbarIcon({super.key, required this.color});
  final Color color;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 20,
      height: 20,
      child: CustomPaint(painter: _KlineToolbarPainter(color: color)),
    );
  }
}

class _KlineToolbarPainter extends CustomPainter {
  const _KlineToolbarPainter({required this.color});
  final Color color;

  @override
  void paint(Canvas canvas, Size size) {
    final stroke = Paint()
      ..color = color
      ..strokeWidth = 1.2
      ..style = PaintingStyle.stroke;
    final fill = Paint()
      ..color = color
      ..style = PaintingStyle.fill;

    // 4根蜡烛：(x, wickTop, bodyTop, bodyBottom, wickBottom)
    const bars = [
      (2.5, 1.0, 4.0, 11.0, 14.0),
      (7.0, 2.0, 3.5, 9.0, 13.0),
      (11.5, 4.0, 6.5, 13.0, 16.0),
      (16.0, 2.5, 4.5, 10.0, 15.0),
    ];
    const barW = 3.0;

    for (final (x, wT, bT, bB, wBtm) in bars) {
      final cx = x + barW / 2;
      canvas.drawLine(Offset(cx, wT), Offset(cx, wBtm), stroke);
      canvas.drawRect(Rect.fromLTWH(x, bT, barW, bB - bT), fill);
    }
  }

  @override
  bool shouldRepaint(covariant _KlineToolbarPainter oldDelegate) =>
      oldDelegate.color != color;
}
