import {
  parseQuery
} from 'uni-shared'

import {
  showWebview
} from './util'

import {
  setStatusBarStyle,
  invoke
} from '../../bridge'

export function navigateTo ({
  url,
  animationType,
  animationDuration
}, callbackId) {
  const urls = url.split('?')
  const path = urls[0]

  const query = parseQuery(urls[1] || '')

  UniServiceJSBridge.emit('onAppRoute', {
    type: 'navigateTo',
    path
  })

  showWebview(
    __registerPage({
      path,
      query,
      openType: 'navigate'
    }),
    animationType,
    animationDuration,
    () => {
      invoke(callbackId, {
        errMsg: 'navigateTo:ok'
      })
    },
    0
  )

  setStatusBarStyle()
}
