package com.pj.project4sp.admin4login;

import javax.servlet.http.HttpServletRequest;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.pj.utils.cache.RedisUtil;
import com.wf.captcha.SpecCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pj.project4sp.admin.SpAdmin;
import com.pj.project4sp.admin.SpAdminUtil;
import com.pj.project4sp.role4permission.SpRolePermissionService;
import com.pj.project4sp.spcfg.SpCfgUtil;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.sg.NbUtil;
import com.pj.utils.so.SoMap;

import cn.dev33.satoken.stp.StpUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * admin账号相关的接口 
 * @author kong
 *
 */
@RestController
@RequestMapping("/AccAdmin/")
public class SpAccAdminController {

	
	@Autowired
	SpAccAdminService spAccAdminService;
	
	@Autowired
	SpRolePermissionService spRolePermissionService;

	@Autowired
	private PasswordEncoder passwordEncoder;

	/** 账号、密码登录  */
	@RequestMapping("doLogin")
	AjaxJson doLogin(String key, String password, String code, String verCode) {
		// 1、验证参数
		if(NbUtil.isOneNull(key, password)) {
			return AjaxJson.getError("请提供key与password参数");
		}
		if (NbUtil.isOneNull(verCode, code)) {
			return AjaxJson.getError("请输入验证码");
		}
		String cacheCode = RedisUtil.get(code);
		if (NbUtil.isNull(cacheCode)) {
			return AjaxJson.getError("验证码已过期");
		}
		RSA rsa = KEY_CACHE.get(code);
		if (rsa==null){
			return AjaxJson.getError("请重新刷新登录");
		}
		if (!StrUtil.equals(cacheCode.toUpperCase(), verCode.toUpperCase())) {
			return AjaxJson.getError("验证码不正确");
		}

		password=rsa.decryptStr(password, KeyType.PrivateKey);
		RedisUtil.del(code);
		KEY_CACHE.remove(code);
		return spAccAdminService.doLogin(key, password);
	}
	private TimedCache<String, RSA> KEY_CACHE = CacheUtil.newTimedCache(60000);
	@PostMapping("captcha")
	public AjaxJson render() {
		SpecCaptcha captcha = new SpecCaptcha(130, 48);
		String key = UUID.randomUUID().toString();
		String verCode = captcha.text().toLowerCase();   // 获取验证码的字符
		RedisUtil.setBySECONDS(key, verCode, 60);
		Map<String, String> map = new HashMap<>();
		map.put("key", key);
		map.put("image", captcha.toBase64());
		RSA rsa = new RSA();
		String publicKeyBase64Key = rsa.getPublicKeyBase64();
		KEY_CACHE.put(key, rsa);
		map.put("pKey", publicKeyBase64Key);
		return AjaxJson.getSuccessData(map);
	}


	/** 退出登录  */
	@RequestMapping("doExit")
	AjaxJson doExit() {
		StpUtil.logout();
		return AjaxJson.getSuccess();
	}
	

	/** 管理员登录后台时需要返回的信息 */
	@RequestMapping("fristOpenAdmin")
	AjaxJson fristOpenAdmin(HttpServletRequest request) {
		StpUtil.checkLogin();
		// 当前admin
		SpAdmin admin = SpAdminUtil.getCurrAdmin();
		
		// 组织参数 (admin信息，权限信息，配置信息)
		SoMap map = new SoMap();
		map.set("admin", SpAdminUtil.getCurrAdmin());	
		map.set("per_list", spRolePermissionService.getPcodeByRid2(admin.getRoleId()));				
		map.set("app_cfg", SpCfgUtil.getAppCfg());	
		return AjaxJson.getSuccessData(map); 
	}
	
	
	
	
	
}
