import { cEvent } from './event'
const wxService = require('./wxService')
const env = require('../config/index').envInfo
function Chain() {
  this.hanlers = {}
}

Chain.prototype = {
  add: function (type, fn) {
    const hanlers = this.hanlers[type] || []
    if (hanlers.length > 1) {
      const preFn = hanlers[length - 1]
      preFn.next = fn
    }
    hanlers.push(fn)
  },
  hanler: function () {
    const args = [].slice.call(arguments)
    const type = args.pop()
    const hanler = this.hanlers[type]
    hanler.apply(this, [hanler.next, ...args])
  },
}

const HandlerLink = function () { }
HandlerLink.prototype = {
  init(type, item = {}) {
    let res = this.currentLinkType(type, item)
    return res
  },
  currentLinkType(type, item={}) {
    // 1. 外部小程序_url  2. 内部动态页面_pageId  3.预览  4. 内部页面 5. 卡券__1_卡包 2_激活领卡
    if (type == 1) {
      wx.navigateToMiniProgram({
        // 跳转外部小程序
        appId: item.link.appid,
        path: `${item.link.url}`,
        success: e => {
          console.log('type=1',e)
        },
        fail: e => {
          console.log('type=1',e)
        },
        complete: e => {
          console.log(e)
        },
      })
      return ''
    } else if (type == 2) {
      return item.link
    } else if (type == 3) {
      return item
    } else if (type == 4) {
      return `${item.link.url}`
    } else if (type == 5) {
      let currentOpen = ''
      if(item.link.url == 1){
        // 卡包
        currentOpen = 1
        return currentOpen
      // wxService.openCardList()
      } else if(item.link.url == 2){
        // 激活领卡--开卡
        currentOpen= 2
        return currentOpen
       // wxService.openCard()
      }
    } else if(type == 6){
      // 跳转路径中包括http的跳转到h5页面中,用web-view去加载h5页面
      if (/http/gi.test(item.link.url)) {
        const enCodeUrl = encodeURIComponent(item.link.url)
        return `/subPackage/page/pages/h5/h5page?url=${enCodeUrl}`
      }
      return `/${item.link.url}`
    } else if(type == 7){
      // 集点详情 /pages/pointDetail/pointDetail?id=610415394393559040
      return item.link.url
    }
  }
}

const getLink = function (type, id, name) {
  const handlerLink = new HandlerLink()
  return handlerLink.init.apply(handlerLink, arguments)
}

const getUrlParam = function (name, url) {
  if (!url) {
    return null
  }
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  var r = url.match(reg)
  if (r != null) return decodeURI(r[2])
  return null
}

class Parmas {
  constructor() {
    this.cache = new Map()
  }
  setObject(key, val) {
    this.cache.set(key, val)
    if (key === '_defaultAddress') {
      cEvent.notify('selectAdress', val)
    }
    if (key === '_posDefaultAddress') {
      cEvent.notify('selectAddress', val)
    }
    console.log(this.cache)
  }
  getObject(key) {
    let res = null
    if (this.cache.has(key)) {
      res = this.cache.get(key)
    }
    return res
  }
  getObjectOnce(key) {
    let res = null
    if (this.cache.has(key)) {
      res = this.cache.get(key)
      this.cache.delete(key)
    }
    return res
  }
}

module.exports = {
  getLink: getLink,
  parmas: new Parmas(),
  getUrlParam: getUrlParam,
  handlerLink: new HandlerLink(),
}
