part of 'helpers.dart';

void printWrapped(String text) {
  final pattern = new RegExp('.{1,800}');
  pattern.allMatches(text).forEach((match) => print(match.group(0)));
}

String checkError(String checkError) {
  String error;
  if (checkError.contains('No address associated with hostname')) {
    error = SharedValue.titleAlertDisconnected;
  }
  else if(checkError.contains('SocketException')) {
    error = "Please Check Your Network Connection";
  }
  else if(checkError.contains('SQLSTATE') || checkError.contains('<HTML>')) {
    error = "API Error";
  }
  else {
    error = checkError;
  }
  return error;
}

Map checkAPIResultStatus(Map data) {
  String errorGeneral = 'There was an error. Please try again';
  Map checkResult;
  String keysFromJson = data.keys.join(',');
  if (!keysFromJson.contains('status')) {
    if (keysFromJson.contains('message')) {
      checkResult = {
        'error': true, 
        'message': data['message'] != 'invalid_token' ? errorGeneral : data['message']
      };
    } else {
      checkResult = {
        'error': true, 
        'message': errorGeneral
      };
    }
  } else if (data['status'] == 'error') {
    if (keysFromJson.contains('errors')) {
      checkResult = {
        'error': true, 
        'message': data['errors'].runtimeType == String ? data['errors'] : errorGeneral
      };
    } else if (keysFromJson.contains('error')) {
      checkResult = {
        'error': true, 
        'message': data['error'].runtimeType == String ? data['error'] : errorGeneral
      };
    } else {
      checkResult = {
        'error': true, 
        'message': errorGeneral
      };
    }
  } else {
    checkResult = {
      'error': false, 
      'message': null
    };
  }
  return checkResult;
}

showToastWidget(
  String message,
  {
    String duration = 'short',
    String position = 'top',
    int durationIos = 5,
    Color bgColor = LLSColors.DARK,
    Color textColor = LLSColors.WHITE,
    double fontSize = 12.0
  }
) {
  Toast toastDuration;
  if (duration == 'long') {
    toastDuration = Toast.LENGTH_LONG;
  } else {
    toastDuration = Toast.LENGTH_SHORT;
  }
  ToastGravity toastPosition;
  if (position == 'center') {
    toastPosition = ToastGravity.CENTER;
  } else if (position == 'bottom') {
    toastPosition = ToastGravity.BOTTOM;
  } else {
    toastPosition = ToastGravity.TOP;
  }
  Fluttertoast.showToast(
    msg: message,
    toastLength: toastDuration,
    gravity: toastPosition,
    timeInSecForIosWeb: durationIos,
    backgroundColor: bgColor,
    textColor: textColor,
    fontSize: fontSize
  );
}