package com.sddtc.controller.account;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.sddtc.model.User;
import com.sddtc.service.user.UserService;
import com.sddtc.utils.accounts.factory.Provider;
import com.sddtc.utils.accounts.factory.SendEmailFactory;
import com.sddtc.utils.accounts.factory.Sender;
import com.sddtc.utils.param.UserParam;

/**
 * 
 * 1.登陆
 * 2.登出
 * 3.帐号相关修改
 * @author sddtc
 *
 */
@Controller
@RequestMapping("/account")
public class AccountController {
    private static Logger logger = LoggerFactory
            .getLogger(AccountController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String register() {
        return "account/login";
    }

    /**
     * 登陆
     * 发送登陆验证邮件
     * @param user
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String doRegister(@ModelAttribute("user") User user) {
        String email = user.getLogin_id();
        logger.info("待登陆邮箱账号:{}", email);
        
        String nickName = email.split("@")[0];
        user.setNick_name(nickName);
        long timeCode = System.currentTimeMillis();
        String pwd = nickName + '@' + timeCode;
        user.setPassword(pwd);

        userService.add(user);
        
        Provider provider = new SendEmailFactory();
        Sender sender = provider.produce();
        sender.send(email, pwd);

        return "redirect:/user/" + user.getId();
    }
    
    /**
     * 从邮箱过来的登陆验证
     * @param request
     * @return
     */
    @RequestMapping(value="authLogin", method=RequestMethod.GET)
    public String authLogin(HttpServletRequest request) {
        String auth = (String) request.getParameter("auth");
        
        try {
            auth = URLDecoder.decode(auth, "utf-8");
            logger.info("通过登陆邮件确认auth:{}", auth);
            String nickName = auth.split("@")[0];
            
            UserParam param = new UserParam();
            param.setPassword(auth);
            User user = userService.get(param);
            
            if(null != user) {
                logger.info("欢迎{}回来:{}", user.getNick_name(), new Date());
                request.getSession(false).setAttribute("currUser", user);
                
                return "redirect:/user/" + user.getId();
            } else {
                logger.info("验证失败,登陆失败:{}", nickName);
                return "redirect:/account/login";
            }
        } catch (UnsupportedEncodingException e) {
            logger.error("auth解码出错", e);
        }
        return "redirect:/account/error";
    }
    
    @RequestMapping(value="logout", method=RequestMethod.GET)
    public String logout(HttpServletRequest request) {
    	HttpSession session = request.getSession(false);
    	if(null != session) {
    		session.removeAttribute("currUser");
    	}
    	
    	return "redirect:/";
    }
    
    @RequestMapping(value="/", method=RequestMethod.GET)
    public String accounts() {
    	return "account/accounts";
    }
    
    @RequestMapping(value="update", method=RequestMethod.POST)
    public ModelAndView update(@ModelAttribute("user") User user, HttpServletRequest request) {
    	ModelAndView mv = new ModelAndView("account/accounts");
    	userService.update(user);
    	mv.addObject("result", "更新成功");
    	
    	//update session
    	request.setAttribute("currUser", user);
    	
    	return mv;
    }
}

@@ -1573,50 +1573,8 @@
 ();%0A
-        logger.info(%22%E5%BE%85%E7%99%BB%E9%99%86%E9%82%AE%E7%AE%B1%E8%B4%A6%E5%8F%B7:%7B%7D%22, email);%0A
     
@@ -2359,58 +2359,8 @@
 %22);%0A
-            logger.info(%22%E9%80%9A%E8%BF%87%E7%99%BB%E9%99%86%E9%82%AE%E4%BB%B6%E7%A1%AE%E8%AE%A4auth:%7B%7D%22, auth);%0A
     
@@ -2598,83 +2598,8 @@
 ) %7B%0A
-                logger.info(%22%E6%AC%A2%E8%BF%8E%7B%7D%E5%9B%9E%E6%9D%A5:%7B%7D%22, user.getNick_name(), new Date());%0A
     

clear unused comment