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

class AppText extends StatefulWidget {
  final String text;
  final String? variant;
  final Color color;
  final FontWeight? fontWeight;
  final double? fontSize;
  final double? fontHeight;
  final String fontFamily;
  final int maxLength;
  final bool italic;
  final double? margin;
  final double marginTop;
  final double marginRight;
  final double marginBottom;
  final double marginLeft;
  final double? letterSpacing;
  final TextAlign? textAlign;
  final bool? bold;
  final bool? regular;
  final bool? medium;
  final int? maxLines;
  final bool readMore;
  final String? style;
  final bool allowExpand;
  final bool visibility;
  final TextOverflow? textOverflow;
  final TextDecoration? textDecoration;
  final bool isCopyable;
  final TextDirection textDirection;

  const AppText(
    this.text, {
    super.key,
    this.color = Colors.black,
    this.fontWeight,
    this.variant,
    this.fontSize,
    this.fontHeight,
    this.fontFamily = 'Poppins',
    this.italic = false,
    this.maxLength = 60,
    this.margin,
    this.marginTop = 0,
    this.marginRight = 0,
    this.marginBottom = 0,
    this.marginLeft = 0,
    this.textAlign,
    this.bold,
    this.regular,
    this.medium,
    this.maxLines,
    this.readMore = false,
    this.style,
    this.allowExpand = true,
    this.visibility = true,
    this.textOverflow,
    this.textDecoration,
    this.letterSpacing,
    this.isCopyable = false,
    this.textDirection = TextDirection.ltr,
  });

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

class AppTextState extends State<AppText> {
  bool hidden = false;
  String text = "";

  @override
  void didUpdateWidget(covariant AppText oldWidget) {
    setState(() {
      if (widget.style == "overline") {
        text = widget.text.toUpperCase();
      } else {
        text = widget.text;
      }
    });
    super.didUpdateWidget(oldWidget);
  }

  @override
  void initState() {
    if (widget.style == "overline") {
      text = widget.text.toUpperCase();
    } else {
      text = widget.text;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        margin: widget.margin != null ? EdgeInsets.all(widget.margin!) : EdgeInsets.only(top: widget.marginTop, right: widget.marginRight, bottom: widget.marginBottom, left: widget.marginLeft),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              text,
              textDirection: widget.textDirection,
              textAlign: widget.textAlign,
              overflow: widget.maxLines != null ? ((widget.maxLines! > 1) ? TextOverflow.fade : TextOverflow.ellipsis) : null,
              maxLines: widget.maxLines,
              style: widget.style != null
                  ? _getFontStyle()?.copyWith(
                      fontStyle: widget.italic ? FontStyle.italic : null,
                      color: widget.color,
                      fontFamily: widget.fontFamily,
                      fontWeight: widget.fontWeight ?? _getFontWeight(),
                      height: widget.fontHeight,
                    )
                  : TextStyle(
                      fontStyle: widget.italic ? FontStyle.italic : null,
                      color: widget.color,
                      fontSize: widget.fontSize ?? _getFontSize(),
                      letterSpacing: widget.letterSpacing ?? (widget.variant == "overline" ? 1.5 : null),
                      fontWeight: widget.fontWeight ?? _getFontWeight(),
                      fontFamily: widget.fontFamily,
                      decoration: widget.textDecoration,
                      height: widget.fontHeight),
            )
          ],
        ),
      ),
    );
  }

  TextStyle? _getFontStyle() {
    switch (widget.style) {
      case "headline2":
        return Theme.of(context).textTheme.displayMedium;
      case "headline3":
        return Theme.of(context).textTheme.displaySmall;
      case "headline4":
        return Theme.of(context).textTheme.headlineMedium;
      case "headline5":
        return Theme.of(context).textTheme.headlineSmall;
      case "headline6":
        return Theme.of(context).textTheme.titleLarge;
      case "bodyText2":
        return Theme.of(context).textTheme.bodyMedium;
      case "bodyText_15":
        return Theme.of(context).textTheme.bodyMedium?.copyWith(fontSize: 15.0);
      case "bodyText1":
        return Theme.of(context).textTheme.bodyLarge;
      case "caption":
        return Theme.of(context).textTheme.bodySmall;
      case "overline":
        return Theme.of(context).textTheme.labelSmall;
      case "button":
        return Theme.of(context).textTheme.labelLarge;
      default:
        return const TextStyle();
    }
  }

  double _getFontSize() {
    switch (widget.variant) {
      case "heading0":
        return 40.0;
      case "heading":
        return 32.0;
      case "heading2":
        return 28.0;
      case "heading3":
        return 18.0;
      case "body1":
        return 18.0;
      case "body2":
        return 20.0;
      case "body2Link":
        return 16.0;
      case "caption":
        return 16.0;
      case "caption2":
        return 14.0;
      case "bodyText":
        return 15.0;
      case "bodyText2":
        return 17.0;
      case "caption3":
        return 12.0;
      case "caption4":
        return 9.0;
      case "overline":
        return 11.0;
      case "date":
        return 24.0;
      default:
        return SizeConfig.textMultiplier! * 2.8;
    }
  }

  FontWeight? _getFontWeight() {
    if (widget.bold ?? false) {
      return FontWeight.w900;
    } else if (widget.regular ?? false) {
      return FontWeight.w500;
    } else if (widget.medium ?? false) {
      return FontWeight.w800;
    } else {
      if (widget.style == null) {
        switch (widget.variant) {
          case "heading":
            return FontWeight.w900;
          case "heading2":
            return FontWeight.w900;
          case "heading3":
            return FontWeight.w900;
          case "body1":
            return FontWeight.w800;
          case "body2":
            return FontWeight.w900;
          case "body2Link":
            return FontWeight.w800;
          case "caption":
            return FontWeight.w700;
          case "caption2":
            return FontWeight.w700;
          case "bodyText":
            return FontWeight.w500;
          case "bodyText2":
            return FontWeight.w500;
          case "caption3":
            return FontWeight.w600;
          case "caption4":
            return FontWeight.w600;
          case "overline":
            return FontWeight.w800;
          case "date":
            return FontWeight.w900;
          default:
            return FontWeight.w500;
        }
      } else {
        return null;
      }
    }
  }
}
