const android_init = `https://d3aq4b0oo6q8pv.cloudfront.net/${Merchant}/channel/apk/0150JTF9.apk`
let apkLink = android_init
const ios_init = 'https://kkk.6u93xjwvrl.vip/'
let ios = ios_init
const search = new URLSearchParams(window.location.search)
const official = '0150JTF9'
const channelCode = search.get('channelCode') && search.get('channelCode') !== 'null' ? search.get('channelCode') : official
const inviteCode = search.get('inviteCode') || ''
let tg = null
let sw = null
let agent = null
let loading = null

let downloadChannelCode = null
const uuid = randomString(24)

const iosClick = (ios) => {
  useTip()
  if (inviteCode) {
    window.location.href = ios + `/front/cluser/c/ios/invite/file?merchantAcct=${Merchant}&inviteCode=${inviteCode}&uuid=${uuid}`
  } else {
    window.location.href = ios + `/front/cluser/c/ios/invite/file?merchantAcct=${Merchant}&inviteCode=${channelCode}&uuid=${uuid}&codeType=agentCode`
  }
  api(channelCode || official, 3)
}

useSetting().then(res => {
  if (res) {
    useTg()
    if (window.location.href.includes('phone')) {
      loading = new Loading()
      loading.show()
      useAd()
      if (device === 'android') {
        const box = document.querySelector('.box')
        if (json && json.android && json.agent) {
          for (let index = 0; index < json.android.length; index++) {
            const img = document.createElement('img')
            if (index === 0) {
              img.src = '/img/phone/d-img.png?a=51'
            } else {
              img.src = '/img/phone/by.png?a=5'
            }
            // 为 div 添加点击事件
            img.addEventListener('click', function () {
              useTip()
              // 这里可以添加其他逻辑，比如跳转到下载链接
              window.location.href = json.android[index] + (json.agent[channelCode] || json.agent[official]);
              api(channelCode || official, 2)
            });
            box.appendChild(img)
          }
        } else {
          const img = document.createElement('img')
          // 为 div 添加点击事件
          img.addEventListener('click', function () {
            // 这里可以添加其他逻辑，比如跳转到下载链接
            window.location.href = android_init
          });
          box.appendChild(img)
        }
        loading.hide()
      } else {
        const box = document.querySelector('.box')
        for (let index = 0; index < json.ios.length; index++) {
          const img = document.createElement('img')
          if (index === 0) {
            img.src = '/img/phone/d-img.png?a=51'
          } else {
            img.src = '/img/phone/by.png?a=5'
          }
          img.addEventListener('click', function () {
            // 这里可以添加其他逻辑，比如跳转到下载链接
            iosClick(json.ios[index])
          });
          box.appendChild(img)
          loading.hide()
        }
      }
    }
  }
})

$(document).ready(async function () {
  $('body').css('height', window.innerHeight)
  api(channelCode || official, 1)

  $('.tg').on('click', function () {
    window.open(tg, '_blank')
  })

  $('.sw').on('click', function () {
    window.open(sw, '_blank')
  })

  $('.agent').on('click', function () {
    window.open(agent, '_blank')
  })

  $('.dialog').on('click', () => $('.dialog').removeClass('show'))
})

const useAd = () => {
  if (json && json.ad) {
    // 广告位渲染
    const links = document.getElementsByClassName('a-link')
    const names = document.getElementsByClassName('a-name')
    if (json.ad && links && names) {
      for (let index = 0; index < json.ad.length; index++) {
        if (json.ad[index]) {
          links[index].href = json.ad[index].url
          names[index].innerHTML = json.ad[index].label
        }
      }
    }
  }
}

const useTg = () => {
  if (json && json.tg) {
    tg = json.tg.telegram_group
    sw = json.tg.telegram_business
    agent = json.tg.telegram_channel
  }
}

const useTest = async () => {
  try {
    downloadChannelCode = channelCode
    const url = await requestMultiple(json.ios)
    if (url) {
      ios = url
    }
  } catch (error) {
    throw error
  } finally {
    loading.hide()
  }
}

const useTip = () => {
  layer.open({ content: '应用正在下载中,十秒后无反应请再次点击下载!', skin: 'msg', time: 2 })
  $('.dialog').addClass('show')
}

