class Update{
	constructor(curV,newV,url) {//当前版本号、最新版本号以及获取最新安装包地址
	    this.curV = curV;
	    this.newV = newV;
	    this.url = url;
	}
	
	startUpdate(){
		if(this.curV != this.newV){
			console.log("需要更新...");
			this.downWgt();
		}else{
			console.log("不需要更新");
			alert("不需要更新");
			return ; 
		}
	}

	downWgt(){
		plus.nativeUI.showWaiting("下载wgt文件...");
		this.onDownloadStart();
		plus.downloader.createDownload( this.url, {filename:"_doc/update/"}, (d,status)=>{
			if ( status == 200 ) { 
				this.onDownloadSuccess();
				console.log("下载wgt成功："+d.filename);
				this.installWgt(d.filename);	// 安装wgt包
			} else {
				console.log("下载wgt失败！");
				this.onError(status);
				plus.nativeUI.alert("下载wgt失败！");
			}
			plus.nativeUI.closeWaiting();
		}).start();
	}

	installWgt(path){
		this.onInstallStart();
		plus.nativeUI.showWaiting("安装wgt文件...");
		plus.runtime.install(path,{force: true},()=>{ //强制安装 不进行版本校验
			plus.nativeUI.closeWaiting();
			console.log("安装wgt文件成功！");
			this.onInstallSuccess();
			plus.nativeUI.alert("应用资源更新完成！",()=>{
				plus.runtime.restart();
			});
		},(e)=>{
			plus.nativeUI.closeWaiting();
			this.onError(e);
			console.log("安装wgt文件失败["+e.code+"]："+e.message);
			plus.nativeUI.alert("安装wgt文件失败["+e.code+"]："+e.message);
		});
	}

	onDownloadStart(){
		console.log(`download wgt file start`);
	}

	onDownloadSuccess(){
    	console.log(`download wgt file success`);
    }

    onError(){
    	console.log(`operate onError`);
    }

    onInstallStart(){
    	console.log(`install wgt file start`);
    }

    onInstallSuccess(){
    	console.log(`install wgt file success`);
    }

}

export default Update;