package com.zcloud.modules.sys.controller;

import cn.hutool.core.util.StrUtil;
import com.zcloud.common.utils.AesEncryptor;
import com.zcloud.common.utils.R;
import com.zcloud.modules.sys.entity.SysDictionariesEntity;
import com.zcloud.modules.sys.entity.SysUserEntity;
import com.zcloud.modules.sys.form.SysLoginForm;
import com.zcloud.modules.sys.service.SysCaptchaService;
import com.zcloud.modules.sys.service.SysUserService;
import com.zcloud.modules.sys.service.SysUserTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

/**
 * 登录相关
 */
@RestController
@Api(tags = "登录接口")
public class SysLoginController extends AbstractController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysCaptchaService sysCaptchaService;
    @Autowired
    private SysUserTokenService sysUserTokenService;

    /**
     * 验证码
     */
    @PostMapping("captcha.jpg")
    public void captcha(HttpServletResponse response, String uuid) throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //获取图片验证码
        BufferedImage image = sysCaptchaService.getCaptcha(uuid);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }

    @PostMapping("/sys/login")
    @ApiOperation("登录")
    public Map<String, Object> login(@RequestBody SysLoginForm form) throws IOException {
//		boolean captcha = sysCaptchaService.validate(form.getUuid(), form.getCaptcha());
//		if(!captcha){
//			return R.error("验证码不正确");
//		}
        //密码解密
        String decryptUsername = AesEncryptor.aesDecrypt(form.getUsername());
        String decryptPassword = AesEncryptor.aesDecrypt(form.getPassword());

        //用户信息
        SysUserEntity user = sysUserService.queryByUserName(decryptUsername);
        //账号不存在、密码错误
        if (user == null || !user.getPassword().equals(new SimpleHash("SHA-1", decryptUsername, decryptPassword).toString())) {
            return R.error(200, "账号或密码不正确");
        }
        //生成token，并保存到数据库
        R r = sysUserTokenService.createToken(user.getUserId());
        return r;
    }

    /**
     * 退出
     */
    @PostMapping("/sys/logout")
    @ApiOperation("退出登录")
    public R logout() {
        sysUserTokenService.logout(getUserId());
        return R.ok();
    }

    /**
     * 刷新token
     */
    @PostMapping("/sys/refreshToken")
    @ApiOperation("刷新token")
    public R refreshToken() {
        sysUserTokenService.refreshToken(getUserId());
        return R.ok();
    }

    public static void main(String[] args) {
        System.out.println(new Sha256Hash("666666", "999999aA"));
    }
}
