package jp.agentec.sinaburocast.action.user;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.mail.MessagingException;

import jp.agentec.sinaburocast.action.AbstractAction;
import jp.agentec.sinaburocast.common.exception.ExpectedException;
import jp.agentec.sinaburocast.common.exception.SystemException;
import jp.agentec.sinaburocast.common.util.SinaburoViewUtil;
import jp.agentec.sinaburocast.form.user.WithDrawForm;
import jp.agentec.sinaburocast.service.MemberWithdrawService;

import org.apache.log4j.Logger;
import org.apache.struts.util.TokenProcessor;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;

/**
 * 退会手続き画面
 *
 * @author choi-c
 *
 */
public class WithDrawAction extends AbstractAction {
	private final Logger logger = Logger.getLogger(getClass());

	@ActionForm
	public WithDrawForm withDrawForm;


	public MemberWithdrawService memberWithdrawService;


	/**
	 * 初期表示
	 */
	@Execute(validator = false)
	public String index() {
		return "/user/withDraw/withDraw.html";
	}

	/**
	 * 会員退会押下
	 *
	 * @return
	 * @throws IOException
	 * @throws SystemException
	 * @throws ExpectedException
	 */
	@Execute(validator = false)
	public String withDrawConfirm()  {
		TokenProcessor.getInstance().saveToken(request);

		session.setAttribute("withDrawFormInfo", withDrawForm);
		//退会手続き確認画面
		return "/user/withDraw/withDrawConfirm.html";
	}

	/**
	 * 会員退会確認画面にて会員退会押下
	 *
	 * @return
	 * @throws MessagingException
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 * @throws SystemException
	 * @throws ExpectedException
	 */
	@Execute(validator = false)
	public String withDrawRegist() throws UnsupportedEncodingException, MessagingException  {
		withDrawForm = (WithDrawForm)session.getAttribute("withDrawFormInfo");
		if (!TokenProcessor.getInstance().isTokenValid(request, true)) {
			addError(SinaburoViewUtil.getMessage("errors.S999"));
			return "/error/doubleAction.html";
		}

		memberWithdrawService.insertMemberWithdrawLogic(getMember(),withDrawForm);

		session.invalidate();
		return "/user/withDraw/withDrawEnd.html";
	}

	/**
	 * 前の画面に遷移
	 *
	 * @return
	 */
	@Execute(validator = false)
	public String back() {
		withDrawForm = (WithDrawForm)session.getAttribute("withDrawFormInfo");
		return "/user/withDraw/withDraw.html";
	}

}