package com.guru.project.clinique.game.formStep 
{
	import com.guru.call.FunctionCall;
	import com.guru.call.ICall;
	import com.guru.component.message.Message;
	import com.guru.component.panel.Panel;
	import com.guru.component.panel.StatusBar;
	import com.guru.component.sectionStructure.SectionStructureStepBase;
	import com.guru.component.tracking.Tracking;
	import com.guru.conn.ConnAbstract;
	import com.guru.conn.ConnPath;
	import com.guru.display.button.LabelButton;
	import com.guru.display.button.LabelCheckButton;
	import com.guru.encrypt.GuruEncrypt;
	import com.guru.facebook.FacebookDataBridge;
	import com.guru.form.FormGroupElement;
	import com.guru.form.FormTextElement;
	import com.guru.form.GuruComboBox;
	import flash.display.MovieClip;
	/**
	 * ...
	 * @author Eddy Lee
	 */
	public class GameSectionFormStep extends SectionStructureStepBase
	{
		private var mNameElement:FormTextElement;
		private var mMobileElement:FormTextElement;
		private var mHkidElement:FormTextElement;
		private var mEmailElement:FormTextElement;
		private var mAgeComboBox:GuruComboBox;
		
		private var mMyForm:FormGroupElement;
		private var mTncBtn:LabelButton;
		private var mAcceptTncBtn:LabelCheckButton;
		private var mAcceptEdmBtn:LabelCheckButton;
		private var mSubmitBtn:LabelButton;
		private var mResetBtn:LabelButton;
		
		private var mAgeError_mc:MovieClip;
		private var mTncError_mc:MovieClip;
		
		private var mSubmitFormConn:ConnAbstract;
		
		public function GameSectionFormStep(pTimeline_mc:MovieClip, pNextCall:ICall = null, pPrevCall:ICall = null, pData_obj:Object = null)
		{
			super(pTimeline_mc, pNextCall, pPrevCall, pData_obj);
			
			Tracking.getInstance().mediamind("109662");
			Tracking.getInstance().track("game_form");
			
			mAgeError_mc = mTimeline_mc.ageError_mc;
			mTncError_mc = mTimeline_mc.tncError_mc;
			
			mSubmitFormConn = new ConnAbstract(ConnPath.getSubmitFormPath(), new FunctionCall(this, onSubmitSuccess), new FunctionCall(this, onSubmitFail));
			
			mTncBtn = new LabelButton(mTimeline_mc.tncBtn_mc);
			mTncBtn.setPressCall(new FunctionCall(this, onTncBtnPress));
			
			mAcceptTncBtn = new LabelCheckButton(mTimeline_mc.acceptTncBtn_mc);
			mAcceptEdmBtn = new LabelCheckButton(mTimeline_mc.acceptEdmBtn_mc);
			
			mNameElement = new FormTextElement(mTimeline_mc.nameElement_mc, 1, 50, null, 1);
			mMobileElement = new FormTextElement(mTimeline_mc.mobileElement_mc, 8, 8, "0-9", 2);
			mEmailElement = new FormTextElement(mTimeline_mc.emailElement_mc, 1, 100, null, 3);
			mAgeComboBox = new GuruComboBox(mTimeline_mc.ageComboBox, 4);
			mHkidElement = new FormTextElement(mTimeline_mc.hkidElement_mc, 4, 4, "0-9", 5);
			
			mAgeComboBox.addItem(["請選擇", "15 – 17", "18 – 35", "36 – 45", "46 或以上"], ["請選擇", "15 – 17", "18 – 35", "36 – 45", "46 或以上"]);
			
			mMyForm = new FormGroupElement();
			mMyForm.addElement(mNameElement);
			mMyForm.addElement(mMobileElement);
			mMyForm.addElement(mHkidElement);
			mMyForm.addElement(mEmailElement, true);
			
			mSubmitBtn = new LabelButton(mTimeline_mc.submitBtn_mc);
			mResetBtn = new LabelButton(mTimeline_mc.resetBtn_mc);
			mSubmitBtn.setPressCall(new FunctionCall(this, onSubmitBtnPress));
			mResetBtn.setPressCall(new FunctionCall(this, onResetBtnPress));
			
			mAcceptEdmBtn.check();
		}
		
		private function onSubmitBtnPress():void
		{
			if (this.checkForm())
			{
				this.submitData();
			}
			else
			{
				mSubmitBtn.reset();
			}
		}
		
		private function checkForm():Boolean
		{
			var valid_bol:Boolean = true;
			
			if (!mMyForm.checkFormValidity())
			{
				valid_bol = false;
			}
			else
			{
				
			}
				
			if (!mAcceptTncBtn.isChecked())
			{
				valid_bol = false;
				mTncError_mc.gotoAndStop(2);
			}
			else
			{
				mTncError_mc.gotoAndStop(1);
			}
			
			if (mAgeComboBox.getSelectedIndex() == 0)
			{
				valid_bol = false;
				mAgeError_mc.gotoAndStop(2);
			}
			else
			{
				mAgeError_mc.gotoAndStop(1);
			}
				
			return valid_bol;
		}
		
		private function onResetBtnPress():void
		{
			this.resetForm();
			
			mResetBtn.reset();
		}
		
		private function resetForm():void
		{
			mMyForm.reset();
			mAcceptTncBtn.reset();
			mAcceptEdmBtn.reset();
			mAcceptEdmBtn.check();
			mTncError_mc.gotoAndStop(1);
			mAgeError_mc.gotoAndStop(1);
			mAgeComboBox.switchToLabel("請選擇");
			mSubmitBtn.reset();
		}
		
		private function onTncBtnPress():void
		{
			Panel.showPanel("tnc", null, new FunctionCall(mTncBtn, mTncBtn.reset));
		}
		
		private function onSubmitSuccess():void
		{
			if (mSubmitFormConn.getXML()..status[0] == "true")
			{
				Panel.hideStatusBar(mNextStepCall);
			}
			else
			{
				this.onSubmitFail();
			}
		}
		
		private function onSubmitFail():void
		{
			Panel.showStatusBar(StatusBar.STATUS, Message.SUBMIT_ERROR, null, new FunctionCall(this, submitData));
		}
		
		private function submitData():void
		{
			Panel.showStatusBar(StatusBar.LOADING, Message.SUBMIT_LOADING, new FunctionCall(this, doSubmitData));
		}
		
		private function doSubmitData():void
		{
			var acceptEdm_str:String = "0";
			if (mAcceptEdmBtn.isChecked())
				acceptEdm_str = "1";
				
			var param_str:String = FacebookDataBridge.getLoggerUid() + "|" + mNameElement.getData() + "|" + mMobileElement.getData() + "|" + mHkidElement.getData() + "|" + mEmailElement.getData() + "|" + mAgeComboBox.getSelectedLabel() + "|" + acceptEdm_str + "|" + mData_obj["gameHistory"];
			Logger.eddy(mSubmitFormConn.getPath() + ": " + param_str);
			mSubmitFormConn.send({data:GuruEncrypt.encrypt(param_str)});
		}
		
	}

}