package cn.iselab.mooctest.user.web.ctrl;

import cn.iselab.mooctest.rpc.user.data.UserDTO;
import cn.iselab.mooctest.user.configure.ThirdPartyAuthConfiguration;
import cn.iselab.mooctest.user.constants.ResponseStatus;
import cn.iselab.mooctest.user.constants.UrlConstants;
import cn.iselab.mooctest.user.data.ResponseResult;
import cn.iselab.mooctest.user.model.VerifyCode;
import cn.iselab.mooctest.user.service.MailService;
import cn.iselab.mooctest.user.service.VerifyCodeService;
import cn.iselab.mooctest.user.util.OSSUtil;
import cn.iselab.mooctest.user.web.data.BankAccountInfoVO;
import cn.iselab.mooctest.user.web.data.UserVO;
import cn.iselab.mooctest.user.web.logic.UserLogic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: xuexb
 * @Date: 2018.12.24 19:11
 */
@RestController
public class TestController extends BaseController{

    @Autowired
    MailService mailService;

    @Autowired
    VerifyCodeService verifyCodeService;

    @Autowired
    UserLogic userLogic;

    @Autowired
    ThirdPartyAuthConfiguration authConfiguration;

    @Autowired
    OSSUtil ossUtil;

    @RequestMapping(value = "/test/first", method = RequestMethod.GET)
    public Map<String, Object> firstResp(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        request.getSession().setAttribute("msg", "hello world");
        map.put("requestURL", request.getRequestURL());
        return map;
    }

    @RequestMapping(value = "/test/sessions", method = RequestMethod.GET)
    public Object sessions(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        map.put("sessionsID", request.getSession().getId());
        map.put("message", request.getSession().getAttribute("msg"));
        return map;
    }

    @RequestMapping(value = "/test/mail", method = RequestMethod.POST)
    public ResponseResult<Object> sendMail(UserDTO userDTO){
        ResponseResult<Object> result = new ResponseResult<>();
        LOG.info("\nreceiver: "+userDTO.getEmail()+"\n");
        try {
            mailService.sendVerifyEmail("diors_po@126.com", "171256175@qq.com", "1234");
            result.init(ResponseStatus.SUCCESS, "发送成功", null);
        } catch (MessagingException e) {
            result.init(ResponseStatus.FAILED, "发送失败", null);
            LOG.error("发送邮件出错",e);
        }
        return result;
    }

    @RequestMapping(value = "/test/error", method = RequestMethod.GET)
    public ModelAndView errorPage(Model model){
        model.addAttribute("message","老铁, 你的页面飞走了~");
        return new ModelAndView("error_page");
    }

    @RequestMapping(value = "/test/success", method = RequestMethod.GET)
    public ModelAndView successPage(Model model){
        model.addAttribute("message","老铁, 你注册成功了~");
        return new ModelAndView("success_page");
    }

    @RequestMapping(value = "/test/register", method = RequestMethod.GET)
    public ModelAndView registerPage(){
        // ResponseResult<Object> result = new ResponseResult<>();
        // result.init(1000, "哈哈哈哈哈", null);
        // model.addAttribute("result", result);
        return new ModelAndView("register");
    }


    @RequestMapping(value = "/test/veirycode", method = RequestMethod.PUT)
    public String testCode(@RequestBody VerifyCode verifyCode){
        String type = verifyCode.getType();
        String receiver = verifyCode.getReceiver();
        verifyCode.setCreateTime(new Timestamp(System.currentTimeMillis()));
        verifyCode.setExpiredTime(new Timestamp(System.currentTimeMillis()+10000));
        verifyCode.setCode("123321");
        Long id = verifyCodeService.saveVerifyCode(verifyCode).getId();
        return "id===="+id;
    }

    @RequestMapping(value = "/test/mobilecode", method = RequestMethod.GET)
    public ResponseResult<String> mobileCode(UserDTO userDTO){
        return userLogic.sendMobileVerfication(userDTO);
    }

    @RequestMapping(value = "/test/emails", method = RequestMethod.GET)
    public void mails(){
            mailService.sendArrearsEmail();
    }

    @RequestMapping(value = "/test/findpwd", method = RequestMethod.GET)
    public ModelAndView findpwd(){
        return new ModelAndView("forget_password");
    }

    @RequestMapping(value = "/test/resetpage", method = RequestMethod.GET)
    public ModelAndView resetPage(){
        ModelAndView modelAndView = new ModelAndView();
        UserVO userVO = new UserVO();
        userVO.setEmail("171333333@qq.com");
        userVO.setVerifyCode("sdlkfjlsfksld");
        modelAndView.setViewName("forget_password_reset");
        modelAndView.addObject("user", userVO);
        return modelAndView;
    }

    @RequestMapping(value = "/test/github/appinfo", method = RequestMethod.GET)
    public String githubInfo(){
        return "client_id: " + authConfiguration.getGitHubClientId()
                + "client_secret: "+authConfiguration.getGitHubClientSecret();
    }

    @RequestMapping(value = "/test/upload/page", method = RequestMethod.GET)
    public ModelAndView uploadBankInfo(){
        return new ModelAndView("upload_card_info");
    }

    @RequestMapping(value = "/test/upload/bankinfo", method = RequestMethod.POST)
    public String doUpload(BankAccountInfoVO infoVO){
        String str = ossUtil.upload(infoVO.getID_card_back(), OSSUtil.FileDirType.BANKINFO);
        return str;
    }

    @RequestMapping(value = "/test/url", method = RequestMethod.GET)
    public String tttttt(){
        return UrlConstants.GO_LOGIN + "\n" + UrlConstants.DEFAULT_GOTO;
    }

}
