/**
 *
 */
package jp.agentec.sinaburocast.action.admin.setting;

import java.util.List;

import javax.annotation.Resource;

import jp.agentec.sinaburocast.action.AbstractAction;
import jp.agentec.sinaburocast.common.SinaburoConstant;
import jp.agentec.sinaburocast.common.annotation.OperationHistoryAno;
import jp.agentec.sinaburocast.common.util.SinaburoUtil;
import jp.agentec.sinaburocast.common.util.SinaburoViewUtil;
import jp.agentec.sinaburocast.entity.PointPeriod;
import jp.agentec.sinaburocast.form.admin.setting.PointPeriodRegistForm;
import jp.agentec.sinaburocast.service.PointGetService;
import jp.agentec.sinaburocast.service.PointPeriodService;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.TokenProcessor;
import org.seasar.framework.util.StringUtil;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;

/**
 * eモニポイント有効期限設定画面
 * @author arakawa
 *
 */
public class PointPeriodRegistAction extends AbstractAction {
	private final Logger logger = Logger.getLogger(getClass());

	@ActionForm
	@Resource
	public PointPeriodRegistForm pointPeriodRegistForm;

	public PointPeriodService pointPeriodService;

	public PointGetService pointGetService;

	/**
	 * 初期表示
	 */
	@Execute(validator = false)
	public String index() {

		TokenProcessor.getInstance().saveToken(request);

		List<PointPeriod> pointPeriodList = pointPeriodService.findAll();

		if (!CollectionUtils.isEmpty(pointPeriodList)){
			pointPeriodRegistForm.pointPeriodPre = String.valueOf(pointPeriodList.get(0).pointPeriod);
			pointPeriodRegistForm.pointPeriod = String.valueOf(pointPeriodList.get(0).pointPeriod);
		}

		return "/admin/setting/pointPeriodRegist.html";
	}

	/**
	 * eモニポイント有効期限登録
	 */
	@Execute(validator = true,validate="pointPeriodRegistValidate",input="/admin/setting/pointPeriodRegist.html")
	@OperationHistoryAno(operationTypeId=SinaburoConstant.OperationType.UPDATE,target="eモニポイント有効期限",detail="登録",screenId=SinaburoConstant.screenId.COS0004)
	public String pointPeriodRegist() {

		// トークン確認
		if (!TokenProcessor.getInstance().isTokenValid(request, true)) {

			return "/error/doubleAction.html";
		}

		List<PointPeriod> pointPeriodList = pointPeriodService.findAll();
		if (CollectionUtils.isEmpty(pointPeriodList)){

			PointPeriod pointPeriod = new PointPeriod();
			pointPeriod.pointPeriod = Integer.parseInt(pointPeriodRegistForm.pointPeriod);
			pointPeriodService.insertPointPeriod(pointPeriod,getAdmin());
		}else{

			PointPeriod pointPeriod = pointPeriodList.get(0);
			Integer pointPeriodold = pointPeriod.pointPeriod;

			pointPeriod.pointPeriod = Integer.parseInt(pointPeriodRegistForm.pointPeriod);
			pointPeriod.updateDate = SinaburoUtil.getTimestamp();
			pointPeriod.upId = getAdmin().loginId;

			pointPeriodService.update(pointPeriod);

			if (pointPeriodold > Integer.parseInt(pointPeriodRegistForm.pointPeriod)){

				pointGetService.updatePointGetDateByPointPeriod(pointPeriodRegistForm.pointPeriod,getAdmin().loginId);
			}
		}

		return "/admin/setting/pointPeriodRegistEnd.html";
	}

	/**
	 * eモニポイント有効期限チェック
	 * @return エラーメッセージを変換する。
	 */
	public ActionMessages pointPeriodRegistValidate(){

		if(!StringUtil.isNumber(pointPeriodRegistForm.pointPeriod) ||
				(Integer.parseInt(pointPeriodRegistForm.pointPeriod) < 1 || Integer.parseInt(pointPeriodRegistForm.pointPeriod) > 10)){
			ActionMessages messages = (ActionMessages)request.getAttribute(Globals.ERROR_KEY);
			if (messages == null) {
				messages = new ActionMessages();
			}
			messages.add("pointPeriod", new ActionMessage(SinaburoViewUtil.getMessage("errors.E031")));
			request.setAttribute(Globals.ERROR_KEY, messages);

			//addError(SinaburoViewUtil.getMessage("errors.E031"));
		}

		return (ActionMessages)request.getAttribute(Globals.ERROR_KEY);
	}

}