import app from '@system.app'
import device from '@system.device'
import prompt from '@system.prompt'
import shortcut from '@system.shortcut'
import router from '@system.router'
import push from '@service.push'

const SHORT_CUT_FORBIDDEN_INFO = 'User forbidden'

// 获取设备信息
const getDeviceInfo = () => {
  device.getDeviceId({
    success: ret => {
      console.log(ret)
    }
  })
}

//获取订阅推送
const getPushRedId = fn => {
  push.subscribe({
    success: ret => {
      fn && fn(ret ? ret.regId : '')
    }
  })
}

//获取应用信息
const getAppInfo = () => {
  const appInfo = app.getInfo()
  return appInfo
}

//获取provider
const getProvider = () => {
  const provider = push.getProvider()
  return provider
}

//获取设备明细
const getAppDev = fn => {
  device.getInfo({
    success: ret => {
      fn && fn(ret)
    }
  })
}
//校验手机号
const validatePhone = phone => {
  const VALIDATE_REG = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[0-9]|18[0-9]|14[57]|19[89])[0-9]{8}$/
  const isMobile = VALIDATE_REG.test(phone)
  return isMobile
}
//获取是否创建图标
const getShortCut = fn => {
  shortcut.hasInstalled({
    success: ret => {
      fn(ret)
    }
  })
}
// 判断用户是否创建图标
const hasCreateShortCut = (isPayPage = false, fn) => {
  shortcut.hasInstalled({
    success: ret => {
      if (!ret) {
        shortcut.install({
          message: '添加【阅乐】到桌面，方便下次阅读',
          success: () => {
            prompt.showToast('添加成功！')
            fn && fn(true)
          },
          fail: code => {
            fn && fn(false)
            if (code === SHORT_CUT_FORBIDDEN_INFO && !isPayPage) app.exit()
          }
        })
      } else {
        fn && fn(false)
      }
    }
  })
}

//弱加桌
const loseLevelShortCut = fn => {
  shortcut.hasInstalled({
    success: ret => {
      if (!ret) {
        shortcut.install({
          message: '添加【阅乐】到桌面，方便下次阅读',
          complete: () => {
            fn && fn(true)
          }
        })
      } else {
        fn && fn(false)
      }
    }
  })
}

//强制加桌
const mustCreateShort = fn => {
  shortcut.hasInstalled({
    success: ret => {
      if (!ret) {
        shortcut.install({
          message: '添加【阅乐】到桌面，方便下次阅读',
          success: () => {
            prompt.showToast('添加成功！')
            fn && fn(true)
          },
          fail: code => {
            fn && fn(false, code)
            prompt.showToast('取消操作')
          }
        })
      } else {
        fn && fn(false)
      }
    }
  })
}

//用于返回时提醒加桌
const backCreateShortCut = fn => {
  shortcut.hasInstalled({
    success: ret => {
      if (!ret) {
        shortcut.install({
          message: '添加【阅乐】到桌面，方便下次阅读',
          success: () => {
            prompt.showToast('添加成功！')
            fn && fn('back')
          },
          fail: code => {
            console.log('我失败了我失败了')
            fn && fn('back')
          }
        })
      } else {
        fn && fn('back')
      }
    }
  })
}

// 右上角菜单点击
const showMenu = () => {
  const appInfo = app.getInfo()
  prompt.showContextMenu({
    itemList: ['保存桌面', '关于', '取消'],
    success: function(ret) {
      switch (ret.index) {
        case 0:
          // 保存桌面
          hasCreateShortCut()
          break
        case 1:
          // 关于
          router.push({
            uri: '/views/About',
            params: {
              name: appInfo.name,
              icon: appInfo.icon
            }
          })
          break
        case 2:
          // 取消
          break
        default:
          prompt.showToast({
            message: '错误'
          })
      }
    }
  })
}

export default {
  shortcut,
  getDeviceInfo,
  hasCreateShortCut,
  backCreateShortCut,
  showMenu,
  getShortCut,
  getAppInfo,
  getAppDev,
  validatePhone,
  getPushRedId,
  getProvider,
  mustCreateShort,
  loseLevelShortCut
}
