import 'dart:io';

class StringHelpers {
  // TODO ( to Mohammad Salameh): convert this to extension method
  static String enhanceFileName(File file) {
    var fileName = " ";
    final filePath = file.path;
    final fileStringArray = filePath.split("/");
    fileName = fileStringArray.last;
    if (fileName.length > 20) {
      // crop the name and keep the extension
      final firstPart = fileName.substring(0, 17);
      fileName = "$firstPart...";
    }
    return fileName;
  }

  static String returnEnglishStringFromArabicNumber(String arabicString) {
    var englishStringNumber = arabicString;
    englishStringNumber = englishStringNumber.replaceAll("٠", "0");
    englishStringNumber = englishStringNumber.replaceAll("١", "1");
    englishStringNumber = englishStringNumber.replaceAll("٢", "2");
    englishStringNumber = englishStringNumber.replaceAll("٣", "3");
    englishStringNumber = englishStringNumber.replaceAll("٤", "4");
    englishStringNumber = englishStringNumber.replaceAll("٥", "5");
    englishStringNumber = englishStringNumber.replaceAll("٦", "6");
    englishStringNumber = englishStringNumber.replaceAll("٧", "7");
    englishStringNumber = englishStringNumber.replaceAll("٨", "8");
    englishStringNumber = englishStringNumber.replaceAll("٩", "9");
    return englishStringNumber;
  }

  static String returnTheFirstThreeWords(String completeString) {
    // We use this method to return a nice readable word that is shorter than 26 characters
    // split the string to substrings with the space as the pattern
    final arrayOfSubStrings = completeString.split(" ");
    String newString = "";
    // keep adding the substrings until 26 is reached
    var index = 0;
    while (newString.length < 20 && index < arrayOfSubStrings.length) {
      newString = "$newString + ${arrayOfSubStrings[index]} ";
      index++;
    }
    return newString;
  }

  static int getIntFromString(String? numberString) {
    if (numberString != null) {
      final numberInt = int.tryParse(numberString);
      if (numberInt != null) {
        return numberInt;
      } else {
        final numberDouble = double.tryParse(numberString);
        if (numberDouble != null) {
          return numberDouble.toInt();
        } else {
          return 0;
        }
      }
    } else {
      return 0;
    }
  }

  static String toTitleCase(String text) {
    if (text.contains(' ')) {
      //remove empty elements
      String title = text.split(' ').where((element) => element.isNotEmpty).join(' ');
      String result = title
          .split(' ')
          .map((element) => element[0].toUpperCase() + element.substring(1))
          .join(' ');
      return result;
    } else {
      return text.toUpperCase();
    }
  }
}
