import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/enums.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/cupertino.dart';
import 'package:intl/intl.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';

extension CapExtension on String {
  String get toCamelCase => "${this[0].toUpperCase()}${this.substring(1)}";

  String get inCaps => '${this[0].toUpperCase()}${this.substring(1)}';

  String get allInCaps => this.toUpperCase();

  String get needTranslation => this;

  String get capitalizeFirstofEach => this.trim().length > 0 ? this.trim().toLowerCase().split(" ").map((str) => str.inCaps).join(" ") : "";
}

extension EmailValidator on String {
  Widget get toWidget => Text(this);

  Widget toText8({Color? color, bool isBold = false, int? maxlines, FontStyle? fontStyle, TextOverflow? textOverflow}) => Text(
        this,
        maxLines: maxlines,
        overflow: textOverflow,
        style: TextStyle(
          fontSize: 8.fSize,
          fontStyle: fontStyle ?? FontStyle.normal,
          fontWeight: isBold ? FontWeight.bold : FontWeight.normal,
          color: color ?? AppColors.blackColor,
          letterSpacing: -1,
        ),
      );

  Widget toText10({Color? color, bool isBold = false, bool isUnderLine = false, int? maxlines, FontStyle? fontStyle, TextOverflow? textOverflow}) => Text(
        this,
        maxLines: maxlines,
        overflow: textOverflow,
        style: TextStyle(
            fontSize: 10.fSize,
            fontStyle: fontStyle ?? FontStyle.normal,
            fontWeight: isBold ? FontWeight.bold : FontWeight.normal,
            color: color ?? AppColors.blackColor,
            letterSpacing: -1,
            decoration: isUnderLine ? TextDecoration.underline : null,
            decorationColor: color ?? AppColors.blackColor),
      );

  Widget toText11({Color? color, FontWeight? weight, bool isUnderLine = false, bool isCenter = false, bool isBold = false, int maxLine = 0, double letterSpacing = 0.64}) => Text(
        this,
        textAlign: isCenter ? TextAlign.center : null,
        maxLines: (maxLine > 0) ? maxLine : null,
        softWrap: true,
        style: TextStyle(
          fontSize: 11.fSize,
          fontWeight: weight ?? (isBold ? FontWeight.bold : FontWeight.normal),
          color: color ?? AppColors.blackColor,
          letterSpacing: -1,
          decoration: isUnderLine ? TextDecoration.underline : null,
        ),
      );

  Widget toText12({Color? color, bool isUnderLine = false, bool isBold = false, FontWeight? fontWeight, bool isCenter = false, double? height, int maxLine = 0}) => Text(
        this,
        textAlign: isCenter ? TextAlign.center : null,
        maxLines: (maxLine > 0) ? maxLine : null,
        style: TextStyle(
          fontSize: 12.fSize,
          fontWeight: fontWeight ?? (isBold ? FontWeight.bold : FontWeight.normal),
          color: color ?? AppColors.blackColor,
          letterSpacing: -1,
          height: height,
          decorationColor: isUnderLine ? AppColors.blackColor : null,
          decoration: isUnderLine ? TextDecoration.underline : null,
        ),
      );

  Widget toText12Auto({Color? color, bool isUnderLine = false, bool isBold = false, bool isCenter = false, int maxLine = 0}) => AutoSizeText(
        this,
        textAlign: isCenter ? TextAlign.center : null,
        maxLines: (maxLine > 0) ? maxLine : null,
        minFontSize: 8,
        style: TextStyle(
          fontSize: 12.fSize,
          fontWeight: isBold ? FontWeight.bold : FontWeight.normal,
          color: color ?? AppColors.blackColor,
          letterSpacing: -1,
          decoration: isUnderLine ? TextDecoration.underline : null,
        ),
      );

  Widget toTextAuto({
    Color? color,
    bool isUnderLine = false,
    bool isBold = false,
    bool isCenter = false,
    int maxLine = 0,
    double fontSize = 12,
    double letterSpacing = 0.64,
    double height = 1,
    TextOverflow? textOverflow,
    FontWeight? fontWeight,
  }) =>
      AutoSizeText(
        this,
        textAlign: isCenter ? TextAlign.center : null,
        maxLines: (maxLine > 0) ? maxLine : null,
        minFontSize: 5,
        overflow: textOverflow,
        softWrap: true,
        style: TextStyle(
          fontSize: fontSize,
          fontWeight: fontWeight ?? (isBold ? FontWeight.bold : FontWeight.normal),
          color: color ?? AppColors.blackColor,
          letterSpacing: letterSpacing,
          decoration: isUnderLine ? TextDecoration.underline : null,
        ),
      );

