
function str2asc(strstr){
    return ("0"+strstr.charCodeAt(0).toString(16)).slice(-2);
}

function asc2str(ascasc){
    return String.fromCharCode(ascasc);
}

exports.UrlEncode=function(str){
    var ret="";
    var strSpecial="!\"#$%&'()*+,/:;<=>?[]^`{|}~%";
    var tt= "";
    for(var i=0;i<str.length;i++){
		var chr = str.charAt(i);
		var c=str2asc(chr);
		tt += chr+":"+c+"n";
		if(parseInt("0x"+c) > 0x7f){
			ret+="%"+c.slice(0,2)+"%"+c.slice(-2);
		}else{
		if(chr==" ")
			ret+="+";
		else if(strSpecial.indexOf(chr)!=-1)
			ret+="%"+c.toString(16);
		else
			ret+=chr;
		}
    }
    return ret;
}

exports.UrlDecode = function(str){
    var ret="";
    for(var i=0;i<str.length;i++){
		var chr = str.charAt(i);
		if(chr == "+"){
			ret+=" ";
		}else if(chr=="%"){
			var asc = str.substring(i+1,i+3);
		if(parseInt("0x"+asc)>0x7f){
			ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6)));
			i+=5;
		}else{
			ret+=asc2str(parseInt("0x"+asc));
			i+=2;
		}
		}else{
			ret+= chr;
		}
    }
    return ret;
}


var template =  {
	std : {
        '20001':    { cfid:20001, typename:'USD', price:.19,    name:'1元档位'},
        '20002':    { cfid:20002, typename:'USD', price:.45,    name:'3元档位'},
        '20003':    { cfid:20003, typename:'USD', price:.99,    name:'6元档位'},
        '20004':    { cfid:20004, typename:'USD', price:.99,    name:'8元档位'},
        '20005':    { cfid:20005, typename:'USD', price:1.99,   name:'12元档位'},
        '20006':    { cfid:20006, typename:'USD', price:2.99,   name:'18元档位'},
        '20007':    { cfid:20007, typename:'USD', price:4.99,   name:'30元档位'},
        '20008':    { cfid:20008, typename:'USD', price:10.99,  name:'68元档位'},
        '20009':    { cfid:20009, typename:'USD', price:14.99,  name:'98元档位'},
        '20010':    { cfid:20010, typename:'USD', price:19.99,  name:'128元档位'},
        '20011':    { cfid:20011, typename:'USD', price:29.99,  name:'198元档位'},
        '20012':    { cfid:20012, typename:'USD', price:49.99,  name:'328元档位'},
        '20013':    { cfid:20013, typename:'USD', price:74.99,  name:'488元档位'},
        '20014':    { cfid:20014, typename:'USD', price:99.99,  name:'648元档位'},
	},
}
// Android 产品包的配置数据
var Android = {
	editor : template.std,  		// 测试渠道
}

// iOS 产品包的配置数据
var IOS = {
	yudha : template.yudha,
	yuda_abd : template.yuda_abd,
	yuda_abdof : template.yuda_abd,
}

//谷歌的验证密钥
var  GooglePlay_SIG = {
	// 'com.mgames.pixel.en' : {
		// sig : 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5XPAAyaVLvvp5tleB2lpDgsDBOOt3Jly/HycNJGGAZe/80niCLe1Tdu6OVzjHCjm4KDxQvcHGUxjbKZK53nsR+zLG1oahk+uBG99BBhnqm3QuYQYoBZs0zj56qqyCkz0r8Nj/TenwBLliYiA4i/z/LIPL+71Z7NBamxH1GqK1JL+E/gNdwI6r0lnEqFqfGqmB/hVP5jQa6ogxxY27f0lD7+p1QV7sDQfIzSGX+M49Notf10Ptesr0BNr6IkMFdQHZRPjdHJBmGAaBw4tAKk/HcyLH22NDexpSU2YOeyr4aMrQ89ydIHGO7KlgPel5Uag/h+ERHv12PrEE2pYrp0qCQIDAQAB',
	// },
	// 'com.jl3rd.zlwx.google' : {			// yytx_dny
		// sig : 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApHXWDxtD66vELk6/Xdz0G4CiKKZLfjCauJ1wqDuAxbx2K1jX8YZ56XfyS594sGtBapVpmweLWT6/YrKXpoRv0OXUCL1TFdFaD0S7F6Fvocxcwjq6VHUxd3d7X9ye0yyURDNdqo2phGZbo2HnAF8kzA2ohSNer3ZTLUqqj53Tv0lUjIEWYo5eI/N/F24xRHy5q1JTm+cuRczuUqN5Gh+gt8qAPYPRNTHTNg4WRjGq097ONLnvjzisjubUQiDaYOj1wbPyHYPypAltX0PVt1JnTQ1H/JlFgOUAjATipm41r9CQ2tZVuB98xetVwYpQLK6tS3HztnDmbN0XbhEFEUL+KwIDAQAB'
	// },
}

