package jp.yoyoyoyo.webformmanager.action;

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

import jp.yoyoyoyo.webformmanager.form.StatusChangeForm;
import jp.yoyoyoyo.webformmanager.model.ChangeStatusData;
import jp.yoyoyoyo.webformmanager.model.LoginInfo;

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 StatusChangeAction extends Action {
	public ActionForward execute(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response){
		
		StatusChangeForm statusChangeForm = (StatusChangeForm)form;
		HttpSession session = request.getSession();
		
		// ログイン情報の取得
		LoginInfo info = (LoginInfo)session.getAttribute("loginData");
		
		// ログインしていないときはエラー
		if(info == null){
			return mapping.findForward("maintenance");
		}
		
		// Writeでないときはエラー
		if(!info.isRoleWrite()){
			return mapping.findForward("maintenance");
		}
		
		if(statusChangeForm.getStatusChangeBack() != null && !statusChangeForm.getStatusChangeBack().equals("")){	// 戻るボタンを押したとき
			return mapping.findForward("detail");
		} else if(statusChangeForm.getStatusChangeNext() != null && !statusChangeForm.getStatusChangeNext().equals("")){	// 次へボタンを押したとき
			// リクエストデータをフォームにコピー
			request.setAttribute("id", request.getParameter("id"));
			request.setAttribute("status", request.getParameter("status"));
			request.setAttribute("comment", request.getParameter("comment"));
			
			// バリデーションの実行
			ActionErrors errors = statusChangeForm.checkValidate(mapping, request);
			
			// バリデーションエラーが発生したとき
			if(errors.size() > 0){
				// エラー内容をセット
				saveErrors(request, errors);
				// IDを再設定
				//statusChangeForm.setId((String)session.getAttribute("detailId"));
				request.setAttribute("id", (String)session.getAttribute("detailId"));
				// 入力画面に戻る
				return mapping.findForward("statusChange");
			}
	
			// ステータス更新オブジェクトに値をセット
			ChangeStatusData data = new ChangeStatusData();
			
			data.setId((String)session.getAttribute("detailId"));
			data.setOldStatus((int)session.getAttribute("detailStatus"));
			data.setNewStatus(statusChangeForm.getStatus());
			data.setComment(statusChangeForm.getComment());
			
			data.setOldStatusText(data.constStatus.get(data.getOldStatus()));
			data.setNewStatusText(data.constStatus.get(data.getNewStatus()));
			
			session.setAttribute("statusChangeData", data);
			
			return mapping.findForward("statusChangeConfirm");
			
		}  else {	

			//statusChangeForm.setId((String)session.getAttribute("detailId"));
			//statusChangeForm.setStatus((int)session.getAttribute("detailStatus"));
			request.setAttribute("id", (String)session.getAttribute("detailId"));
			request.setAttribute("status", (int)session.getAttribute("detailStatus"));
			
			return mapping.findForward("statusChange");
		}
	}
}
