package com.roma.romaapi.service;

import com.roma.romaapi.dao.ApiDao;
import com.roma.romaapi.utils.CommonUtil;
import com.roma.romaapi.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import com.roma.romaapi.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class ApiService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private ApiDao apiDao;
    @Autowired
    SecurityUtils securityUtils;
    @Autowired
    JWTUtil jwtUtil;
    @Autowired
    CommonUtil commonUtil;
    // 登录方法
    public Map<String, Object> login(Map<String, String[]> maps) {
        // 请求参数,获取验证码,验证码token,用户名,密码
        String userName = maps.get("api_name")[0]; // 用户名
        String password = maps.get("api_password")[0]; // 密码
        String captcha = maps.get("api_captcha")[0]; // 用户输入验证码
        String captchaToken = maps.get("captchaToken")[0]; // 验证码token,获取验证码的值
        String captchaTokenValue = redisTemplate.opsForValue().get(captchaToken); // 缓存中验证码的值
        Map<String, Object> loginResult = new HashMap<>();
//        String aa = securityUtils.encodePassword(password); // 加密密码
        // 判断是否存在此用户,用户密码是否正确
        Map<String, Object> UserInfo = apiDao.adminInfoQueryForMap(userName);
        if(UserInfo.containsKey("sysErrorCode")) {
            loginResult.put("sysErrorCode", "500");
            loginResult.put("sysErrorMessage", "账号或密码错误");

            return loginResult;
        }
        Object adminPassword = UserInfo.get("admin_password");
        String stringAdminPassword = adminPassword.toString();
        // 校验密码是否正确
        if(!securityUtils.matchesPassword(password,stringAdminPassword)) {
            // 校验密码是否正确 密码不正确,返回错误
            loginResult.put("sysErrorCode", "500");
            loginResult.put("sysErrorMessage", "账号或密码错误");

            return loginResult;
        }
        // 校验图形验证码
        if(!Boolean.TRUE.equals(redisTemplate.hasKey(captchaToken)) || !Objects.equals(captchaTokenValue, captcha)){
            loginResult.put("sysErrorCode", "500");
            loginResult.put("sysErrorMessage", "验证码错误");

            return loginResult;
        }

        // 通过密码,验证码校验,发放token
        Map<String, Object> jwtInfo = new HashMap<>();
        Object userId = UserInfo.get("id");
        String stringUserId = userId.toString();
        jwtInfo.put("id", stringUserId);
        jwtInfo.put("loginTime", System.currentTimeMillis()/1000+"");
        jwtInfo.put("name", userName);
        String jwtToken = jwtUtil.getToken(jwtInfo);
        Map<String, Object> map = new HashMap<>();
        map.put("loginToken", jwtToken);

        return map;
    }

    // 通过头部,获取用户信息
    public Map<String, Object> userInfo(String headerToken) {
        String userId = commonUtil.getUserIdByHeaderAuthorization(headerToken);
        Map<String, Object> map = new HashMap<>();
        if (userId.length() > 0) {
            // 获取用户信息
            Map<String, Object> userInfo = apiDao.userInfoByIdQueryForMap(userId);
            if (userInfo.containsKey("admin_name")) {
                map.put("userName", userInfo.get("admin_name"));
            }
        }

        return map;
    }

    // 退出登录
    public Map<String, Object> logout(String headerToken) {
        Boolean logout = commonUtil.logoutDeleteRedisToken(headerToken);
        Map<String, Object> map = new HashMap<>();
        map.put("sysErrorCode","50000");
        return map;
    }
}
