import { OpenLocation, OpenLocationOptions, OpenLocationSuccessImpl, OpenLocationErrorCode } from "../interface.uts"
import { OpenLocationFailImpl } from "../unierror.uts"

export const openLocation : OpenLocation = function (options : OpenLocationOptions) {
  const uuid = `${Date.now()}${Math.floor(Math.random() * 1e7)}`
  const baseEventName = `uni_open_location_${uuid}`
  const readyEventName = `${baseEventName}_ready`
  const optionsEventName = `${baseEventName}_options`
  const successEventName = `${baseEventName}_success`
  const failEventName = `${baseEventName}_fail`
  
  const readyEventId = uni.$on(readyEventName, () => {
    uni.$emit(optionsEventName, JSON.parse(JSON.stringify(options)))
  })
  const successEventId = uni.$on(successEventName, () => {
    const res = new OpenLocationSuccessImpl()
    options.success?.(res)
    options.complete?.(res)
  })
  const failEventId = uni.$on(failEventName, (errCode : OpenLocationErrorCode) => {
    const res = new OpenLocationFailImpl(errCode)
    options.fail?.(res)
    options.complete?.(res)
  })
  uni.openDialogPage({
    url: `/uni_modules/uni-openLocation/pages/openLocation/openLocation?readyEventName=${readyEventName}&optionsEventName=${optionsEventName}&successEventName=${successEventName}&failEventName=${failEventName}`,
    fail(err) {
      const res = new OpenLocationFailImpl(4)
      options.fail?.(res)
      options.complete?.(res)
      uni.$off(readyEventName, readyEventId)
      uni.$off(successEventName, successEventId)
      uni.$off(failEventName, failEventId)
    }
  })
};
