import { Dimensions, Platform } from 'react-native';
import { Colors } from './../theme';

const { height, width } = Dimensions.get('window');

/**
 * Check device
 */
export const isIphoneX = () => {
  return (
    // This has to be iOS duh
    Platform.OS === 'ios' &&
    // Accounting for the height in either orientation
    (height >= 812 || width >= 812)
  );
};

export const validateEmail = (email: any) => {
  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

  if (!email || email.length === 0) {
    return { success: false, message: 'Email Id is required!' };
  }
  if (reg.test(email) === false) {
    return { success: false, message: 'Invalid Email Address!' };
  }
  return { success: true, message: '' };
};

/**
 * Http query builder
 * @param d     Http paramter
 */
export const buildURLQuery = (d: any) =>
  Object.keys(d)
    .filter(k => d[k] !== null)
    .map(k => [k, d[k]].map(encodeURIComponent).join('='))
    .join('&');

export const getStatus = (statusKey: any) => {
  let status = '';
  switch (statusKey) {
    case 1: //Reserved
      status = 'Reserved';
      break;
    case 2: //Issued
      status = 'Issued';
      break;
    case 3: //Returned
      status = 'Returned';
      break;
    case 4: //Unknown
      status = 'Unknown';
      break;
    case 5: //Unreserved
      status = 'Unreserved';
      break;
    case 6: //Lost
      status = 'Lost';
      break;
    case 7: //Damaged
      status = 'Damaged';
      break;
    default:
      status = 'Unknown';
      break;
  }
  return status;
};

export const getColor = (status: any) => {
  let color = '';
  switch (status) {
    case 1: //Reserved
      color = Colors.skyblue;
      break;
    case 2: //Issued
      color = Colors.orange;
      break;
    case 3: //Returned
      color = Colors.black;
      break;
    case 4: //Unknown
      color = Colors.black;
      break;
    case 5: //Unreserved
      color = Colors.skyblue;
      break;
    case 6: //Lost
      color = Colors.red;
      break;
    case 7: //Damaged
      color = Colors.yellow;
      break;
    default:
      color = Colors.black;
      break;
  }
  return color;
};

export const getBookRequestColor = (status: any) => {
  let color = '';
  switch (status) {
    case 0: //Pending
      color = Colors.orange;
      break;
    case 1: //Approved
      color = Colors.green;
      break;
    case 2: //Available
      color = Colors.skyblue;
      break;
    case 3: //Rejected
      color = Colors.red;
      break;
    default:
      color = Colors.black;
      break;
  }
  return color;
};

export const getBookRequestStatus = (statusKey: any) => {
  let status = '';
  switch (statusKey) {
    case 0: //Pending
      status = 'Pending';
      break;
    case 1: //Approved
      status = 'Approved';
      break;
    case 2: //Available
      status = 'Available';
      break;
    case 3: //Rejected
      status = 'Rejected';
      break;
    default:
      status = 'Unknown';
      break;
  }
  return status;
};
