import { showMessage } from 'react-native-flash-message';

import errors from '../config/errors';

export function getErrorObject(error) {
  switch (error) {
    case errors.AUTH_INVALID_EMAIL: return { 
      type: 'Authentication Error', 
      desc: 'Invalid email format used.' 
    };
    case errors.AUTH_INVALID_PHONE_NUMBER: return { 
      type: 'Authentication Error',
      desc: 'Invalid phone number format is used.' 
    };
    case errors.AUTH_USER_NOT_FOUND: return {
      type: 'Authentication Error',
      desc: 'User is not recognized.'
    };
    case errors.AUTH_WRONG_PASSWORD: return {
      type: 'Authentication Error',
      desc: 'Wrong password.'
    };
    case errors.AUTH_UID_ALREADY_EXISTS: return {
      type: 'Authentication Error',
      desc: 'Tagfer ID already exists.' 
    };
    case errors.AUTH_EMAIL_ALREADY_EXISTS: return {
      type: 'Authentication Error', 
      desc: 'Email already exists.' 
    };
    case errors.AUTH_PHONE_ALREADY_EXISTS: return {
      type: 'Authentication Error', 
      desc: 'Phone number already exists.' 
    };
    case errors.AUTH_PHONE_NOT_CACHED: return { 
      type: 'Authentication Error', 
      desc: 'Phone number is not recognized.' 
    };
    case errors.AUTH_VERIFICATION_CODE_MISMATCH: return { 
      type: 'Authentication Error', 
      desc: 'Verification code mismatch' 
    };
    case errors.AUTH_LINKEDIN_REQUEST_TOKEN_FAILURE: return {
      type: 'Authentication Error',
      desc: 'Could not retrieve your login info'
    };
    case errors.AUTH_LINKEDIN_ACCESS_TOKEN_FAILURE: return {
      type: 'Authentication Error',
      desc: 'Could not log into your account'
    };
    case errors.AUTH_LINKEDIN_PROFILE_REQUEST_FAILURE: return {
      type: 'Authentication Error',
      desc: 'Could not retrieve your profile info'
    };
    case errors.AUTH_LINKEDIN_TOKEN_SCOPE_INVALID_REQUEST_FAILURE: return {
      type: 'Authentication Error',
      desc: 'There was a problem connecting to linkedin login, if you have previously authenticated Tagfer with your account please remove the tagfer app from account and reconnect tagfer.'
    };
    case errors.APP_NETWORK_ERROR: return {
      type: 'Network Error',
      desc: 'Request failed, check your internet connection.'
    };
    case errors.APP_NETWORK_TIMEOUT: return {
      type: 'Network Error',
      desc: 'Timed out while issuing the request. Check your network.'
    };
    case errors.APP_UNABLE_TO_PARSE_RESPONSE: return {
      type: 'Network Error',
      desc: 'Unable to get the response.'
    };

    case errors.APP_FIREBASE_DATABASE_ERROR: return {
      type: 'Network Error',
      desc: 'Unable to save data to our servers.'
    };
    
    default: return {
      type: 'Error',
      desc: 'Oops, an error occured! Please try again.'
    };
  }
}
/**
 * Display a flash message at the top of the screen.
 * @param {String} error one of the error codes in @see config/error.js
 */
export function showFlashErrorMessage(error) {
  const errorObject = getErrorObject(error);
  showMessage({
    message: errorObject.type,
    description: errorObject.desc,
    type: getFlashMessageType(errorObject.type),
    icon: 'auto',
    duration: 8000
  });
}

function getFlashMessageType(errorType) {
  if (errorType === 'Network Error') {
    return 'warning';
  }

  return 'danger';
}
