/**
 * 公共配置文件
 * @邠心vbe on 2021/04/07
 */
import {
  Dimensions,
  Platform,
  StatusBar,
  StyleSheet,
  NativeModules,
} from 'react-native';
import DeviceInfo from 'react-native-device-info';
import apiUser from '../api/apiUser';

navigator.geolocation = require('@react-native-community/geolocation');
global.$t = (scop) => (scop);
global.$width = Dimensions.get('window').width;
global.$height = Dimensions.get('window').height;
global.statusHeight = 0;
global.toolbarSize = 56;
global.isIOS = Platform.OS == 'ios';
if (isIOS) {
  const {StatusBarManager} = NativeModules;
  StatusBarManager.getHeight(statusBarHeight => {
    const height = statusBarHeight.height;
    global.toolbarSize = toolbarSize + height;
    global.statusHeight = height;
  });
  global.BRAND = '';
} else {
  global.statusHeight = StatusBar.currentHeight;
  global.BRAND = DeviceInfo.getBrand().toLowerCase();
}

global.currency = '$';
global.accessToken = '';
global.startPage = {};
global.storageSite = [];
global.chargeInfoState = {};

global.userInfo = {
  userPk: -1,
  credit: 0,
  nickname: 'Sign In'
};

global.isLogin = () => {
  return global.accessToken !== '';
};

global.getUserId = () => {
  return global.userInfo.userPk;
};

global.getUserInfo = (resolve, force) => {
  if (isLogin()) {
    if (userInfo.userPk > 0 && !force) {
      //console.log('-->>>>> 获取用户数据');
      if (resolve) resolve(global.userInfo);
    } else {
      apiUser.getProfile().then(res => {
        console.log('-->>>>> global.getUserInfo', res);
        if (res.data) {
          global.userInfo = res.data;
          if (resolve) resolve(res.data);
        }
      }).catch(err => {
        console.info('<<<<<-- global.getUserInfo', err);
      })
    }
  } else {
    if (resolve) resolve(global.userInfo)
  }
}

global.$vh = percent => {
  return (global.$height * percent) / 100;
};

global.$vhs = percent => {
  return global.$height * percent / 100 - global.statusHeight;
};

global.$vht = percent => {
  return global.$height * percent / 100 - toolbarSize;
};

global.$vhts = percent => {
  return global.$height * percent / 100 - toolbarSize - global.statusHeight;
};

global.$vw = percent => {
  return (global.$width * percent) / 100;
};

global.$padding = (top, right, bottom, left) => {
  if (top == undefined) {
    return {};
  }
  if (right == undefined) {
    right = top;
  }
  if (bottom == undefined) {
    bottom = top;
  }
  if (left == undefined) {
    left = right;
  }
  
  return {
    paddingTop: top,
    paddingLeft: left,
    paddingRight: right,
    paddingBottom: bottom
  };
}

global.$margin = (top, right, bottom, left) => {
  if (top == undefined) {
    return {};
  }
  if (right == undefined) {
    right = top;
  }
  if (bottom == undefined) {
    bottom = top;
  }
  if (left == undefined) {
    left = right;
  }
  return {
    marginTop: top,
    marginLeft: left,
    marginRight: right,
    marginBottom: bottom
  };
}

global.$borderRadius = (topLeft, topRight, bottomRight, bottomLeft) => {
  if (topLeft == undefined) {
    return {};
  }
  if (topRight == undefined) {
    topRight = topLeft;
  }
  if (bottomRight == undefined) {
    bottomRight = topLeft;
  }
  if (bottomLeft == undefined) {
    bottomLeft = topRight;
  }
  return {
    borderTopLeftRadius: topLeft,
    borderTopRightRadius: topRight,
    borderBottomLeftRadius: bottomLeft,
    borderBottomRightRadius: bottomRight
  };
}

global.ui = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: pageBackground
  },
  flex: {
    flexDirection: 'row'
  },
  flexc: {
    alignItems: 'center',
    flexDirection: 'row'
  },
  flexcc: {
    alignItems: 'center',
    flexDirection: 'row',
    justifyContent: 'center'
  },
  flexcw: {
    alignItems: 'center',
    flexDirection: 'row',
    justifyContent: 'space-between'
  },
  flexvc: {
    alignItems: 'center',
    justifyContent: 'center'
  },
  flex1: {
    flex: 1
  },
  flex2: {
    flex: 2
  },
  flex3: {
    flex: 3
  },
  flex4: {
    flex: 4
  },
  flex5: {
    flex: 5
  },
  center: {
    textAlign: 'center',
    alignItems: 'center'
  },
  right: {
    textAlign: 'right',
    
  },
  link: {
    color: "#0684D4"
  },
  bold: {
    fontWeight: 'bold'
  },
  underline: {
    textDecorationLine: "underline"
  },
  blackText: {
    color: '#000'
  },
  mainText: {
    color: colorAccent
  },
  button: {
    flex: 1,
    height: 50,
    elevation: 2,
    borderRadius: 8,
    marginBottom: 8,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: colorAccent
  },
  buttonMini: {
    height: 42,
    borderRadius: 6,
    paddingLeft: 16,
    paddingRight: 16,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: colorAccent
  },
  buttonText: {
    color: textPrimary,
    fontSize: 17,
    fontWeight: 'bold',
    textAlign: 'center',
  },
  noData: {
    color: '#aaa',
    fontSize: 12,
    padding: 16,
    textAlign: 'center'
  }
});

/*if (!__DEV__) {
  global.console = {
    info: () => {},
    log: () => {},
    warn: () => {},
    error: () => {},
  };
}*/