package jp.yoyoyoyo.webform.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jp.yoyoyoyo.webform.form.EntryForm;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class EntryAction extends Action {

	public ActionForward execute(ActionMapping mapping,
								ActionForm form,
								HttpServletRequest request,
								HttpServletResponse response){
	
		EntryForm entryForm = (EntryForm)form;
		
		// 次へボタンを押したとき
		if(entryForm.getNext() != null && !entryForm.getNext().equals("")){
			// リクエストデータをフォームにコピー
			request.setAttribute("seiKanji", request.getParameter("seiKanji"));
			request.setAttribute("meiKanji", request.getParameter("meiKanji"));
			request.setAttribute("seiKana", request.getParameter("seiKana"));
			request.setAttribute("meiKana", request.getParameter("meiKana"));
			request.setAttribute("birthDate", request.getParameter("birthDate"));
			//request.setAttribute("gender", request.getParameter("gender"));
			request.setAttribute("zip1", request.getParameter("zip1"));
			request.setAttribute("zip2", request.getParameter("zip2"));
			request.setAttribute("address1", request.getParameter("address1"));
			request.setAttribute("address2", request.getParameter("address2"));
			request.setAttribute("address3", request.getParameter("address3"));
			request.setAttribute("address4", request.getParameter("address4"));
			request.setAttribute("tel1", request.getParameter("tel1"));
			request.setAttribute("tel2", request.getParameter("tel2"));
			request.setAttribute("tel3", request.getParameter("tel3"));
			request.setAttribute("mTel1", request.getParameter("mTel1"));
			request.setAttribute("mTel2", request.getParameter("mTel2"));
			request.setAttribute("mTel3", request.getParameter("mTel3"));
			request.setAttribute("email1", request.getParameter("email1"));
			request.setAttribute("email2", request.getParameter("email2"));
			request.setAttribute("occupation", request.getParameter("occupation"));
			
			// バリデーションの実行
			ActionErrors errors = entryForm.checkValidate(mapping, request);
			
			// バリデーションエラーが発生したとき
			if(errors.size() > 0){
				// エラー内容をセット
				saveErrors(request, errors);
				// 入力画面に戻る
				return mapping.findForward("entry");
			}
			
			// セッションの取得
			HttpSession session = request.getSession();
			
			// 電話番号をセット
			if(!entryForm.getTel1().equals("")){
				entryForm.setTel(entryForm.getTel1() + "-" + entryForm.getTel2() + "-" + entryForm.getTel3());
			}
	
			// 携帯電話番号をセット
			if(!entryForm.getmTel1().equals("")){
				entryForm.setmTel(entryForm.getmTel1() + "-" + entryForm.getmTel2() + "-" + entryForm.getmTel3());
			}
			
			// 性別のテキストをセット
			entryForm.setGenderText(entryForm.constGender.get(entryForm.getGender()));
			// 職業のテキストをセット
			entryForm.setOccupationText(entryForm.constOccupation.get(entryForm.getOccupation()));
			
			// IPアドレスをセット
			entryForm.setIpAddress(request.getRemoteAddr());
			// セッションIDをセット
			entryForm.setSessionId(session.getId());
			
			// セッションにフォーム情報を記録
			session.setAttribute("entryData", entryForm);

			// 確認画面へ遷移
			return mapping.findForward("confirm");
		} else {			
			return mapping.findForward("entry");
		}
		
		
	}
}
