import { Dimensions, StatusBar, Platform, Linking } from 'react-native';
import pxToDp from './pxToDp';
import { getStatusBarHeight } from 'react-native-safearea-height';
import * as _ from 'lodash';
import myToast, { Toast } from './myToast';
export default {
  width: Dimensions.get('window').width,
  height:
    Platform.OS === 'ios'
      ? Dimensions.get('window').height
      : Dimensions.get('window').height / Dimensions.get('window').width > 1.8
        ? Dimensions.get('window').height + getStatusBarHeight()
        : Dimensions.get('window').height,
  statusBarHeight: getStatusBarHeight(),
  bottomTabHeight: (Platform.OS === 'ios' && Dimensions.get('window').height / Dimensions.get('window').width > 1.8?34:0) + pxToDp(98),
  isIOS: Platform.OS == 'ios',
  isNullOrEmpty(strings) {
    return !strings || String(strings).replace(/(^s*)|(s*$)/g, '').length == 0;
  },
  stringToNumberStr(s) {
    console.log(16, String(Number(s.replace(/[^0-9]/g, ''))));
    return String(Number(s.replace(/[^0-9]/g, '')));
  },
  isIOSFullScreen() {
    const dimen = Dimensions.get('window');
    return (
      Platform.OS === 'ios' &&
      !Platform.isPad &&
      !Platform.isTVOS &&
      (dimen.height === 780 ||
        dimen.width === 780 ||
        dimen.height === 812 ||
        dimen.width === 812 ||
        dimen.height === 844 ||
        dimen.width === 844 ||
        dimen.height === 896 ||
        dimen.width === 896 ||
        dimen.height === 926 ||
        dimen.width === 926)
    );
  },
  //写一个方法，传入一个数组，判断数量是否是n的倍数，如果不是，补null
  getNewArr(arr, n) {
    let count = n - arr.length % n;
    for (let i = 0; i < count; i++) {
      arr.push(null);
    }
    return arr;
  },
  //获取随机颜色
  getRandomColor() {
    return '#' + ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).slice(-6);
  },
  goPage(navigation, type, page, params) {
    console.log(44, type, page, params);
    if(page === '#') {
      myToast.show('敬请期待','error');
      return;
    }
    if (type === 2) {
      console.log(57, '外链')
      Linking.openURL(page);
    } else {
      console.log(60, '内链')
      const json = this.toJSON(_.replace(params, /&quot;/g, '"'));
      navigation.navigate(page, { ...json });
    }
  },
  toJSON(str) {
    try {
      const a = JSON.parse(str);
      return a;
    } catch (error) {
      return {};
    }
  },
  getIntegerAndDecimal(num, type = 1) {
    //type 1:去掉小数点前面的0 2:去掉0.,整数部分加小数点
    let integer = Math.floor(num).toFixed(0)
    let decimal = (num - integer).toFixed(1)
    if (decimal === '0.0') {
      return { integer, decimal: '' }
    }
    if (type === 1) {
      decimal = decimal.replace('0', '')
    } else if (type === 2) {
      decimal = decimal.replace('0.', '')
      integer += '.'
    }
    return { integer, decimal }
  },
  getHideString(str){
    //写一个方法，判断如果是手机号，隐藏中间四位，如果是邮箱，隐藏@前面的，如果是汉字，隐藏后面的
    if(!str) return '';
    if(str.indexOf('@') > -1){
      let arr = str.split('@');
      return arr[0].substr(0,1) + '****' + arr[0].substr(arr[0].length-1) + '@' + arr[1];
    }
    if(str.length === 11){
      return str.substr(0,3) + '****' + str.substr(7);
    }
    if(str.length > 1){
      return str.substr(0,1) + '**';
    }
  },
};
