import { createApp } from 'vue'
import App from './App.vue'
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
import router from '../router/index-student'
import vueFilter from '@/helpers/vueFilter'
import {
  listenerMessage,
  postMessage,
  promisefiyPostMessage
} from '@/helpers/native-message'
import 'normalize.css'
import '../styles/index.less'
import { state } from '@/state'
import qs from 'query-string'
import { browser, setAuth } from '@/helpers/utils'
import request from '@/helpers/request'

const app = createApp(App)

const getClassroomDefaultInfo = async () => {
  state.sourcType = 'kt'
  await promisefiyPostMessage({ api: 'getUserAccount' }).then(
    async (res: any) => {
      const content = res.content
      // state.orchestraInfo.token = content.token.split(' ')[1]
      // state.orchestraInfo.phone = content.phone
      // state.orchestraInfo.nickname = content.nickname
      // state.orchestraInfo.avatar = content.avatar
      // state.orchestraInfo.unionId = content.unionId || 0
      console.log(content, 'orchestra')

      const { data } = await request.post('/api-auth/smsLogin', {
        requestType: 'form',
        data: {
          clientId: 'student',
          clientSecret: 'student',
          phone: content.phone,
          isSurportRegister: true,
          token: content.token.split(' ')[1]
        }
      })
      const { authentication } = data
      setAuth(authentication.token_type + ' ' + authentication.access_token)
    }
  )
}

const initProject = async () => {
  // import Vconsole from 'vconsole'
  // const vconsole = new Vconsole()
  // 设置是否显示导航栏 0 显示 1 不显示
  postMessage({ api: 'setBarStatus', content: { status: 0 } })
  postMessage({
    api: 'backIconChange',
    content: { backIconHide: true }
  })
  postMessage(
    {
      api: 'getVersion'
    },
    (res: any) => {
      state.version = res.content.version
    }
  )

  // 判断是否是管乐团学生端，用来获取基础数据
  if (browser().isOrchestraStudent) {
    // 获取管乐团token
    promisefiyPostMessage({ api: 'getUserAccount' }).then((res: any) => {
      const content = res.content
      state.orchestraInfo.token = content.token.split(' ')[1]
      state.orchestraInfo.phone = content.phone
      state.orchestraInfo.nickname = content.nickname
      state.orchestraInfo.avatar = content.avatar
      state.orchestraInfo.unionId = content.unionId || 0
    })

    // 从缓存里面获取token
    promisefiyPostMessage({
      api: 'getCache',
      content: { key: 'h5-colexiu-token' }
    }).then((res: any) => {
      const content = res.content
      if (content.value) {
        setAuth(content.value)
      }
    })
  }

  // 判断是否是课堂乐器学生端，用来获取基础数据
  const parseSearch: any = qs.parse(location.search)
  const hashSearch = qs.parse(location.hash.split('?')[1])
  const source: string = parseSearch.source || hashSearch.source || ''
  const Authorization: string =
    parseSearch.Authorization || hashSearch.Authorization || ''
  if (source === 'kt') {
    // 获取课堂乐器token
    if (Authorization) {
      state.sourcType = 'kt'
    } else {
      await getClassroomDefaultInfo()
    }
  }

  if (browser().isTeacher) {
    state.platformType = 'TEACHER'
  } else if (browser().isStudent) {
    state.platformType = 'STUDENT'
  } else {
    state.platformType = 'STUDENT'
  }
  if (state.platformType === 'TEACHER') {
    state.platformApi = '/api-teacher'
  } else {
    state.platformApi = '/api-student'
  }

  dayjs.locale('zh-ch')
  app.config.globalProperties.$dayjs = dayjs
  app.config.globalProperties.$filters = vueFilter
  app.use(router)

  app.mount('#app')
}

initProject()
