import {
  http
} from '@/utils/request'

const PACKAGE_INFO_KEY = '__package_info__'

export function callCheckVersion() {
  return new Promise((resolve, reject) => {
    // #ifdef APP-PLUS
    const {
      platform
    } = uni.getSystemInfoSync()
    plus.runtime.getProperty(plus.runtime.appid, widgetInfo => {
      // console.log("==========plus");
      // console.log(plus.runtime);
      // console.log("==========widget");
      // console.log(widgetInfo);

      http.get('/v1/app-version', {
        params: {
          cate: platform,
          // v:plus.runtime.versionCode,
          v: process.env.VUE_APP_VERSION_CODE
        },
        custom: {
          toast: false
        }
      }).then(resData => {
        resolve({
          ...resData,
          platform
        })
      })
    })
    // #endif
    // #ifndef APP-PLUS
    reject({
      message: '请在App中使用'
    })
    // #endif
  })
}

export function checkUpdate() {
  // #ifdef APP-PLUS
  return new Promise((resolve, reject) => {
    callCheckVersion().then(e => {
      if (!e) return;
      // if (e.v > plus.runtime.versionCode) {
      // uni.showModal({
      //   title: '提示',
      //   content: `${e.v}:${e.name}======${process.env.VUE_APP_VERSION_CODE}:${process.env.VUE_APP_VERSION}`,
      //   success: function(res) {
      //     if (res.confirm) {
      //       console.log('用户点击确定');
      //     } else if (res.cancel) {
      //       console.log('用户点击取消');
      //     }
      //   }
      // });

      if (e.v > process.env.VUE_APP_VERSION_CODE) {
        const point = e.link.lastIndexOf('.') + 1
        const pp = e.link.substr(point)
        const result = {
          is_silently: pp == 'wgt',
          url: e.link,
          platform: e.platform,
          type: pp == 'wgt' ? 'wgt' : e.platform,
          version: e.v,
          is_mandatory: e.is_force,
          ...e
        }

        resolve(result)

        uni.setStorageSync(PACKAGE_INFO_KEY, result)

        // 静默更新，只有wgt有
        if (result.is_silently) {
          uni.downloadFile({
            url: result.url,
            success: res => {
              if (res.statusCode == 200) {
                // 下载好直接安装，下次启动生效
                plus.runtime.install(res.tempFilePath, {
                  force: true
                }, () => {
                  console.log("热更成功");
                  uni.navigateTo({
                    url: `/pages/upgrade_popup/index?local_storage_key=${PACKAGE_INFO_KEY}`,
                    fail: (err) => {
                      uni.removeStorageSync(PACKAGE_INFO_KEY)
                    }
                  })
                }, (e) => {
                  console.log("热更失败");
                  console.log(e);
                });

              }
            }
          });

          return;
        }


        uni.navigateTo({
          url: `/pages/upgrade_popup/index?local_storage_key=${PACKAGE_INFO_KEY}`,
          fail: (err) => {
            uni.removeStorageSync(PACKAGE_INFO_KEY)
          }
        })
      }
    }).catch(err => {
      reject(err)
    })
  })
  // // #endif
}
