/*
* 登录注册模块;
* @author yhw;
* @require ../css/logRegPop.css
*/

define(function () {
	return function ( opts ) {
		opts = $.extend({
			isBindPhone: true, // 是否需要绑定手机选项;
			gameUrl: "http://www.37.com/",
			gameName: "37游戏"
		}, opts||{});

		var app = {
			init: function () {
				this.deal();
				this.appendTpl();
			},
			deal: function () {
				// 接口兼容测试环境;
				var host = window.location.hostname, apiType = "",
					pathname = window.location.pathname;
				if ( /^m/.test(host) ) {
					apiType = "m";
				}
				this.apiType = apiType;
			},
			insertCss: function ( csstext ) {
				var style = '<style type="text/css">' + csstext + '</style>';
				$( "head" ).eq( 0 ).prepend( style );
			},
			appendTpl: function () {
				var ts = this;
				// 背景样式;
				ts.$lrBox = $( "#logregBox" );
				ts.$lrLoading = $( "#lrLoading" );
				ts.$sentToPhone = ts.$lrBox.find( ".sent-to-phone" );
				ts._evt();
				ts._login();
				ts._rl();

			},
			_rl: function () {
				var ts = this,
					$lrBox = ts.$lrBox;
				// 注册;
				$lrBox.on( "click", "#regBtn", function(e) {
					e.preventDefault();
					var data = {
						"name": $("#regName").val().trim(),
						"pw": $("#regPwd").val().trim(),
						"rpw": $("#regPwd1").val().trim(),
						"type": "reg"
					};
					// 如果绑定手机;
					if ( ts.isBind ) {
						data.phone = $( "#regTel" ).val().trim();
					}
					ts._check( data );
				})
				// 登录;
					.on( "click", "#logBtn", function(e) {
						e.preventDefault();
						var data = {
							"name": $("#logName").val().trim(),
							"pw": $("#logPwd").val().trim(),
							"type": "log"
						};
						ts._check( data );
					});

			},
			_check: function ( o ) {
				var ts = this;
				if ( o.type === "reg" ) {
					var $regNameTip = $( "#regNameTip" ),
						$regPwTip = $( "#regPwTip" ),
						$regPwTip1 = $( "#regPwTip1" ),
						$telCodeTip = $( "#telCodeTip" );

					if ( !o.name || SQ.TgLogin.checkUsername(o.name, true) !== true ) {
						return $regNameTip.addClass( "x1" );
					} else {
						$regNameTip.removeClass( "x1" );
					}

					if ( !o.pw || SQ.TgLogin.checkPassword(o.pw) !== true ) {
						return $regPwTip.addClass( "x1" );
					} else {
						$regPwTip.removeClass( "x1" );
					}

					if ( o.pw !== o.rpw ) {
						return $regPwTip1.addClass("x1");
					} else {
						$regPwTip1.removeClass("x1");
					}

					if ( ts.isBind ) {
						// 检测手机号;
						if ( o.phone == "" || !SQ.Validate.methods.phone("_",o.phone) ) {
							return $telCodeTip.addClass( "x1" ).html( "手机号码不合法" );
						} else {
							ts.phoneCode = o.phone; // 手机号;
							$telCodeTip.removeClass( "x1" ).html( "绑定手机，礼包将升级" );
						}
					}
					ts.login.toReg( o.name, o.pw, o.rpw );
				} else {
					var $logNameTip = $( "#logNameTip" ),
						$logPwTip = $( "#logPwTip");

					if ( !o.name || SQ.TgLogin.checkUsername(o.name, true) !== true ) {
						return $logNameTip.addClass( "x1" );
					} else {
						$logNameTip.removeClass( "x1" );
					}

					if ( !o.pw || SQ.TgLogin.checkPassword(o.pw) !== true ) {
						return $logPwTip.addClass( "x1" );
					} else {
						$logPwTip.removeClass( "x1" );
					}
					// 去登陆;
					ts.login.toLog( o.name, o.pw );
				}
			},
			_login: function () {
				var ts = this;
				this.login = new SQ.TgLogin({
					dialog: false,
					game_id: opts.game_id,
					lr: function( u ) {
						typeof opts.lr === "function" && opts.lr(u);
					},
					reged: function( res ){
						if ( ts.isBind ) {
							ts.getCheckCode();
						} else {
							ts.getCardNum();
						}
					},
					unreg: function( type, res ) {
						type == "reg" && res.message && alert( res.message );
					},
					loged: function () {
						ts.getCardNum();
					},
					unlog: function( res, type ) {
						type == "log" && res.message && alert( res.message );
					},
					out: function() {
						location.reload();
					}
				});
			},
			_evt: function () {
				var ts = this,
					$lrpannels = ts.$lrBox.find( ".lr-wrap" );
				// 注册 登录 切换;
				ts.$lrBox.on( "click", ".js-lg-btn", function(e) {
					e.preventDefault();
					var index = +$( this ).data( "index" );
					ts.posModule( (1-index) );
				})
				// 返回首页;
					.on( "click", ".js-home", function(e) {
						e.preventDefault();
						ts.$lrBox.removeClass( "show-logregBox" );
					}).on( "click", ".bind-radio", function() {
					var val = +$( ".bind-radio:checked" ).val();
					if ( val ) {
						// 选了绑定手机;
						ts.$lrBox.addClass( "show-bind-box" );
						ts.isBind = true;
					} else {
						ts.$lrBox.removeClass( "show-bind-box" );
						ts.isBind = false;
					}
				}).
				// 获取手机验证码;
				on( "click", "#getTelCode", function(e) {
					e.preventDefault();
					var tel = $( "#codeTel" ).val().trim(),
						$tip = $( "#codeTelTip" );
					// 检测手机号;
					if ( tel == "" || !SQ.Validate.methods.phone("_",tel) ) {
						return $tip.addClass( "x1" );
					} else {
						$tip.removeClass( "x1" );
					}
					// 手机号;
					ts.phoneCode = tel;
					ts.getCheckCode();
				})
				// 提交手机验证码;
					.on( "click", "#sentCheckCode", function(e) {
						e.preventDefault();
						var code = $( "#codeCheck" ).val().trim();
						if ( !/^\d{6}$/.test(code) ) {
							return $( "#codeCheckTip" ).addClass( "x1" ).html( "验证码由6位数字组成" );
						} else {
							$( "#codeCheckTip" ).removeClass( "x1" ).html( "验证码已经发送到手机，请注意查收" );
						}
						ts.getPhoneCardNum( code );
					});

				// 发送到手机按钮事件;
				ts.$sentToPhone.on( "click", function(e) {
					e.preventDefault();
					// 弹手机号输入框;
					ts.posModule( 3 );
				});
			},
			show: function () {
				this.$lrBox.addClass( "show-logregBox" );
			},
			hide: function () {
				this.$lrBox.find( ".js-home" ).trigger( "click" );
			},
			// 获取手机验证码;
			getCheckCode: function () {
				var ts = this;
				ts.$lrLoading.show();
				$.ajax({
					url: opts.getCodeUrl || "http://"+ ts.apiType +"hdapi.37.com/index.php?c=auto_get_gift_sms&a=get_code",
					dataType: "jsonp",
					data: {
						alias_info: opts.alias, // 专题唯一标识;
						phone: ts.phoneCode
					}
				}).always(function () {
					ts.$lrLoading.hide();
				}).done(function( data ) {
					if( +data.result === 1 ){
						//alert( "验证码已经发送到手机，请注意查收！" );
						ts.posModule( 4 );
					}else{
						data.msg && alert( data.msg );
					}
				}).fail(function() {
					alert( "网络问题，发送验证码失败，请重试！" );
				});
			},
			// 显示对应模块;
			posModule:function ( id ) {
				var $lrBox = this.$lrBox;
				$lrBox.find( ".lr-wrap" ).css({
					"transform": "translate3d(0,-300%,0)",
					"-webkit-transform": "translate3d(0,-300%,0)"
				});
				$lrBox.find( '.lr-wrap[data-index="'+ id +'"]' ).css({
					"transform": "translate3d(0,0,0)",
					"-webkit-transform": "translate3d(0,0,0)"
				});
			},
			// 获取礼包;
			getCardNum: function(){
				var ts = this,
					url = opts.giftUrl + "&callback=?";
				ts.$lrLoading.show();
				$.ajax({
					url: opts.giftUrl,
					dataType: "jsonp"
				}).done(function(data) {
					if ( !data ) return alert( "服务器错误" );
					var code = +data.result;
					if ( code === 1 || code === -32 ) {
						ts.getGiftInfor();
					} else {
						data.msg && alert( data.msg );
					}
				}).always(function () {
					ts.$lrLoading.hide();
				});

			},
			// 获取礼包信息;
			getGiftInfor: function () {
				var ts = this;
				ts.$lrLoading.show();
				$.ajax({
					url: "http://"+ ts.apiType +"hdapi.37.com/index.php?c=auto_get_gift_sms&a=gift_info",
					dataType: "jsonp",
					data: {alias_info: opts.alias}
				}).always(function () {
					ts.$lrLoading.hide();
				}).done(function(data) {
					if ( +data.result === 1 ) {
						ts.isBind = !!data.val; // 是否绑定了手机;
						var item = ( data.list || [] )[0];
						$( "#codeVal" ).text( item.CARD_NUM );
						$( "#giftName" ).text( item.GIFT_NAME );
						if ( ts.isBind ) {
							ts.$sentToPhone.hide();
						} else {
							ts.$sentToPhone.show();
						}
						ts.posModule( 2 );
					}
				});
			},
			// 获取手机礼包;
			getPhoneCardNum: function ( code ) {
				var ts = this;
				ts.$lrLoading.show();
				$.ajax({
					url: opts.phoneGiftUrl || "http://"+ ts.apiType +"hdapi.37.com/index.php?c=auto_get_gift_sms&a=get_gift",
					dataType: "jsonp",
					data: {
						alias_info: opts.alias,
						card_type: opts.cardType || "GET_MORE_GIFT_ARR",
						class_key: opts.classKey || "get_more_gift_0",
						type: opts.type || 2,
						code: code,
						app_id: opts.app_id || 32,
						phone: ts.phoneCode,
						game_name: opts.giftName,
						game_url: opts.gameUrl
					}
				}).always(function () {
					ts.$lrLoading.hide();
				}).done(function (data) {
					var code = +data.result;
					if( code === 1 || code === -84 ) {
						ts.getCardNum();
					} else if( code === -9 ) {
						alert( "该验证码已经验证成功！" );
					} else if( code === -10 ) {
						alert( "输入的验证码有误！" );
					} else if( code === -85 ) {
						alert( "很抱歉，礼包已经被领完！" );
					} else {
						data.msg && alert( data.msg );
					}
				});
			}
		};
		app.init();
		// 暴露的api;
		return {
			// 弹出登录注册框;
			show: function () {
				app.show();
			},
			// 收起弹窗;
			hide: function () {
				app.hide();
			},
			// 注销;
			toOut: function () {
				app.login.toOut();
			},
			// 登录状态;
			hasLogin: function () {
				return app.login.hasLogin();
			}
		};

	};
});