package com.sifang.modules.smallprogram;

import com.google.common.collect.Maps;
import com.sifang.modules.config.SfCacheService;
import com.sifang.modules.customer.Service.CustomerService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.site.registerInfo.service.RegisterService;
import com.sifang.modules.site.registerInfo.web.RegisteredController;
import com.sifang.modules.smallprogram.service.LoginService;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.web.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping(value="${adminPath}/main/login")
public class SmallProgramLoginControl extends BaseController {
    /**小程序专用类
    */

    @Autowired
    private LoginService loginService;
    @Autowired
    private RegisterService registerService;

    @Autowired
    private CustomerService customerService;


    @Autowired
    SfCacheService cacheService;

    /**
     * 验证手机登录
     */
    @ResponseBody
    @RequestMapping(value="toLogin")
    public Map<String,String> toLogin(ServletRequest request){
     String loginName=request.getParameter("loginName");
     String password=request.getParameter("password");
     String from = request.getParameter("from");
        Map<String,String> map=loginService.ifLogin(loginName,password,from);
        return map;
    }
    /**
     * 忘记密码后的重置密码
     */
    @ResponseBody
    @RequestMapping(value="resetPassword")
public Object resetPassword(ServletRequest request){
    String telephoneNumber=request.getParameter("telephoneNumber");
    String newPassword=request.getParameter("newPassword");
    Result result=loginService.resetPassword(telephoneNumber,newPassword);
    return  result;
}

    /**
     * 点击获取验证码按钮---获取验证码
     * @param request
     * @return
     */
    @RequestMapping(value = "newSendMsg")
    @ResponseBody
    public Object newSendMsg(HttpServletRequest request, HttpServletResponse response){
        String mobile = request.getParameter("mobile");//手机号
        String code = randomCode();//获取生成的随机数
        Map<String ,String> map = Maps.newHashMap();
        HttpSession session = request.getSession();
        String checkMobile = "";
        if(StringUtils.isNotBlank(mobile) && StringUtils.isNotBlank(code)){
            checkMobile = registerService.checkMobile(mobile,code);//发送短信
          //  cacheService.setex(mobile+"confirmCode",5*60,code);
        }
        map.put("ckeck",checkMobile);
        map.put("sessionId",session.getId());
        return map;
    }

    /**
     * 校验输入的验证码和手机获取的是否一致
     * @param request
     * @return
     */
    @RequestMapping(value = "newCheckConfirmCode")
    @ResponseBody
    public String newCheckConfirmCode(HttpServletRequest request){
        String code = request.getParameter("confirmCode");
        String mobile = request.getParameter("mobile");
        String confirmCode = cacheService.get(mobile+"confirmCode");
        if (StringUtil.isBlank(confirmCode)) {//如果有,就直接返回
            //请重新发送短信
            logger.error("checkConfirmCode:confirmCode is null");
            return "notidNull";
        }
        String codeMsg = confirmCode;
        String checkConfirmCode = "";
        if(StringUtils.isNotBlank(code) && StringUtils.isNotBlank(mobile) && StringUtils.isNotBlank(codeMsg)){
            checkConfirmCode = registerService.checkConfirmCode(code,codeMsg,mobile);
            cacheService.del(mobile+"confirmCode");
        }
        return checkConfirmCode;
    }

    /**
     * 重置密码时 点击获取验证码按钮---获取验证码
     * @param request
     * @return
     */
    @RequestMapping(value = "sendMsg")
    @ResponseBody
    public Object sendMsg(HttpServletRequest request){
        String mobile = request.getParameter("mobile");//手机号
        String code = randomCode();//获取生成的随机数
        Map<String ,String> map = Maps.newHashMap();
        HttpSession session = request.getSession();
        String checkMobile = "";
        try{
            if(StringUtils.isNotBlank(mobile) && StringUtils.isNotBlank(code)){
                checkMobile = customerService.sendingMobile(mobile,code);//发送短信
                cacheService.setex(mobile+"confirmCode",5*60,code);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        map.put("ckeck",checkMobile);
        map.put("sessionId",session.getId());
        return map;
    }
    /**
     * 获取4位随机验证码
     * @return
     */
    public String randomCode(){
        Random rand = new Random();
        String code = "";
        for(int i=0;i<4;i++){
            code = code + String.valueOf(rand.nextInt(10));
        }
        return code;
    }
    private static Logger logger = Logger.getLogger(RegisteredController.class);
    /**
     * 重置密码时校验输入的验证码和手机获取的是否一致
     * @param request
     * @return
     */
    @RequestMapping(value = "checkConfirmCode")
    @ResponseBody
    public String checkConfirmCode(HttpServletRequest request){
        String code = request.getParameter("confirmCode");
        String mobile = request.getParameter("mobile");
        String confirmCode = cacheService.get(mobile+"confirmCode");
        if (StringUtil.isBlank(confirmCode)) {//如果有,就直接返回
            //请重新发送短信
            logger.error("checkConfirmCode:confirmCode is null");
            return "notidNull";
        }
        String codeMsg = confirmCode;
        String checkConfirmCode = "";
        if(StringUtils.isNotBlank(code) && StringUtils.isNotBlank(mobile) && StringUtils.isNotBlank(codeMsg)){
            checkConfirmCode = loginService.checkConfirmCode(code,codeMsg,mobile);
            cacheService.del(mobile+"confirmCode");
        }
        return checkConfirmCode;
    }

}
