import 'package:flutter/material.dart';
import 'package:llswidget/llswidget.dart';

class LLSBadge extends StatefulWidget {
  /// Create badges of all types, check out [LLSButtonBadge] for button badges and [LLSIconBadge] for icon type badges
  const LLSBadge({
    Key? key,
    this.textStyle,
    this.borderShape,
    this.shape = LLSBadgeShape.standard,
    this.color = LLSColors.DANGER,
    this.textColor = LLSColors.WHITE,
    this.size = LLSSize.SMALL,
    this.border,
    this.text,
    this.child,
  }) : super(key: key);

  final BorderSide? border;
  
  final ShapeBorder? borderShape;

  final LLSBadgeShape shape;

  final Color color;

  final double size;

  final Widget? child;

  final String? text;

  final TextStyle? textStyle;

  final Color textColor;

  @override
  _LLSBadgeState createState() => _LLSBadgeState();
}

class _LLSBadgeState extends State<LLSBadge> {
  late Color color;
  late Color textColor;
  Widget? child;
  LLSBadgeShape? counterShape;
  late double size;
  double? height;
  double? width;
  double? fontSize;

  @override
  void initState() {
    color = widget.color;
    textColor = widget.textColor;
    child = widget.text != null ? Text(widget.text ?? '') : widget.child;
    counterShape = widget.shape;
    size = widget.size;
    super.initState();
  }

  @override
  void didUpdateWidget(LLSBadge oldWidget) {
    color = widget.color;
    textColor = widget.textColor;
    child = widget.text != null ? Text(widget.text ?? '') : widget.child;
    counterShape = widget.shape;
    size = widget.size;
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    final BorderSide shapeBorder = widget.border != null
        ? widget.border!
        : BorderSide(
            color: color,
            width: 0,
          );

    ShapeBorder shape;

    if (counterShape == LLSBadgeShape.pills) {
      shape = RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(50),
        side: shapeBorder,
      );
    } else if (counterShape == LLSBadgeShape.square) {
      shape = RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(0),
        side: shapeBorder,
      );
    } else if (counterShape == LLSBadgeShape.standard) {
      shape = RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(5),
        side: shapeBorder,
      );
    } else if (counterShape == LLSBadgeShape.circle) {
      shape = RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(100),
        side: shapeBorder,
      );
    } else {
      shape = RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(5),
        side: shapeBorder,
      );
    }

    if (widget.size == LLSSize.SMALL) {
      height = size * 0.56;
      width = size * 0.73;
      fontSize = size * 0.31;
    } else if (widget.size == LLSSize.MEDIUM) {
      height = size * 0.58;
      width = size * 0.76;
      fontSize = size * 0.34;
    } else if (widget.size == LLSSize.LARGE) {
      height = size * 0.6;
      width = size * 0.79;
      fontSize = size * 0.37;
    } else {
      height = size * 0.58;
      width = size * 0.76;
      fontSize = size * 0.34;
    }

    return Container(
      height: height,
      width: counterShape == LLSBadgeShape.circle ? height : width,
      child: Material(
        textStyle: TextStyle(
          color: textColor,
          fontSize: fontSize,
        ),
        shape: widget.borderShape ?? shape,
        color: color,
        type: MaterialType.button,
        child: Container(
          child: Center(
            widthFactor: 1,
            heightFactor: 1,
            child: child,
          ),
        ),
      ),
    );
  }
}