const NEED_UPDATE = 2;
const CHOOSE_UPDATE = 1;
const NO_UPDATE = 0;

class CheckUpdate{
	constructor() {
	}
	
	checkWgtuVersion(min, max){
		console.log(min);
		console.log(max);
		let wgtu = this.checkVersion(min.ver, max.ver);
		console.log(wgtu);
		let support = false;
		let type = "";
		if(plus.os.name == 'Android'){
			type = 'android';
		}else if(plus.os.name == 'iOS'){
			type = 'ios';
		}
		if(wgtu){
			if(max.support[type] < plus.runtime.version){
				support = true;
			}
		}
		let wgtuSupport = {
			"wgtu": wgtu,
			"support": support
		}
		console.log(wgtuSupport);
		return wgtuSupport;
	}
	
	checkVersion(min, max){
		let minVer = this.transformVersion(min);
		let maxVer = this.transformVersion(max);
		console.log(minVer+maxVer);
		console.log(plus.runtime.version);
		let nowVer = this.transformVersion(plus.runtime.version);
		console.log(minVer);
		console.log(maxVer);
		console.log(nowVer);
		if(nowVer < minVer){
			//当前版本小于最低版本，必须更新app
			console.log("必须更新app");
			return NEED_UPDATE;
		}else if(nowVer < maxVer){
			//当前版本介于最低版本和最高版本之间，可选择更新app
			console.log("可选择更新app");
			return CHOOSE_UPDATE;
		}else{
			//等于当前版本不用更新
			console.log("不用更新");
			return NO_UPDATE;
		}
	}
	
	//将版本号转化成
	transformVersion(v) {
		var array = v.split(".");
        var floatVersion = "";
        console.log("下面是获取系统版本");
        console.log(array);
        if(array.length>2){
            for (var i = 0; i < array.length; i++) {
            	if(i == 1){
            		floatVersion = floatVersion + '.'+ array[i]
            	}else{
                	floatVersion = floatVersion + array[i];
            	}
            }
        }else{
            floatVersion = v;
        }
        if(isNaN(floatVersion)){
            return null;
        }
        return floatVersion;
	}
}

export default CheckUpdate;