import {getVersion} from "../api";

const updateVersion = {
    data() {
        return {
            updateVersion: {
                modalShow: false,
                showConfirmButton: false,
                showCancelButton: false,
                confirmText: '',
                cancelText: '',
                modalContent: '',
                confirmType: '',
                newVersionUrl: '',
                downloadCompleteTemporaryPath: '',
            },
			platform:'',
            version:''
        }
    },
    methods: {
        async fnUpdateVersion(toast) {
            var _this = this;
                  uni.getSystemInfo({
                    success: (res) => {
                        console.info('55')
                        console.info(res.platform)
                      _this.platform = res.platform
                    }
                  });
            // #ifdef APP-PLUS
            plus.runtime.getProperty(plus.runtime.appid, function (inf) {
                _this.version = inf.version;  //获取当前版本号
            });
            // #endif
            console.info('2')
		await _this.getUrlVarsion(toast)
        },
		async getUrlVarsion(toast){
			// #ifdef APP-PLUS
			let resData = await getVersion()
			 if(this.version < resData.pd.VERSION){
				 if (this.platform=="android") {
				     this.updateVersion.newVersionUrl = this.$store.state.filePath + resData.pd.FILEURL
				     uni.getNetworkType({
				         success: ({
				                       networkType
				                   }) => {
				             if (networkType === 'wifi') {
				                 this.updateVersion.modalContent = '发现新版本,是否更新?为了更好的体验,请更新到最新版本。'
				             } else {
				                 this.updateVersion.modalContent =
				                     '发现新版本,检查到您当前使用的是移动网络,是否更新?更新时请注意流量消耗。为了更好的体验,请更新到最新版本。'
				             }
				             this.updateVersion.confirmType = 'download'
				             this.updateVersion.confirmText = '立即更新'
				             this.updateVersion.cancelText = '稍后更新'
				             this.updateVersion.showConfirmButton = true;
				             this.updateVersion.showCancelButton = true;
				             this.updateVersion.modalShow = true;
				             uni.hideTabBar({
				                 animation: true
				             })
				         }
				     })
				 } else {
                     plus.nativeUI.confirm( "请保持应用最新版本,否则将无法使用。", function(e){
                         if(e.index == 0){
                             var url='itms-apps://itunes.apple.com/cn/app/id6448770162?mt=8';// 应用在appstore的地址
                             plus.runtime.openURL(url);
                             setTimeout(function() {
                                 const threadClass = plus.ios.importClass("NSThread");
                                 const mainThread = plus.ios.invoke(threadClass, "mainThread");
                                 plus.ios.invoke(mainThread, "exit");
                             }, 2000);
                         }
                     }, "下载更新", ["下载"] );
				 }

			 }
			// #endif
            // #ifdef MP-WEIXIN
            const updateManager = uni.getUpdateManager();
            updateManager.onCheckForUpdate(function (res) {
                // 请求完新版本信息的回调
                console.log(res.hasUpdate,'版本信息');
                if (res.hasUpdate) {
                    updateManager.onUpdateReady(function (res) {
                        uni.showModal({
                            title: '更新提示',
                            content: '新版本已经准备好,是否重启应用?',
                            success(res) {
                                if (res.confirm) {
                                    // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
                                    updateManager.applyUpdate();
                                }
                            }
                        });
                    });
                }
            });
            // #endif
		},
        modalConfirm() {
            this.updateVersion.confirmType === 'download' ? this.downloadNewVersion() : this.installNewVersion()
        },
        modalCancel() {
            this.updateVersion.modalShow = false
            uni.showTabBar({
                animation: true
            })
        },
        downloadNewVersion() {
            this.updateVersion.showConfirmButton = false;
            this.updateVersion.showCancelButton = false;
            this.updateVersion.modalContent = '正在下载新版本,请稍后...'
            const downloadTask = uni.downloadFile({
                url: this.updateVersion.newVersionUrl,
                success: (res) => {
                    this.updateVersion.downloadCompleteTemporaryPath = res.tempFilePath
                }
            });
            downloadTask.onProgressUpdate((res) => {
                this.updateVersion.modalContent = `正在下载新版本,请稍后...<br>已经下载${res.progress}%`
                if (res.progress === 100) {
                    this.updateVersion.showConfirmButton = true;
                    this.updateVersion.confirmType = 'install'
                    this.updateVersion.confirmText = '立即安装'
                    this.updateVersion.modalContent = '下载成功,点击立即安装进行安装。'
                }
            });
        },
        installNewVersion() {
            plus.runtime.install(
                this.updateVersion.downloadCompleteTemporaryPath, {
                    force: true
                },
                function (res) {
                    uni.showTabBar({
                        animation: true
                    })
                    plus.runtime.restart();
                }
            );
        },
    }
}
export default updateVersion