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.InsertMstUserDAO;
import jp.yoyoyoyo.webformmanager.form.NewUserConfirmForm;
import jp.yoyoyoyo.webformmanager.form.NewUserForm;
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 NewUserConfirmAction extends Action {
	public ActionForward execute(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session = request.getSession();
		NewUserConfirmForm 	newUserConfirmForm = (NewUserConfirmForm)form;
		
		// ログイン情報の取得
		LoginInfo info = (LoginInfo)session.getAttribute("loginData");
		// ログインしていないときはエラー
		if(info == null){
			return mapping.findForward("maintenance");
		}
		// UserManagerでないときはエラー
		if(!info.isRoleUserManager()){
			return mapping.findForward("maintenance");
		}
		if(newUserConfirmForm.getNewUserConfirmBack() != null && !newUserConfirmForm.getNewUserConfirmBack().equals("")){ // 戻るボタンを押したとき
			NewUserForm  newUserForm = (NewUserForm)session.getAttribute("newUserData");
				
			// 値の復元
			request.setAttribute("id", newUserForm.getId());
			request.setAttribute("operatorName", newUserForm.getOperatorName());
			
			request.setAttribute("password1", newUserForm.getPassword1());
			request.setAttribute("password2", newUserForm.getPassword2());
			
			
			return mapping.findForward("newUser");
		} else if(newUserConfirmForm.getNewUserConfirmSave() != null && !newUserConfirmForm.getNewUserConfirmSave().equals("")){	// 保存ボタンを押したとき
			NewUserForm  newUserForm = (NewUserForm)session.getAttribute("newUserData");
			
			newUserForm.setRoleValueText(newUserForm.getRoleValueTextString());
			
			// パスワードをSHA-256で暗号化
			byte[] binary = newUserForm.getPassword1().getBytes();
			MessageDigest md;
			try{
				md = MessageDigest.getInstance("SHA-256");
				byte[] digest = md.digest(binary);
				
				newUserForm.setEncryptedPassword(CommonUtil.bytesToHexString(digest));
			}catch(Exception e){
				// エラーが発生したとき
				return mapping.findForward("maintenance");
			}
			
			// ユーザマスタに情報を記録する
			InsertMstUserDAO dao = new InsertMstUserDAO();
			
			try {
				dao.executeSP(newUserForm);
			} catch (SQLException e) {
				
				return mapping.findForward("maintenance");
			}
			
			session.removeAttribute("newUserData");
			
			return mapping.findForward("messageBox");
		} else {
			// 表示データをJSPに渡す
			NewUserForm  newUserForm = (NewUserForm)session.getAttribute("newUserData");
			
			request.setAttribute("data", newUserForm);
			
			return mapping.findForward("newUserConfirm");
			
		}
		
		
	}
}