// IOS 中是否是沙盒测试的istest=1沙盒测试  2:正式
var IOS_ISTEST = {
	// zd_ios: {istest : 1,},
}

/*
	channel: 渠道名
	istest: google 使用 (IOS 使用 IOS_ISTEST)

	通过包名,查找对于的渠道标识
*/
var appid_channel = {
	// "com.jl3rd.zlwx.google":{channel:"yytx_dny",istest:1 },		// yytx_dny(公司发行东南亚) //1是测试2是正式
}

// 通过 cifd 反向查找配置
exports.reverse = function(platform, channel, cfid) {
	platform = platform ? platform.toLowerCase() : 'null'
	channel = channel ? channel.toLowerCase() : "null"
	cfid = cfid ? cfid : 'null'

	var hash = null;
	if (platform == "ios") {
		hash = IOS;
	} else if (platform == 'android') {
		hash = Android;
	} else if (platform == 'zayaweb') {
		hash = Web;
	}
	if (hash) {
		var option = hash[channel]
		if (option) {
			for (var key in option) {
				var ret = option[key];
				if (ret.cfid == cfid) {
					return ret
				}
			}
		}
	}
	return { cfid:0, typename:'', price:0, diamond:0 }
}

// 解析(在google/ios设置的档位名称) xxx.xxx.1001 返回最后的数据
exports.parse_end = function(key){
	var a = key.split('.');
	var len = a.length;
	if (len > 0) {
		key = parseInt(a[len-1]);
		if(!key){
			key = a[len-1];
		}
	}
	return key
}

exports.parse = function(platform,channel,key) {
	platform = platform ? platform.toLowerCase() : 'null'
	channel = channel ? channel.toLowerCase() : "null"
	key = key ? key : 'null'

	var hash = null;
	if (platform == 'ios') {
		if (key) {
			var a = key.split('.');
			var len = a.length;
			if (len > 0) {
				key = parseInt(a[len-1]);
				if(!key){
					key = a[len-1];
				}
				hash = IOS;
			}
		}
	} else if (platform == 'android') {
		key = parseInt(key);
		hash = Android;
	} else if(platform == 'googleplay'){
		if (key) {
			var a = key.split('.');
			var len = a.length;
			if (len > 0) {
				key = parseInt(a[len-1]);
				if(!key){
					key = a[len-1];
				}
				hash = Android;
                if(hash.editor && hash.editor[key]){
                    return hash.editor[key]
                }
			}
		}
	} else if(platform == "web"){
		key = parseInt(key);
		hash = Web;
	}
	if (hash) {
		var option = hash[channel];
		if (option && option[key]) {
			return option[key]
		}
	}
	return { cfid:0, typename:'', price:0, diamond:0 }
}

//@TODO: 是否存在该渠道
exports.allowed_ios_channel = function(channel) {
	var option = IOS[channel];
	return option ? true : false
}

//@TODO: 充值模式获取
exports.get_ios_istest = function(channel){
	var option = IOS_ISTEST[channel];
	console.log("option.istest:%s",option.istest)
	if (option && (option.istest || 0 == option.istest)){
		return option.istest
	}
	return 2
}

//@TODO: 是否是安卓的充值
exports.allowed_android_channel = function(channel) {
	var option = Android[channel];
	return option ? true : false
}

//@TODO: google充值秘钥
exports.get_googleplay_sig = function(channel){
	var option = GooglePlay_SIG[channel];
	if (option && option.sig){
		return option.sig
	}
	return 2
}

// 配置 TalkingData 的 AppID
exports.getAppID = function(channel) {
	if (channel=="yudha"){
		return "0FE175A24C984525AD76DBE6A46031A0";			// 屠龙简体
	}
	else if(channel == 'yuda_abd'){
		return "91BF1908F5554C0E8DF31C2F9B180A3E";			// 屠龙港澳台
	}
	else if(channel == 'yuda_abdof'){
		return "0645B126AF3642A8AF8A510BF657BA64";			// 屠龙港澳台3测
	}
	else {
		return null;
	}
}

// google 通过包名获取渠道配置
exports.get_channel = function(open_appid){
	if (appid_channel[open_appid]){
		return appid_channel[open_appid].channel;
	}
}

// 获取 是否测试标识
exports.get_istest = function(open_appid) {
	// console.log("####get_istest %s", open_appid)
	if (appid_channel[open_appid]){
		return appid_channel[open_appid].istest;
	}
	return 3
}

// 卓动 是否验证 md5(小游戏使用)
exports.get_verify = function(open_appid) {
	if (appid_channel[open_appid]){
		return appid_channel[open_appid].verify;
	}
	return false;
}

//@TODO: 将传入的 data数据, 拼接在一起
exports.url_sort = function(data){
	var sort_data = Object.keys(data).sort();
	// 生成本地签名验证签名
	var strsign = data[sort_data[0]];
	for(var i=1;i<sort_data.length;i++){
		if(sort_data[i] != "sign"){
			strsign = strsign + '&'+ data[sort_data[i]]
		}
	}
	return strsign
}