/// Created by @Haoyi on 4/13/21
///
class NumberUtils {
  static String twoDigits(int n) {
    if (n >= 10) return "$n";
    return "0$n";
  }

  static String threeDigits(int n) {
    if (n >= 100) return "$n";
    if (n >= 10) return "0$n";
    return "00$n";
  }

  static String fourDigits(int n, {String placeholder = '0', int align = 0}) {
    if (n >= 1000) return "$n";
    if (n >= 100) return "$placeholder$n";
    if (n >= 10) return align == 0 ? "$placeholder$placeholder$n" : "$placeholder$n$placeholder";
    return align == 0 ? "$placeholder$placeholder$placeholder$n" : "${placeholder}0$n$placeholder";
  }

  static String fiveDigits(int n) {
    if (n >= 10000) return "$n";
    if (n >= 1000) return "0$n";
    if (n >= 100) return "00$n";
    if (n >= 10) return "000$n";
    return "0000$n";
  }

  static String sixDigits(int n) {
    if (n >= 100000) return "$n";
    if (n >= 10000) return "0$n";
    if (n >= 1000) return "00$n";
    if (n >= 100) return "000$n";
    if (n >= 10) return "0000$n";
    return "00000$n";
  }

  static String sevenDigits(int n) {
    if (n >= 1000000) return "$n";
    if (n >= 100000) return "0$n";
    if (n >= 10000) return "00$n";
    if (n >= 1000) return "000$n";
    if (n >= 100) return "0000$n";
    if (n >= 10) return "00000$n";
    return "000000$n";
  }

  static String eightDigits(int n) {
    if (n >= 10000000) return "$n";
    if (n >= 1000000) return "0$n";
    if (n >= 100000) return "00$n";
    if (n >= 10000) return "000$n";
    if (n >= 1000) return "0000$n";
    if (n >= 100) return "00000$n";
    if (n >= 10) return "000000$n";
    return "0000000$n";
  }

  static String nineDigits(int n) {
    if (n >= 100000000) return "$n";
    if (n >= 10000000) return "0$n";
    if (n >= 1000000) return "00$n";
    if (n >= 100000) return "000$n";
    if (n >= 10000) return "0000$n";
    if (n >= 1000) return "00000$n";
    if (n >= 100) return "000000$n";
    if (n >= 10) return "0000000$n";
    return "00000000$n";
  }

  static String tenDigits(int n) {
    if (n >= 1000000000) return "$n";
    if (n >= 100000000) return "0$n";
    if (n >= 10000000) return "00$n";
    if (n >= 1000000) return "000$n";
    if (n >= 100000) return "0000$n";
    if (n >= 10000) return "00000$n";
    if (n >= 1000) return "000000$n";
    if (n >= 100) return "0000000$n";
    if (n >= 10) return "00000000$n";
    return "000000000$n";
  }

  static String nDigits(int value, int n) {
    final result = value.toString();
    final supplement = n - result.length;
    if (supplement > 0) {
      StringBuffer zero = StringBuffer();
      for (int i = 0; i < supplement; ++i) {
        zero.write("0");
      }
      return "${zero.toString()}$result";
    }
    return result;
  }

  static int getUnsignedNumLength(int num) {
    if (num < 10) return 1;
    if (num < 100) return 2;
    if (num < 1000) return 3;
    if (num < 10000) return 4;
    if (num < 100000) return 5;
    if (num < 1000000) return 6;
    if (num < 10000000) return 7;
    if (num < 100000000) return 8;
    if (num < 1000000000) return 9;
    if (num < 10000000000) return 10;
    int count = 11;
    for (var tmp = num ~/ 100000000000; tmp != 0; tmp ~/= 10) {
      count++;
    }
    return count;
  }

  static int safeParseInt(String? numStr, {required int defNum, int? radix}) {
    if (numStr == null || numStr == '') {
      return defNum;
    }
    try {
      return int.parse(numStr, radix: radix);
    } catch (error) {
      return defNum;
    }
  }
}
