import { reactive } from 'vue'
import { browser, setAuth } from './helpers/utils'
import { postMessage } from './helpers/native-message'

type status = 'init' | 'login' | 'logout' | 'error'

export const state = reactive({
  user: {
    status: 'init' as status,
    data: {} as any
  },
  platformType: '' as 'STUDENT' | 'TEACHER',
  platformApi: '/api-student' as '/api-student' | '/api-teacher',
  version: '', // 版本号 例如: 1.0.0
  ossUploadUrl: 'https://ks3-cn-beijing.ksyuncs.com/',
  musicCertStatus: false as boolean, // 是否音乐认证
  openLiveStatus: false as boolean // 是否开通直播
})

// 预览上传到oss的地址
export const getOssUploadUrl = (bucket: string) => {
  const tmpBucket = bucket || 'daya'
  return `https://${tmpBucket}.ks3-cn-beijing.ksyuncs.com/`
}

export const setLoginInit = () => {
  state.user.status = 'init'
  state.user.data = null
}

export const setLogin = (data: any) => {
  state.user.status = 'login'
  state.user.data = data
}

export const setLogout = () => {
  state.user.status = 'logout'
  state.user.data = null
}

export const setLoginError = () => {
  state.user.status = 'error'
  state.user.data = null
}

// 用于处理跳转地址，如果是在app内，则打开一个新的webview, 否则跳转连接
export const openDefaultWebView = (url?: string, callBack?: any) => {
  if (browser().isApp) {
    postMessage({
      api: 'openWebView',
      content: {
        url,
        orientation: 1,
        isHideTitle: false
      }
    })
  } else {
    callBack && callBack()
  }

}