package jp.yoyoyoyo.webformmanager.action;

import java.security.MessageDigest;
import java.sql.SQLException;

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

import jp.yoyoyoyo.webformmanager.dao.UpdatePasswordDAO;
import jp.yoyoyoyo.webformmanager.form.EditPasswordConfirmForm;
import jp.yoyoyoyo.webformmanager.form.EditPasswordForm;
import jp.yoyoyoyo.webformmanager.model.LoginInfo;
import jp.yoyoyoyo.webformmanager.util.CommonUtil;

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

public class EditPasswordConfirmAction extends Action {
	public ActionForward execute(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session = request.getSession();
		EditPasswordConfirmForm editPasswordConfirmForm = (EditPasswordConfirmForm)form;
		// ログイン情報の取得
		LoginInfo info = (LoginInfo)session.getAttribute("loginData");
		// ログインしていないときはエラー
		if(info == null){
			return mapping.findForward("maintenance");
		}
		// UserManagerでないときはエラー
		if(!info.isRoleUserManager()){
			return mapping.findForward("maintenance");
		}
		if(editPasswordConfirmForm.getEditPasswordConfirmBack() != null && !editPasswordConfirmForm.getEditPasswordConfirmBack().equals("")){	// 戻るボタンを押したとき
			EditPasswordForm  editPasswordForm = (EditPasswordForm)session.getAttribute("editPasswordData");
			
			request.setAttribute("id", editPasswordForm.getId());
			request.setAttribute("password1", editPasswordForm.getPassword1());
			request.setAttribute("password2", editPasswordForm.getPassword2());
			return mapping.findForward("editPassword");	
		} else if(editPasswordConfirmForm.getEditPasswordConfirmSave() != null && !editPasswordConfirmForm.getEditPasswordConfirmSave().equals("")){	// 保存ボタンを押したとき
			EditPasswordForm  editPasswordForm = (EditPasswordForm)session.getAttribute("editPasswordData");
			
			// パスワードをSHA-256で暗号化
			byte[] binary = editPasswordForm.getPassword1().getBytes();
			MessageDigest md;
			try{
				md = MessageDigest.getInstance("SHA-256");
				byte[] digest = md.digest(binary);
					
				editPasswordForm.setEncryptedPassword(CommonUtil.bytesToHexString(digest));
			}catch(Exception e){
				// エラーが発生したとき
				return mapping.findForward("maintenance");
			}
			
			// パスワードを更新する
			UpdatePasswordDAO dao = new UpdatePasswordDAO();
			
			try {
				dao.executeSP(editPasswordForm, 1);
			} catch (SQLException e) {
				
				return mapping.findForward("maintenance");
			}
			
			session.removeAttribute("editPasswordData");
			
			return mapping.findForward("messageBox");	
		} else {
			// JSPに値を渡す
			EditPasswordForm  editPasswordForm = (EditPasswordForm)session.getAttribute("editPasswordData");
			
			request.setAttribute("data", editPasswordForm);
			
			return mapping.findForward("editPasswordConfirm");	
		}
	}
}
