import 'package:flutter/cupertino.dart';
import 'package:intl/intl.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:auto_size_text/auto_size_text.dart';

extension EmailValidator on String {
  Widget toText(
          {Color? color,
          bool isBold = false,
          double? fontSize,
          bool isUnderLine = false,
          TextDecoration? textDecoration,
          double letterSpacing = -0.4,
          TextAlign? textAlign,
          double? height,
          int? maxLines}) =>
      AutoSizeText(
        this,
        textAlign: textAlign,
        maxLines: maxLines,
        style: TextStyle(
          height: height,
          decoration: isUnderLine ? TextDecoration.underline : textDecoration ?? TextDecoration.none,
          fontSize: fontSize ?? 10,
          fontWeight: isBold ? FontWeight.bold : FontWeight.w600,
          color: color ?? MyColors.darkTextColor,
          letterSpacing: letterSpacing,
        ),
      );

  bool isValidEmail() {
    return RegExp(r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$').hasMatch(this);
  }

  bool isNum() {
    return RegExp(r'^[0-9]+$').hasMatch(this);
  }

  String toFormattedDate() {
    String date = split("T")[0];
    String time = split("T")[1];
    var dates = date.split("-");
    return "${dates[2]} ${getMonth(int.parse(dates[1]))} ${dates[0]} ${DateFormat('hh:mm a').format(DateFormat('hh:mm:ss').parse(time))}";
  }

  getMonth(int month) {
    switch (month) {
      case 1:
        return "January";
      case 2:
        return "February";
      case 3:
        return "March";
      case 4:
        return "April";
      case 5:
        return "May";
      case 6:
        return "June";
      case 7:
        return "July";
      case 8:
        return "August";
      case 9:
        return "September";
      case 10:
        return "October";
      case 11:
        return "November";
      case 12:
        return "December";
    }
  }
}
