package com.huimv.admin.controller.primary;

import com.huimv.admin.domain.primary.SysAccountEntity;
import com.huimv.admin.service.primary.ILoginService;
import com.huimv.admin.session.AccessToken;
import com.huimv.admin.session.HmSession;
import com.huimv.admin.utils.CheckNumber;
import com.huimv.admin.utils.IpTools;
import com.huimv.admin.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;

@CrossOrigin
@RestController
@RequestMapping(value = "/my")
public class LoginController {
    @Autowired
    ILoginService iLoginService;

    private static String text;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public AccessToken login(HttpSession session, @RequestParam(name = "accountName", required = true) String accountName, @RequestParam(name = "password", required = true) String password, HttpServletRequest req)  {
        // 对密码加密
//        Result result = iLoginService.login(accountName,         .getMD5Str(password));
        // 未对密码加密
        Result result = iLoginService.login(accountName, password);
        if(result.getCode() != 1000){
            AccessToken accessToken = new AccessToken();
//            accessToken.setToken(session.getId());
//            accessToken.setLoginIp(userIp);
            accessToken.setCreated(new Date());
            accessToken.setAccountName(accountName);
            accessToken.setCode(result.getCode());
            accessToken.setMessage(result.getMessage());
            return accessToken;
        }

        SysAccountEntity accountEntity = (SysAccountEntity) result.getData();
        String userIp = IpTools.getRemoteHost(req);
        //spring-session默认使用uuid作为sessionId返回：
        System.out.println("当前请求的sessionId为：" + session.getId());
        HmSession hmSession = new HmSession();
        hmSession.setAccountEntity(accountEntity);
        session.setAttribute("hmSession", hmSession);

        AccessToken accessToken = new AccessToken();
        accessToken.setToken(session.getId());
        accessToken.setLoginIp(userIp);
        accessToken.setCreated(new Date());
        accessToken.setAccountName(accountName);
        accessToken.setCode(result.getCode());
        return accessToken;
    }

    @RequestMapping("/logout")
    public Result logout(@RequestParam(name = "accountName", required = true) String accountName, @RequestParam(name = "password", required = true) String password) {
        return iLoginService.logout();
    }

    //获取验证码
    @RequestMapping(value = "getCheckNumber", produces = {"text/html;charset=UTF-8"})
    public void getCheckNumber(HttpServletResponse response) throws IOException {
        //创建对象
        CheckNumber checkNumber = new CheckNumber();
        //获取图片对象
        BufferedImage image = checkNumber.getImage();
        //获得图片的文本内容
        text = checkNumber.getText();
        //把图片返回前端
        ImageIO.write(image, "JPEG", response.getOutputStream());
    }

    @RequestMapping("/checkNumber")
    public Result checkNumber(String userCaptcha){
        //获取用户输入的校验码并进行比较
       // String sessionCheckNumber = (String) session.getAttribute("checkNumber");
        System.out.println(userCaptcha +":"+ text);
        if (text.equalsIgnoreCase(userCaptcha)) {
            return new Result(10001,"验证码正确",true);
        }
        return new Result(10002,"验证码错误",false);
    }
}