  Widget toText13({
    Color? color,
    bool isUnderLine = false,
    bool isBold = false,
    bool isCenter = false,
    int maxLine = 0,
  }) =>
      Text(
        this,
        textAlign: isCenter ? TextAlign.center : null,
        maxLines: (maxLine > 0) ? maxLine : null,
        style: TextStyle(
            fontSize: 13.fSize,
            fontWeight: isBold ? FontWeight.bold : FontWeight.normal,
            color: color ?? AppColors.blackColor,
            letterSpacing: -1,
            decoration: isUnderLine ? TextDecoration.underline : null),
      );

  Widget toText14({Color? color, bool isUnderLine = false, bool isBold = false, bool isCenter = false, FontWeight? weight, int? maxlines}) => Text(
        this,
        textAlign: isCenter ? TextAlign.center : null,
        maxLines: maxlines,
        style: TextStyle(
            color: color ?? AppColors.blackColor,
            fontSize: 14.fSize,
            letterSpacing: -1,
            fontWeight: weight ?? (isBold ? FontWeight.bold : FontWeight.normal),
            decoration: isUnderLine ? TextDecoration.underline : null),
      );

  Widget toText15({Color? color, bool isUnderLine = false, bool isBold = false, bool isCenter = false, FontWeight? weight, int? maxlines, double? letterSpacing = -0.4,}) => Text(
        this,
        textAlign: isCenter ? TextAlign.center : null,
        maxLines: maxlines,
        style: TextStyle(
            color: color ?? AppColors.blackColor,
            fontSize: 15.fSize,
            letterSpacing: letterSpacing,
            fontWeight: weight ?? (isBold ? FontWeight.bold : FontWeight.normal),
            decoration: isUnderLine ? TextDecoration.underline : null),
      );

  Widget toText16({
    Color? color,
    bool isUnderLine = false,
    bool isBold = false,
    bool isCenter = false,
    int? maxlines,
    TextAlign? textAlign,
    FontWeight? weight,
    double? letterSpacing = -0.4,
  }) =>
      Text(
        this,
        maxLines: maxlines,
        textAlign: isCenter ? TextAlign.center : null,
        style: TextStyle(
          color: color ?? AppColors.blackColor,
          fontSize: 16.fSize,
          letterSpacing: letterSpacing,
          fontWeight: weight ?? (isBold ? FontWeight.bold : FontWeight.normal),
          decoration: isUnderLine ? TextDecoration.underline : null,
        ),
      );

  Widget toText17({Color? color, bool isBold = false, bool isCenter = false}) => Text(
        this,
        textAlign: isCenter ? TextAlign.center : null,
        style: TextStyle(color: color ?? AppColors.blackColor, fontSize: 17.fSize, letterSpacing: -1, fontWeight: isBold ? FontWeight.bold : FontWeight.normal),
      );

  Widget toText18({Color? color, bool isBold = false, bool isCenter = false, int? maxlines}) => Text(
        maxLines: maxlines,
        textAlign: isCenter ? TextAlign.center : null,
        this,
        style: TextStyle(fontSize: 18.fSize, fontWeight: isBold ? FontWeight.bold : FontWeight.normal, color: color ?? AppColors.blackColor, letterSpacing: -0.4),
      );

  Widget toText19({Color? color, bool isBold = false}) => Text(
        this,
        style: TextStyle(fontSize: 19.fSize, fontWeight: isBold ? FontWeight.bold : FontWeight.normal, color: color ?? AppColors.blackColor, letterSpacing: -0.4),
      );

  Widget toText20({Color? color, bool isBold = false}) => Text(
        this,
        style: TextStyle(fontSize: 20.fSize, fontWeight: isBold ? FontWeight.bold : FontWeight.normal, color: color ?? AppColors.blackColor, letterSpacing: -0.4),
      );

  Widget toText21({Color? color, bool isBold = false, FontWeight? weight, int? maxlines}) => Text(
        this,
        maxLines: maxlines,
        style: TextStyle(color: color ?? AppColors.blackColor, fontSize: 21.fSize, letterSpacing: -1, fontWeight: weight ?? (isBold ? FontWeight.bold : FontWeight.normal)),
      );

