package net.qingruanit.market.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

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

import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import net.qingruanit.market.dao.UserInfoDAO;
import net.qingruanit.market.vo.UserInfo;

public class UserInfoAction extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		String action = request.getParameter("action");
		if("login".equals(action)){
			Login(request, response);
		}
		else if("regist".equals(action)){
			Regist(request, response);
		}
		else if("findpassI".equals(action)){
			findpassI(request, response);
		}
		else if("findpassII".equals(action)){
			findpassII(request, response);
		}
		else if("findName".equals(action)){
			findName(request, response);
		}
		else if("code".equals(action)){
			Code(request, response);
		}
		else if("update".equals(action)){
			update(request, response);
		}
		else if("updatePass".equals(action)){
			updatePass(request, response);
		}
	}
	//ºóÌ¨ÐÞ¸Ä¸öÈËÃÜÂë
	public void updatePass(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String oldpass = request.getParameter("oldpass");
		String newpass = request.getParameter("newpass");
		String surepass = request.getParameter("surepass");
		Integer id = Integer.parseInt(request.getParameter("id"));
		Boolean isExist = UserInfoDAO.getDAO().isExistPass(oldpass, id);
		if(isExist){
			if(newpass.equals(surepass)){
				UserInfoDAO.getDAO().updatePass(oldpass, newpass, id);
				response.sendRedirect("http://localhost:8080/supermarket/user/userlogin.jsp");
			}
			else{
				request.setAttribute("err1", "ÃÜÂë²»Ò»ÖÂ");
				this.getServletContext().getRequestDispatcher("/controls/jsp/updatePass.jsp").forward(request, response);
			}
		}
		else{
			request.setAttribute("err0", "ÃÜÂë´íÎó");
			this.getServletContext().getRequestDispatcher("/controls/jsp/updatePass.jsp").forward(request, response);
		}
		
	}
	//ºóÌ¨ÐÞ¸Ä¸öÈËÐÅÏ¢   ÉÏ´«Í·Ïñ
	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			//1ÊµÀý»¯SmartUpload¶ÔÏó
			SmartUpload su = new SmartUpload();
			//2 ³õÊ¼»¯
			su.initialize(this.getServletConfig(), request, response);
			//3 ÉÏ´«
			su.upload();
			//4 Ô­Ãû±£´æËùÓÐÎÄ¼þ
			su.save("/uploadImage");
			
			Request req = su.getRequest();
			Integer id = Integer.parseInt(request.getParameter("id"));
			String name = req.getParameter("name");
			String nic = req.getParameter("nic");
			String sex = req.getParameter("sex");
			String phone = req.getParameter("phone");
			String email = req.getParameter("email");
			String address = req.getParameter("address");
			String pic = req.getParameter("pic");
			int n = pic.lastIndexOf("\\");
			String p = pic.substring(n+1, pic.length());
			UserInfo userinfo = new UserInfo();
			userinfo.setId(id+"");
			userinfo.setName(name);
			userinfo.setNic(nic);
			userinfo.setSex(sex);
			userinfo.setPhone(phone);
			userinfo.setEmail(email);
			userinfo.setAddress(address);
			userinfo.setPic(p);
			UserInfoDAO.getDAO().updateInfo(userinfo,id);
			request.getSession().setAttribute("userinfo", userinfo);
			this.getServletContext().getRequestDispatcher("/controls/jsp/index.jsp").forward(request, response);
			
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	//²éÕÒÓÃ»§ÃûÊÇ·ñÒÑ±»×¢²á
	public void findName(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String name = request.getParameter("name");
		if("".equals(name)){
			out.print(3);
		}
		else{
			Boolean isExist = UserInfoDAO.getDAO().isExistName(name);
			
			if(isExist)
			{
				out.print(1);
			}
			else
			{
				out.print(0);
			}
		}		
		out.close();
	}
	//µÇÂ½
	public void Login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpSession session = request.getSession();
		String username = request.getParameter("username");
		String pass = request.getParameter("pass");
		UserInfo userinfo = UserInfoDAO.getDAO().login(username, pass);
			if(userinfo!=null){
				int loginCount = UserInfoDAO.loginCount;
				session.setAttribute("loginCount", loginCount);
				session.setAttribute("userinfo",userinfo);
				response.sendRedirect("/supermarket/index.jsp");
			//	this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
			//	response.sendRedirect("/supermarket/controls/jsp/index.jsp");
				return;
			}
			else{
				request.setAttribute("err","ÓÃ»§Ãû»òÃÜÂë´íÎó");			
				this.getServletContext().getRequestDispatcher("/user/userlogin.jsp").forward(request, response);
			}
		
	}
	//×¢²á
	public void Regist(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
			String name = request.getParameter("name");
			String password = request.getParameter("password");
			String nic = request.getParameter("nic");
			String sex = request.getParameter("sex");
			String question = request.getParameter("question");
			String answer = request.getParameter("answer");
			String phone = request.getParameter("phone");
			String email = request.getParameter("email");
			String address = request.getParameter("address");
			UserInfo userinfo = new UserInfo();
			userinfo.setName(name);
			userinfo.setPassword(password);
			userinfo.setNic(nic);
			userinfo.setSex(sex);
			userinfo.setQuestion(question);
			userinfo.setAnswer(answer);
			userinfo.setPhone(phone);
			userinfo.setEmail(email);
			userinfo.setAddress(address);
			UserInfoDAO.getDAO().add(userinfo);
			response.sendRedirect("http://localhost:8080/supermarket/user/userlogin.jsp");
		
	}
	//¸ù¾ÝÓÃ»§Ãû  »ñÈ¡ÃÜ±£ÎÊÌâ 
	public void findpassI(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name = request.getParameter("name");
		UserInfo userinfo = UserInfoDAO.getDAO().question(name);
			if(userinfo!=null){
				request.setAttribute("question",userinfo);
				this.getServletContext().getRequestDispatcher("/user/findpassII.jsp").forward(request, response);
				return;
			}
			else{
				request.setAttribute("err","ÓÃ»§Ãû²»´æÔÚ");			
				this.getServletContext().getRequestDispatcher("/user/findpassI.jsp").forward(request, response);
			}			
		
		
	}
	//»Ø´ðÃÜ±£ÎÊÌâÕÒ»ØÃÜÂë
	public void findpassII(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String id = request.getParameter("id");
		String answer = request.getParameter("answer");
		String newpass = request.getParameter("newpass");
		String surepass = request.getParameter("surepass");
		UserInfo userinfo = UserInfoDAO.getDAO().answer(id,answer);
		
		String rightCode = (String)request.getSession().getAttribute("code");
		String code = request.getParameter("code");	
		
		if(newpass.equals(surepass)){
			if(rightCode!=null&&rightCode.equalsIgnoreCase(code)){
				if(userinfo!=null){
					UserInfoDAO.getDAO().updatePass(newpass,id);
					response.sendRedirect("http://localhost:8080/supermarket/user/userlogin.jsp");
					return;
				}
				else{
					UserInfo info = UserInfoDAO.getDAO().questions(id);
					request.setAttribute("err","ÃÜ±£´ð°¸´íÎó");
					request.setAttribute("question", info);
					this.getServletContext().getRequestDispatcher("/user/findpassII.jsp").forward(request, response);
				}
			}
			else{
				UserInfo info = UserInfoDAO.getDAO().questions(id);
				request.setAttribute("codeerr","ÑéÖ¤Âë´íÎó");
				request.setAttribute("question", info);
				this.getServletContext().getRequestDispatcher("/user/findpassII.jsp").forward(request, response);
			}		
		}
		else{		
			UserInfo info = UserInfoDAO.getDAO().questions(id);
			request.setAttribute("passerr","ÃÜÂë²»Ò»ÖÂ");
			request.setAttribute("question", info);
			this.getServletContext().getRequestDispatcher("/user/findpassII.jsp").forward(request, response);
		}
		
				
	}
	//ÑéÖ¤Âë
		public void Code(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			String rightCode = (String)request.getSession().getAttribute("code");
			String code = request.getParameter("code");	
			PrintWriter o = response.getWriter();
			
			if(rightCode!=null&&rightCode.equalsIgnoreCase(code)){
				o.println(1);
			}
			else{
				o.println(0);
			}
			
			o.close();
			
		}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
