package com.itjob.action;

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

import org.apache.struts.action.*;

import com.itjob.dao.model.User;
import com.itjob.service.UserManager;

/**
 * 用户基本设置的Action
 * @author AmigoXie
 */
public class UserAction extends Action {
	protected UserManager userManager;

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String method = request.getParameter("method");
		if ("userLogin".equals(method)) {
			return userLogin(mapping, form, request, response);
		} else if ("logoutUser".equals(method)) {
			return logoutUser(mapping, form, request, response);
		} else if ("setRingType".equals(method)) {
			return setRingType(mapping, form, request, response);
		} else if ("updateUserPasswd".equals(method)) {
			return updateUserPasswd(mapping, form, request, response);
		} else if ("updateUserPasswd".equals(method)) {
			return updateUserPasswd(mapping, form, request, response);
		} else {
			request.setAttribute("errorMsg", "错误请求");
			return mapping.findForward("error");
		}
	}
	
	/**
	 * 用户登录
	 */
	public ActionForward userLogin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String userNumber = request.getParameter("userNumber");
		String userPasswd = request.getParameter("userPasswd");
		int resultCode = userManager.userLogin(userNumber, userPasswd);
		if (resultCode == 0) {
			User user = userManager.getUser(userNumber);
			request.getSession().setAttribute("userNumber", userNumber);
			request.getSession().setAttribute("user", user);
			return mapping.findForward("main");
		} else {
			if (resultCode == 1) {
				request.setAttribute("errorMsg", "对不起，登录失败，用户不存在！");
			} else if (resultCode == 2){
				request.setAttribute("errorMsg", "对不起，登录失败，密码错误！");
			} else {
				request.setAttribute("errorMsg", "对不起，登录失败，您的号码已注销！");
			}
			return mapping.findForward("error");
		}
	}
	
	/**
	 * 注销用户
	 */
	public ActionForward logoutUser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String userNumber = (String) request.getSession().getAttribute("userNumber");
		userManager.logoutUser(userNumber);
		return mapping.findForward("index");
	}
	
	/**
	 * 设置振铃方式
	 */
	public ActionForward setRingType(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String userNumber = (String) request.getSession().getAttribute("userNumber");
		int ringType = Integer.parseInt(request.getParameter("ringType"));
		userManager.setRingType(userNumber, ringType);
		
		// 改变Session中的user对象的值
		User user = (User) request.getSession().getAttribute("user");
		user.setRingType(ringType);
		request.getSession().setAttribute("user", user);
		request.setAttribute("returnPage", "/userManage/setRingType.jsp");
		return mapping.findForward("success");
	}
	
	/**
	 * 修改密码
	 */
	public ActionForward updateUserPasswd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String userNumber = (String) request.getSession().getAttribute("userNumber");
		String userPasswd = request.getParameter("userPasswd");
		String confirmPasswd = request.getParameter("confirmPasswd");
		if (userPasswd.equals(confirmPasswd)) {
			userManager.updateUserPasswd(userNumber, userPasswd);
			request.setAttribute("returnPage", "/userManage/updatePasswd.jsp");
			return mapping.findForward("success");
		} else {
			request.setAttribute("errorMsg", "两次密码输入不一致！");
			return mapping.findForward("error");
		}
	}
}
