import 'package:flutter/material.dart';
import 'package:hmg_qline/constants/app_constants.dart';
import 'package:hmg_qline/views/common_widgets/app_texts_widget.dart';
import 'package:hmg_qline/views/view_helpers/size_config.dart';

class HmgNewsWidget extends StatelessWidget {
  final String tagTitle;

  const HmgNewsWidget({super.key, required this.tagTitle});

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: ArrowShapePainter(
        fillColor: AppColors.redColor,
        arrowWidth: 20.0,
        arrowHeight: 20.0,
        topLeftRadius: const Radius.circular(10),
        bottomLeftRadius: const Radius.circular(10),
        topRightRadius: const Radius.circular(8),
        bottomRightRadius: const Radius.circular(8),
      ),
      child: Container(
        color: Colors.transparent,
        alignment: Alignment.center,
        padding: const EdgeInsets.fromLTRB(20, 8, 35, 8),
        child: AppText(
          tagTitle,
          color: AppColors.whiteColor,
          fontWeight: FontWeight.bold,
          fontSize: SizeConfig.getWidthMultiplier() * 1.5,
        ),
      ),
    );
  }
}

class ArrowShapePainter extends CustomPainter {
  final Color fillColor;
  final Radius topLeftRadius;
  final Radius topRightRadius;
  final Radius bottomLeftRadius;
  final Radius bottomRightRadius;
  final double arrowWidth;
  final double arrowHeight;

  ArrowShapePainter({
    required this.fillColor,
    this.topLeftRadius = Radius.zero, // Default to no rounding
    this.topRightRadius = Radius.zero,
    this.bottomLeftRadius = Radius.zero,
    this.bottomRightRadius = Radius.zero,
    this.arrowWidth = 20.0,
    this.arrowHeight = 20.0,
  });

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

    final path = Path();

    // Main rounded rectangle body with customizable corners
    path.addRRect(RRect.fromLTRBAndCorners(
      0,
      0,
      size.width - arrowWidth, // Adjust width for the arrow
      size.height,
      topLeft: topLeftRadius,
      topRight: topRightRadius,
      bottomLeft: bottomLeftRadius,
      bottomRight: bottomRightRadius,
    ));

    // Arrow part remains the same
    path.moveTo(size.width - arrowWidth, (size.height / 2) - (arrowHeight / 2));
    path.lineTo(size.width, size.height / 2);
    path.lineTo(size.width - arrowWidth, (size.height / 2) + (arrowHeight / 2));
    path.close();

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    if (oldDelegate is ArrowShapePainter) {
      return oldDelegate.fillColor != fillColor ||
          oldDelegate.topLeftRadius != topLeftRadius ||
          oldDelegate.topRightRadius != topRightRadius ||
          oldDelegate.bottomLeftRadius != bottomLeftRadius ||
          oldDelegate.bottomRightRadius != bottomRightRadius ||
          oldDelegate.arrowWidth != arrowWidth ||
          oldDelegate.arrowHeight != arrowHeight;
    }
    return true;
  }
}
