tafqitNumberWithParts method

String? tafqitNumberWithParts({
  1. required List<num> listOfNumberAndParts,
  2. required TafqitUnitCode tafqitUnitCode,
  3. String justWord = 'فقط',
  4. String noOtherWord = 'لاغير',
  5. bool tryTafqit = true,
})

Implementation

String? tafqitNumberWithParts(
    {required List<num> listOfNumberAndParts,
    required TafqitUnitCode tafqitUnitCode,
    String justWord = 'فقط',
    String noOtherWord = 'لاغير',
    bool tryTafqit = true}) {
  ///----
  List? splitedUnitValue;
  String andWord = '';
  TafqitUnit currentUnit;
  TafqitUnitCode currentUnitCode = tafqitUnitCode;
  bool mainUnitFlag = true;
  bool allWasZeroFlag = true;
  bool? negativeFlag;

  String tafResult = '';
  int listLenght = listOfNumberAndParts.length;

  for (int i = 0; i < listLenght; i++) {
    splitedUnitValue = splitUnitValue(listOfNumberAndParts[i]);

    currentUnit = TafqitUnit.fromMap(tafqitPredefinedUnits.firstWhere(
        (element) => element['unitCode'] == currentUnitCode, orElse: () {
      return tafqitPredefinedUnits
          .firstWhere((e) => e['unitCode'] == TafqitUnitCode.undefinedPart);
    }));

    if (splitedUnitValue.length > 2) {
      if (tryTafqit) {
        return null;
      } else {
        throw FormatException(
            '''It is not possible to handle numbers containing commas ${listOfNumberAndParts[i]},
            enter the number without commas  ( ${splitedUnitValue[1]}) ,
            and add the parts ( ${splitedUnitValue[2]}) as a valid number in the parts field:''');
      }
    }
    if (i > 0 &&
        currentUnit.unitMaxValue != 0 &&
        splitedUnitValue[1] >= currentUnit.unitMaxValue) {
      if (tryTafqit) {
        return null;
      } else {
        throw FormatException(
            '''The value of the parts should not exceed the upper limit of the unit  ${currentUnit.unitCode}    "
          upper limit:  ${currentUnit.unitMaxValue}
          the pass value was: ${splitedUnitValue[1]}  ''');
      }
    }

    if ((splitedUnitValue[1] != 0) ||
        ((i == listLenght - 1) && allWasZeroFlag)) {
      tafResult = tafResult +
          andWord +
          (_getTafqit(
                  am: splitedUnitValue[1].toString(),
                  tafqitUnit: currentUnit,
                  isMainUnit: (currentUnit.comprehensiveUnit.isNotEmpty &&
                          mainUnitFlag
                      ? true
                      : false)) ??
              '');
      andWord = ' و ';
      mainUnitFlag = false;
      allWasZeroFlag = false;
      negativeFlag =
          (negativeFlag ?? ((splitedUnitValue[0] > 0 ? false : true)));
    }
    currentUnitCode = currentUnit.partialUnitCode;
  }
  var negativeSign = (negativeFlag ?? false) == true ? 'سالب' : '';
  tafResult = '$justWord ${(negativeSign)} $tafResult $noOtherWord'
      .replaceAll('  ', ' ')
      .replaceAll('  ', ' ')
      .replaceAll('و ', 'و');
  return (tafResult);
}