package ink.wgink.login.base.controller.route;

import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.login.base.consts.IUserCenterConst;
import ink.wgink.login.base.service.IOAuthService;
import ink.wgink.login.base.service.login.ILoginFormService;
import ink.wgink.util.verification.code.Captcha;
import ink.wgink.util.verification.code.GifCaptcha;
import ink.wgink.util.verification.code.SpecCaptcha;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: LoginController
 * @Description: 登录
 * @Author: WangGeng
 * @Date: 2021/2/25 19:16
 * @Version: 1.0
 **/
@Controller
@RequestMapping("oauth")
public class OAuthRouteController {

    @Autowired
    private IOAuthService oAuthService;
    @Autowired
    private ILoginFormService loginFormService;

    /**
     * 登录
     *
     * @return
     */
    @GetMapping("login")
    public ModelAndView login(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        Map<String, String> pageParams = oAuthService.getPageParams(request);
        mv.addObject(IUserCenterConst.PAGE_PARAMS, pageParams);
        mv.addObject(IUserCenterConst.CUSTOM_LOGIN_FORM, loginFormService.getActive());
        mv.setViewName("login");
        return mv;
    }

    /**
     * 登出
     *
     * @param request
     * @param response
     */
    @GetMapping("logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) {
        new SecurityContextLogoutHandler().logout(request, null, null);
        try {
            String referer = request.getHeader("referer");
            response.sendRedirect(referer.replace("/default-main", ""));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 验证码
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("verification-code/{type}")
    public void verificationCode(@PathVariable("type") String type, HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession(true);
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        Captcha captcha;
        if (StringUtils.equalsIgnoreCase("png", type)) {
            response.setContentType("image/png");
            captcha = new SpecCaptcha(130, 38, 5);
        } else {
            response.setContentType("image/gif");
            captcha = new GifCaptcha(130, 38, 5);
        }
        captcha.out(response.getOutputStream());
        session.setAttribute(ISystemConstant.VERIFICATION_CODE, captcha.text().toLowerCase());
    }

}