  Widget toText22({Color? color, bool isBold = false, bool isCenter = false}) => Text(
        this,
        textAlign: isCenter ? TextAlign.center : null,
        style: TextStyle(height: 1, color: color ?? AppColors.blackColor, fontSize: 22.fSize, letterSpacing: -1, fontWeight: isBold ? FontWeight.bold : FontWeight.normal),
      );

  Widget toText24({Color? color, bool isBold = false, bool isCenter = false}) => Text(
        this,
        textAlign: isCenter ? TextAlign.center : null,
        style: TextStyle(height: 23 / 24, color: color ?? AppColors.blackColor, fontSize: 24.fSize, letterSpacing: -1, fontWeight: isBold ? FontWeight.bold : FontWeight.normal),
      );

  Widget toText26({Color? color, bool isBold = false, double? height, bool isCenter = false}) => Text(
        this,
        textAlign: isCenter ? TextAlign.center : null,
        style: TextStyle(height: height ?? 23 / 26, color: color ?? AppColors.blackColor, fontSize: 26.fSize, letterSpacing: -1, fontWeight: isBold ? FontWeight.bold : FontWeight.normal),
      );

  Widget toText28({Color? color, bool isBold = false, double? height, bool isCenter = false}) => Text(
        this,
        textAlign: isCenter ? TextAlign.center : null,
        style: TextStyle(height: height ?? 23 / 28, color: color ?? AppColors.blackColor, fontSize: 28.fSize, letterSpacing: -1, fontWeight: isBold ? FontWeight.bold : FontWeight.normal),
      );

  Widget toText32({Color? color, bool isBold = false, bool isCenter = false}) => Text(
        this,
        textAlign: isCenter ? TextAlign.center : null,
        style: TextStyle(height: 32 / 32, color: color ?? AppColors.blackColor, fontSize: 32.fSize, letterSpacing: -1, fontWeight: isBold ? FontWeight.bold : FontWeight.normal),
      );

  Widget toText44({Color? color, bool isBold = false}) => Text(
        this,
        style: TextStyle(height: 32 / 32, color: color ?? AppColors.blackColor, fontSize: 44.fSize, letterSpacing: -1, fontWeight: isBold ? FontWeight.bold : FontWeight.normal),
      );

  Widget toSectionHeading({String upperHeading = "", String lowerHeading = ""}) {
    String upper = "";
    String lower = "";
    String heading = this;
    if (heading.isNotEmpty) {
      List<String> data = heading.split(" ");

      if (data.length > 1) {
        upper = data[0];
        data.removeAt(0);
        lower = data.join(" ");
      } else {
        lower = data[0];
      }
    }
    if (upperHeading.isNotEmpty) {
      upper = upperHeading;
    }
    if (lowerHeading.isNotEmpty) {
      lower = lowerHeading;
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        if (upper.isNotEmpty) upper.toText12(),
        lower.toText24(isBold: true),
      ],
    );
  }

  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);
  }

  String toFormattedDate() {
    String date = this.split("T")[0];
    String time = this.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))}";
  }

  String 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";
      default:
        return "";
    }
  }

  String truncate(int max, {String suffix = ''}) {
    try {
      return length <= max ? this : '${substring(0, max - suffix.length)}$suffix';
    } catch (e) {
      return this;
    }
  }
}

extension DynamicTextStyleExtension on BuildContext {
  TextStyle dynamicTextStyle(
      {double? fontSize,
      FontWeight? fontWeight,
      Color? color,
      double? letterSpacing,
      double? wordSpacing,
      double? height,
      List<Shadow>? shadows,
      Color? backgroundColor,
      TextDecoration? decoration,
      Color? decorationColor,
      TextDecorationStyle? decorationStyle,
      double? decorationThickness,
      Locale? locale,
      TextBaseline? textBaseline,
      FontStyle? fontStyle,
      bool isLanguageSwitcher = false}) {
    final family = FontUtils.getFontFamilyForLanguage(true);
    return TextStyle(
      fontFamily: family,
      fontSize: fontSize,
      fontWeight: fontWeight,
      color: color,
      letterSpacing: letterSpacing,
      wordSpacing: wordSpacing,
      height: height,
      shadows: shadows,
      backgroundColor: backgroundColor,
      decoration: decoration,
      decorationColor: decorationColor,
      decorationStyle: decorationStyle,
      decorationThickness: decorationThickness,
      locale: locale ?? Localizations.localeOf(this),
      textBaseline: textBaseline,
      fontStyle: fontStyle,
    );
  }
}

