export class Router {
  constructor() {
    this.interceptor = {
      before: null,
      after: null
    }
  }

  /**
   * 保留当前页面，跳转到应用内的某个页面，使用navigateBack可以返回到原页面。
   * @description navigateTo, redirectTo 只能打开非 tabBar 页面。
   * @param {*} options
   */
  navigateTo(
    options = {
      url: '',
      animationType: 'pop-in',
      animationDuration: 300,
      success: () => {},
      fail: () => {},
      complete: () => {}
    }
  ) {
    if (this.interceptor.before) {
      let newOptions = this.interceptor.before(options)
      if (newOptions) {
        options = newOptions
      } else {
        return
      }
    }
    uni.navigateTo({
      url: options.url,
      animationType: options.animationType,
      animationDuration: options.animationDuration,
      success: options.success,
      fail: options.fail,
      complete: options.complete
    })
    if (this.interceptor.after) {
      let newOptions = this.interceptor.after(options)
      if (newOptions) {
        options = newOptions
      }
    }
  }

  /**
   * 关闭当前页面，跳转到应用内的某个页面。
   * @description navigateTo, redirectTo 只能打开非 tabBar 页面。
   * @param {*} options
   */
  redirectTo(
    options = {
      url: '',
      success: () => {},
      fail: () => {},
      complete: () => {}
    }
  ) {
    if (this.interceptor.before) {
      let newOptions = this.interceptor.before(options)
      if (newOptions) {
        options = newOptions
      } else {
        return
      }
    }
    uni.redirectTo({
      url: options.url,
      success: options.success,
      fail: options.fail,
      complete: options.complete
    })
    if (this.interceptor.after) {
      let newOptions = this.interceptor.after(options)
      if (newOptions) {
        options = newOptions
      }
    }
  }

  /**
   * 关闭所有页面，打开到应用内的某个页面。
   * @description reLaunch 可以打开任意页面。
   * @param {*} options
   */
  reLaunch(
    options = {
      url: '',
      success: () => {},
      fail: () => {},
      complete: () => {}
    }
  ) {
    if (this.interceptor.before) {
      let newOptions = this.interceptor.before(options)
      if (newOptions) {
        options = newOptions
      } else {
        return
      }
    }
    uni.reLaunch({
      url: options.url,
      success: options.success,
      fail: options.fail,
      complete: options.complete
    })
    if (this.interceptor.after) {
      let newOptions = this.interceptor.after(options)
      if (newOptions) {
        options = newOptions
      }
    }
  }

  /**
   * 跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面。
   * @description switchTab 只能打开 tabBar 页面。
   * @param {*} options
   */
  switchTab(
    options = {
      url: '',
      success: () => {},
      fail: () => {},
      complete: () => {}
    }
  ) {
    if (this.interceptor.before) {
      let newOptions = this.interceptor.before(options)
      if (newOptions) {
        options = newOptions
      } else {
        return
      }
    }
    uni.switchTab({
      url: options.url,
      success: options.success,
      fail: options.fail,
      complete: options.complete
    })
    if (this.interceptor.after) {
      let newOptions = this.interceptor.after(options)
      if (newOptions) {
        options = newOptions
      }
    }
  }

  /**
   * 关闭当前页面，返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈，决定需要返回几层。
   * @param {*} options
   */
  navigateBack(
    options = {
      delta: 1,
      animationType: 'pop-out',
      animationDuration: 300
    }
  ) {
    if (this.interceptor.before) {
      let newOptions = this.interceptor.before(options)
      if (newOptions) {
        options = newOptions
      } else {
        return
      }
    }
    uni.navigateBack({
      url: options.url,
      animationType: options.animationType,
      animationDuration: options.animationDuration,
      complete: options.complete
    })
    if (this.interceptor.after) {
      let newOptions = this.interceptor.after(options)
      if (newOptions) {
        options = newOptions
      }
    }
  }
}
