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

class CheckUpdate{
	constructor() {
	}
	
	checkWgtuVersion(min, max, nowVer){
		let wgtu = this.checkVersion(min.ver, max.ver, nowVer);
		let support = false;
		let type = "";
		if(plus.os.name == 'Android'){
			type = 'android';
		}else if(plus.os.name == 'iOS'){
			type = 'ios';
		}
		console.log(type);
		if(wgtu){
			console.log(max.support[type]);
			//若是当前的版本小于支持版本，则app必须更新
			if(plus.runtime.version < max.support[type]){
				support = true;
			}
		}
		let wgtuSupport = {
			"wgtu": wgtu,
			"support": support
		}
		return wgtuSupport;
	}
	
	checkVersion(min, max, ver){
		let minVer = this.transformVersion(min);
		let maxVer = this.transformVersion(max);
		let nowVer = this.transformVersion(ver);
		if(nowVer < minVer){
			//当前版本小于最低版本，必须更新app
			return NEED_UPDATE;
		}else if(nowVer < maxVer){
			//当前版本介于最低版本和最高版本之间，可选择更新app
			return CHOOSE_UPDATE;
		}else{
			//等于当前版本不用更新
			return NO_UPDATE;
		}
	}
	
	//将版本号转化成可对比数字
	transformVersion(v) {
		var array = v.split(".");
        var floatVersion = "";
        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;