import 'package:flyinsono/lab/components/lab_text_tool_tip.dart';
import 'package:flutter/material.dart';

class LabOverflowTooltipWrapper extends StatelessWidget {
  final Text child;
  final DisplayPosition position;
  final Offset offset;

  LabOverflowTooltipWrapper({
    Key? key,
    required this.child,
    this.position = DisplayPosition.top,
    this.offset = const Offset(0, -3),
  })  : assert(
          child.style != null && child.style!.fontFamily != null,
          'child must have a non-null style with a non-null fontFamily',
        ),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    var tp = TextPainter(
      maxLines: child.maxLines,
      textAlign: child.textAlign ?? TextAlign.start,
      textDirection: child.textDirection ?? TextDirection.ltr,
      text: child.textSpan ??
          TextSpan(
            text: child.data,
            style: child.style,
          ),
    );

    return LayoutBuilder(
      builder: (context, constrains) {
        tp.layout(maxWidth: constrains.maxWidth);
        final overflowed = tp.didExceedMaxLines;
        if (overflowed) {
          return LabTextTooltip(
            position: position,
            offset: offset,
            message: child.data ?? '',
            child: child,
          );
        }
        return child;
      },
    );
  }
}
