package com.css.txw.sso.service.yhxx.impl;

import com.css.txw.sso.service.yhxx.AccountLockService;
import com.css.ggzc.framework.common.util.gy.GyUtils;
import com.css.ggzc.framework.common.exception.util.ServiceExceptionUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import static com.css.txw.sso.constants.ErrorCodeConstants.AUTH_LOGIN_ACCOUNT_LOCK;
import static com.css.txw.sso.constants.ErrorCodeConstants.AUTH_LOGIN_PASSWORD_ERROR_LOCK;

@RefreshScope
@Service
public class AccountLockServiceImpl implements AccountLockService {

    @Value("${accountlock.enable:false}")
    private Boolean enableLock;

    @Value("${accountlock.lockTime:30}")
    private String lockTime;
    @Value("${accountlock.lockLimit:4}")
    private String lockLimit;
    @Value("${accountlock.accountLockLimit:10}")
    private String accountLockLimit;


    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public void handlePasswordError(String yhuuid) {
        if (!enableLock){
            return;
        }
        final String redisKey = formatCountKey(yhuuid);
        final String value = stringRedisTemplate.opsForValue().get(redisKey);
        if (!GyUtils.isNull(value)){
            final Long increment = stringRedisTemplate.opsForValue().increment(redisKey);
            if (increment % Long.parseLong(lockLimit) ==0 && increment!= Long.parseLong(accountLockLimit)){
                stringRedisTemplate.opsForValue().set(formatStatusKey(yhuuid),"1",Long.parseLong(lockTime), TimeUnit.MINUTES);
                throw ServiceExceptionUtil.exception(AUTH_LOGIN_PASSWORD_ERROR_LOCK,"密码输入错误次数过多，锁定{}分钟",lockTime);
            }else if (increment == Long.parseLong(accountLockLimit)){
//                yhxxbMapper.lock(yhuuid,"密码输入错误次数过多",new Date(),"SSO");
                throw ServiceExceptionUtil.exception(AUTH_LOGIN_ACCOUNT_LOCK);
            }
        }else {
            stringRedisTemplate.opsForValue().increment(redisKey);
            stringRedisTemplate.opsForValue().getOperations().expire(redisKey,2,TimeUnit.DAYS);
        }
    }

    @Override
    public Boolean checkLockStatus(String yhuuid) {
        if (enableLock){
            final String redisKey = formatStatusKey(yhuuid);
            return stringRedisTemplate.hasKey(redisKey);
        }else {
            return false;
        }
    }

    @Override
    public void clearCache(String yhuuid) {
        if (enableLock){
            stringRedisTemplate.delete(formatCountKey(yhuuid));
        }
    }

    private static String formatStatusKey(String yhuuid) {
        String redisKeyPrefix = "account_lock_status:%s";
        return String.format(redisKeyPrefix, yhuuid);
    }

    private static String formatCountKey(String yhuuid) {
        String redisKeyPrefix = "account_lock_count:%s";
        return String.format(redisKeyPrefix, yhuuid);
    }
}
