/**
 * Created by yaohx_169 on 2017/6/8.
 */
import { cookie } from './config';
import { AsyncStorage } from 'react-native';

export async function getToken() {
  return AsyncStorage.getItem(cookie.token);
}

export async function setToken(token) {
  return AsyncStorage.setItem(cookie.token, token);
}

export async function delToken() {
  return AsyncStorage.removeItem(cookie.token);
}

export async function getUser() {
  return {
    id: await AsyncStorage.getItem(cookie.userId),
    name: await AsyncStorage.getItem(cookie.userName),
  };
}

export async function setUser(id, name) {
  await AsyncStorage.setItem(cookie.userId, id);
  await AsyncStorage.setItem(cookie.userName, name);
}

export async function delUser() {
  await AsyncStorage.removeItem(cookie.userId);
  await AsyncStorage.removeItem(cookie.userName);
}

export async function getDomain() {
  return {
    name: await AsyncStorage.getItem(cookie.domainName),
    path: await AsyncStorage.getItem(cookie.domainPath),
  };
}

export async function setDomain(name, path) {
  await AsyncStorage.setItem(cookie.domainName, name);
  await AsyncStorage.setItem(cookie.domainPath, path);
}

export async function delDomain() {
  await AsyncStorage.removeItem(cookie.domainName);
  await AsyncStorage.removeItem(cookie.domainPath);
}

export async function isAuthed() {
  return getToken().then(result => !!result);
}

export async function hasDomain() {
  return getDomain().then(result => !!result);
}
