import stringify from '@/utils/querystring'
export const _router = {
  mode: 'history',
  switchTab,
  push,
  replace,
  go,
  back,
  reLaunch,
}
export function handleAuth() {
  /**
   *	如何判断权限?
   *	用户如果登录了系统，会留下两个东西，一个是token，一个是userInfo
   *	token存在会过期的问题，如果长时间没有打开小程序，会导致登录失效，出现打开一个页面瞬间跳转到授权页面的问题
   *		解决办法，保存token的时候加上过期时间，每次请求都取一下缓存里的token
   *	userInfo只是用来限时用户信息，作用并不是很大
   * 	ps：只需要判断 token 是否存在即可
   */
  // if (cookie.get('login_status')) {
  //   return true
  // }
  return true
}

export const handleLoginStatus = (location, complete, fail, success) => {
  // 不登录可访问的页面
  let page = [
    {
      path: '/pages/home/index',
      name: '首页',
    }
  ]

  // 是否可以访问
  let isAuth = false

  // 从 location 中获取当前url，location typeof string || object
  let path = ''
  if (typeof location === 'string') {
    path = location
  } else {
    path = location.path
  }
  // 判断用户是否有token
  if (!handleAuth()) {
    page.map(item => {
      if (item.path == path) {
        isAuth = true
      }
    })
  } else {
    isAuth = true
  }
  return new Promise((resolve, reject) => {
    resolve({
      url: parseUrl(location),
      complete,
      fail,
      success,
    })
    // if (isAuth) {
    //   // 有token
    //   if (path == '/pages/home/index' || path == '/pages/shop/GoodsClass/index' || path == '/pages/shop/ShoppingCart/index' || path == '/pages/user/User/index') {
    //     // switchTab({
    //     // 	path: parseUrl(location),
    //     // })
    //     // return
    //   }

    //   resolve({
    //     url: parseUrl(location),
    //     complete,
    //     fail,
    //     success,
    //   })
    // } else {
    //   // 没有token，先校验用户是否授权，如果授权了，进行自动登录
    //   routerPermissions(parseUrl(location))
    //   reject()
    // }
  })
}
export function switchTab(location, complete, fail, success) {
  handleLoginStatus(location, complete, fail, success)
    .then(params => {
      uni.switchTab(params)
    })
    .catch(error => {
      // 没有权限
    })
}
export function push(location, complete, fail, success) {
  handleLoginStatus(location, complete, fail, success)
    .then(params => {
      uni.navigateTo(params)
    })
    .catch(error => {
      // 没有权限
    })
}
export function replace(location, complete, fail, success) {
  handleLoginStatus(location, complete, fail, success)
    .then(params => {
      uni.redirectTo(params)
    })
    .catch(error => {
      // 没有权限
    })
}

export function reLaunch(location, complete, fail, success) {
  handleLoginStatus(location, complete, fail, success)
    .then(params => {
      uni.reLaunch(params)
    })
}

export function go(delta) {
  uni.navigateBack({
    delta,
  })
}

export function back() {
  uni.navigateBack({
    delta: 1,
    success: function(e) {},
    fail: function(e) {},
  })
}
export function parseRoute($mp) {
  const _$mp = $mp || {}
  const path = _$mp.page && _$mp.page.route
  return {
    path: `/${path}`,
    params: {},
    query: _$mp.query || _$mp.page.options,
    hash: '',
    fullPath: parseUrl({
      path: `/${path}`,
      query: _$mp.query || _$mp.page.options,
    }),
    name: path && path.replace(/\/(\w)/g, ($0, $1) => $1.toUpperCase()),
  }
}
export function parseUrl(location) {
  if (typeof location === 'string') return location
  const { path, query } = location

  const queryStr = stringify(query)

  if (!queryStr) {
    return path
  }

  return `${path}?${queryStr}`
}