class FontUtils {
  /// Get the appropriate font family based on the current language
  static bool getFontFamily(BuildContext context) {
    // final projectViewModel = Provider.of<ProjectViewModel>(context, listen: false);
    return true;
  }

  /// Get the appropriate font family for a specific language
  static String getFontFamilyForLanguage(bool isArabic) {
    return isArabic ? 'Cairo' : 'Poppins';
  }
}

extension CountryExtension on CountryEnum {
  String get displayName {
    switch (this) {
      case CountryEnum.saudiArabia:
        return "Kingdom Of Saudi Arabia";
      case CountryEnum.unitedArabEmirates:
        return "United Arab Emirates";
    }
  }

  String get nameArabic {
    switch (this) {
      case CountryEnum.saudiArabia:
        return "المملكة العربية السعودية";
      case CountryEnum.unitedArabEmirates:
        return "الإمارات العربية المتحدة";
    }
  }

  String get iconPath {
    switch (this) {
      case CountryEnum.saudiArabia:
        return AppAssets.ksa;
      case CountryEnum.unitedArabEmirates:
        return AppAssets.uae;
    }
  }

  String get countryCode {
    switch (this) {
      case CountryEnum.saudiArabia:
        return "966";
      case CountryEnum.unitedArabEmirates:
        return "971";
    }
  }

  static CountryEnum fromDisplayName(String name) {
    switch (name) {
      case "Kingdom Of Saudi Arabia":
      case "المملكة العربية السعودية":
        return CountryEnum.saudiArabia;
      case "United Arab Emirates":
      case "الإمارات العربية المتحدة":
        return CountryEnum.unitedArabEmirates;
      default:
        throw Exception("Invalid country name");
    }
  }
}

extension GenderTypeExtension on GenderTypeEnum {
  String get value => this == GenderTypeEnum.male ? "M" : "F";

  String get type => this == GenderTypeEnum.male ? "Male" : "Female";

  String get typeAr => this == GenderTypeEnum.male ? "ذكر" : "أنثى";

  static GenderTypeEnum? fromValue(String? value) {
    switch (value) {
      case "M":
        return GenderTypeEnum.male;
      case "F":
        return GenderTypeEnum.female;
      default:
        return null;
    }
  }

  static GenderTypeEnum? fromType(String? type) {
    switch (type) {
      case "Male":
        return GenderTypeEnum.male;
      case "Female":
        return GenderTypeEnum.female;
      default:
        return null;
    }
  }
}

extension MaritalStatusTypeExtension on MaritalStatusTypeEnum {
  String get value {
    switch (this) {
      case MaritalStatusTypeEnum.single:
        return "U";
      case MaritalStatusTypeEnum.married:
        return "M";
      case MaritalStatusTypeEnum.divorced:
        return "D";
      case MaritalStatusTypeEnum.widowed:
        return "W";
    }
  }

  String get type {
    switch (this) {
      case MaritalStatusTypeEnum.single:
        return "Single";
      case MaritalStatusTypeEnum.married:
        return "Married";
      case MaritalStatusTypeEnum.divorced:
        return "Divorced";
      case MaritalStatusTypeEnum.widowed:
        return "Widowed";
    }
  }

  String get typeAr {
    switch (this) {
      case MaritalStatusTypeEnum.single:
        return "أعزب";
      case MaritalStatusTypeEnum.married:
        return "متزوج";
      case MaritalStatusTypeEnum.divorced:
        return "مطلق";
      case MaritalStatusTypeEnum.widowed:
        return "أرمل";
    }
  }

  static MaritalStatusTypeEnum? fromValue(String? value) {
    switch (value) {
      case "U":
        return MaritalStatusTypeEnum.single;
      case "M":
        return MaritalStatusTypeEnum.married;
      case "D":
        return MaritalStatusTypeEnum.divorced;
      case "W":
        return MaritalStatusTypeEnum.widowed;
      default:
        return null;
    }
  }

  static MaritalStatusTypeEnum? fromType(String? type) {
    switch (type) {
      case "Single":
        return MaritalStatusTypeEnum.single;
      case "Married":
        return MaritalStatusTypeEnum.married;
      case "Divorced":
        return MaritalStatusTypeEnum.divorced;
      case "Widowed":
        return MaritalStatusTypeEnum.widowed;
      default:
        return null;
    }
  }
}
