package com.backendsys.modules.app.user.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.backendsys.exception.CustException;
import com.backendsys.modules.app.user.dao.AppUserDao;
import com.backendsys.modules.app.user.entity.AppAuth;
import com.backendsys.modules.app.user.entity.AppUser;
import com.backendsys.modules.app.user.service.AppAuthService;
import com.backendsys.modules.app.user.utils.AuthWechat.WechatUtil;
import com.backendsys.modules.common.config.redis.utils.RedisUtil;
import com.backendsys.modules.common.config.security.entity.SecurityAppUserInfo;
import com.backendsys.modules.common.config.security.utils.*;
import com.backendsys.modules.system.entity.TokenCatch;
import com.backendsys.modules.system.service.SysCommonService;
import com.backendsys.utils.response.ResultEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.code.kaptcha.Producer;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class AppAuthServiceImpl implements AppAuthService {

    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private WechatUtil wechatUtil;
    @Autowired
    private CaptchaUtil captchaUtil;
    @Autowired
    private LockStatusUtil lockStatusUtil;
    @Autowired
    private HttpRequestUtil httpRequestUtil;

    @Autowired
    private AppUserDao appUserDao;
    @Autowired
    private SysCommonService sysCommonService;

    @Value("${spring.application.name}")
    private String APPLICATION_NAME;
    @Value("${CAPTCHA_DURATION}")
    private Integer CAPTCHA_DURATION;
    @Value("${REDIS_LOGIN_TOKEN_PREFIX}")
    private String REDIS_LOGIN_TOKEN_PREFIX;

    @Autowired
    private Producer captchaProducer;

    @Override
    public void renderCaptcha(HttpServletResponse response) throws IOException {
        try {

            byte[] captchaChallengeAsJpeg;
            ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();

            String createText = captchaProducer.createText();
            // 获得当前 (UA + IP) 生成的 Key
            String captchaRedisKey = httpRequestUtil.getKaptchaKey("APP");
            // 保存 验证码字符串 到 redis 中
            redisUtil.setCacheObject(captchaRedisKey, createText, this.CAPTCHA_DURATION, TimeUnit.MILLISECONDS);
            // 返回 BufferedImage 对象并转为 byte 写入到 byte 数组中
            BufferedImage challenge = captchaProducer.createImage(createText);
            ImageIO.write(challenge, "jpg", jpegOutputStream);

            // 定义response输出类型为image/jpeg类型，使用response输出流输出图片的byte数组
            captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
            response.setHeader("Cache-Control", "no-store");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/jpeg");

            ServletOutputStream responseOutputStream = response.getOutputStream();
            responseOutputStream.write(captchaChallengeAsJpeg);
            responseOutputStream.flush();
            responseOutputStream.close();

        } catch (IOException e) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }


    // [方法] 登录失败 (errMsg: 错误提示文本, phone: 手机号码, intercept: 是否拦截)
    public void loginFail(String errMsg, String phone, Boolean isIntercept) {
        // 验证码是否必填
        Boolean currentCaptchaRequired = captchaUtil.isCaptchaRequired(APPLICATION_NAME + "-app-login-required-captcha-" + phone, 3);
        System.out.println("(loginFailByUsername) currentCaptchaRequired = " + currentCaptchaRequired);

        // 删除图形验证码
        redisUtil.delete(httpRequestUtil.getKaptchaKey("APP"));
        // 添加登录错误的冻结标记
        if (isIntercept) lockStatusUtil.setLockStatus(APPLICATION_NAME + "-app-login-error", phone);

        if (currentCaptchaRequired) {
            throw new CustException(errMsg, ResultEnum.INVALID_CREDENTIALS.getCode(), Map.of("is_captcha_required", true));
        } else {
            throw new CustException(errMsg, ResultEnum.INVALID_CREDENTIALS.getCode());
        }
    }

    // [方法] 登录成功
    public AppUser loginSuccess(AppUser appUser) {

        // 删除图形验证码缓存 (如果有的话)
        redisUtil.delete(httpRequestUtil.getKaptchaKey("APP"));

        // 删除旧的登录缓存
        tokenUtil.deleteRedisLoginToken(appUser.getLast_login_uuid());

        // 判断用户是否启用
        Integer status = appUser.getStatus();
        if (status != null && status.equals(-1)) throw new CustException("该用户已被禁用，请与客服联系");

        // 判断用户是否已删除
        Integer del_flag = appUser.getDel_flag();
        if (del_flag != null && del_flag.equals(1)) throw new CustException("当前用户不可用，请与客服联系");

        // 设置 最后一次的登录信息 (uuid, ip, 登录时间)
        String uuid = Convert.toStr(UUID.randomUUID());
        appUser.setUser_id(appUser.getId());
        appUser.setLast_login_uuid(uuid);
        appUser.setLast_login_ip(httpRequestUtil.getIpAddr());

        // 最后登录时间 (本地时间)
        String localTimeStr = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        appUser.setLast_login_time(localTimeStr);

//        // UTC 时间
//        String utcTimeStr = DateUtil.date().setTimeZone(TimeZone.getTimeZone("UTC"))
//                .toString("yyyy-MM-dd HH:mm:ss");
//        appUser.setLast_login_time(utcTimeStr);

        // [DB] 更新前台用户信息
        appUserDao.updateById(appUser);


        // [系统配置] 微信用户默认登录过期时间(小时)
        Integer APP_USER_LOGIN_DURATION_DEFAULT = Convert.toInt(sysCommonService.getCommonByTag("APP_USER_LOGIN_DURATION_DEFAULT"));
        // 将小时转换为毫秒
        Long DEFAULT_MILLISECONDS = APP_USER_LOGIN_DURATION_DEFAULT * DateUnit.HOUR.getMillis();
        Integer token_duration_hours = Convert.toInt(DEFAULT_MILLISECONDS / 3600000L);

        // Token 记录的登录时间，是本地时间
        Date token_expiration = new Date((new Date()).getTime() + DEFAULT_MILLISECONDS);
        appUser.setToken_expiration(DateUtil.format(token_expiration, "yyyy-MM-dd HH:mm:ss"));
        appUser.setRole("APP_USER");

        // 生成 Token
        SecurityAppUserInfo securityUserInfo = JSONUtil.toBean(JSONUtil.parseObj(appUser), SecurityAppUserInfo.class);
        String token = jwtUtil.createAppJwtToken(securityUserInfo);
        String token_redis_key = REDIS_LOGIN_TOKEN_PREFIX + uuid;
        appUser.setToken(token);

        // [Redis] 将 Token 存入缓存
        TokenCatch tokenCatch = new TokenCatch(token, null);
        redisUtil.setCacheObject(token_redis_key, JSONUtil.toJsonStr(tokenCatch), token_duration_hours, TimeUnit.HOURS);

        return appUser;
    }

    private void setLoginRequired(String key) {
        Object captchaValue = redisUtil.getCacheObject(APPLICATION_NAME + "-app-login-required-captcha-" + key);
        Integer currentErrCount = (captchaValue == null) ? 1 : (Convert.toInt(captchaValue) + 1);
        redisUtil.setCacheObject(APPLICATION_NAME + "-app-login-required-captcha-" + key, currentErrCount, 1, TimeUnit.MINUTES);
        System.out.println("currentErrCount: " + currentErrCount);
    }
    private void cleanLoginRequired(String key) {
        redisUtil.delete(APPLICATION_NAME + "-app-login-required-captcha-" + key);
    }



    /**
     * 前台用户登录 (手机账号)
     */
    @Override
    public AppUser loginWithPhone(AppAuth appAuth) {

        String phone = appAuth.getPhone();
        String password = appAuth.getPassword();
        String captcha = appAuth.getCaptcha();

        // 判断是否处于登录错误的冻结状态 (2分钟内错误5次，则出现冻结提示)
        lockStatusUtil.checkLockStatus(APPLICATION_NAME + "-app-login-error", phone);

        // -- 判断是否需要输入验证码 ----------------------------------------------------
        // - 当输错 3 次密码时，需要输入验证码
        // - 当输错后 1 分钟后重置
        Boolean isCaptchaRequired = captchaUtil.isCaptchaRequired(APPLICATION_NAME + "-app-login-required-captcha-" + phone, 3);
        if (isCaptchaRequired) {
            Boolean isCaptchaEmpty = StrUtil.isEmpty(captcha);
            Boolean isCpatchaValid = (captchaUtil.isCaptchaValid(captcha, httpRequestUtil.getKaptchaKey("APP")));
            if (isCaptchaEmpty) { loginFail("验证码不能为空", phone, false); return null; }
            if (!isCpatchaValid) { loginFail("验证码错误", phone, false); return null; }
        }
        // --------------------------------------------------------------------------

        // [Method] 判断 用户 是否存在 && 密码是否正确
        AppUser appUser = appUserDao.selectOne(new LambdaQueryWrapper<AppUser>().eq(AppUser::getPhone, phone));
        if (appUser == null) {
            // 输入错误时，计数器叠加，并且设置重置时间 (会一直叠加，直到重置 或 登录成功)
            setLoginRequired(phone);
            // [登录失败] 用户不存在
            loginFail("手机号码或密码错误", phone, true);
            return null;
        } else {
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            if (!encoder.matches(password, appUser.getPassword())) {
                // 输入错误时，计数器叠加，并且设置重置时间 (会一直叠加，直到重置 或 登录成功)
                setLoginRequired(phone);
                // [登录失败] 密码不正确
                loginFail("手机号码或密码错误", phone, true);
            }
            // [登录成功]
            cleanLoginRequired(phone);
            return loginSuccess(appUser);
        }

    }

    /**
     * 前台用户登录 (微信小程序用户)
     */
    @Override
    public AppUser loginWithWechatMiniprogram(AppAuth appAuth) {

        String code = appAuth.getCode();
        JSONObject response = wechatUtil.getCode2Session(code);
        // [失败] 返回值：{"errcode":40164,"errmsg":"invalid ip 116.31.165.86 ipv6 ::ffff:116.31.165.86, not in whitelist, rid: 67d15951-5b3f2778-79d6f047"}
        // [成功] 返回值：{"session_key":"uQAcry2PC1Lx/Krp+6rr0g==","openid":"oSB9r7T7kN1bC7PabJ7RmTUiaJmo"}

        // - errcode: 错误码，请求失败时返回
        if (!response.containsKey("errcode")) {

            String openid = response.getStr("openid");
            String session_key = response.getStr("session_key");

            String uuid = Convert.toStr(UUID.randomUUID());
            String last_login_ip = httpRequestUtil.getIpAddr();
            String last_login_time = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

            // [Get] 判断用户是否存在 (openid)
            AppUser appUser = appUserDao.selectOne(new LambdaQueryWrapper<AppUser>().eq(AppUser::getWechat_open_id, openid));
            if (appUser == null) {
                // [Insert] 不存在，则创建
                appUser = new AppUser();
                appUser.setWechat_open_id(openid);
                appUser.setNickname("微信用户" + RandomUtil.randomStringUpper(4));
                //
                appUser.setLast_login_uuid(uuid);
                appUser.setLast_login_ip(last_login_ip);
                appUser.setLast_login_time(last_login_time);
                appUserDao.insert(appUser);
            } else {
                // [Update] 更新时间
                appUser.setLast_login_uuid(uuid);
                appUser.setLast_login_ip(last_login_ip);
                appUser.setLast_login_time(last_login_time);
                appUserDao.updateById(appUser);
            }
            appUser.setUser_id(appUser.getId());
            appUser.setSession_key(session_key);

            // 登录成功
            return loginSuccess(appUser);
        } else {
            throw new CustException(response.getStr("errmsg"));
        }

    }

}
