package cn.palmte.work.service;

import cn.palmte.work.model.SlideCaptcha;
import cn.palmte.work.model.SlideCaptchaRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import top.jfunc.common.utils.StrUtil;

import java.util.Date;

/**
 * @author xiongshiyan at 2020/9/30 , contact me with email yanshixiong@126.com or phone 15208384257
 */
@Service
public class SlideCaptchaService {
    private static final Logger logger = LoggerFactory.getLogger(SlideCaptchaService.class);


    @Autowired
    private SlideCaptchaRepository slideCaptchaRepository;

    @Value("${fourcal.slideInterval:1800000}")
    private long slideInterval;

    /**
     * @param serialNumber 序列号
     */
    public boolean updateCaptcha(String serialNumber, String identifier, int x, int y){
        if(StrUtil.isEmpty(serialNumber) || StrUtil.isEmpty(identifier)){
            return false;
        }

        SlideCaptcha slideCaptcha = slideCaptchaRepository.findFirstBySerialNumberEquals(serialNumber);
        if(null == slideCaptcha){
            slideCaptcha = new SlideCaptcha();
            slideCaptcha.setSerialNumber(serialNumber);
            slideCaptcha.setStatus(SlideCaptcha.STATUS_INIT);
        }

        slideCaptcha.setX(x);
        slideCaptcha.setY(y);
        slideCaptcha.setIdentifier(identifier);
        slideCaptcha.setModifyTime(new Date());
        slideCaptchaRepository.saveAndFlush(slideCaptcha);

        return true;
    }
    /**
     * @param serialNumber 序列号
     */
    public boolean verifyCaptcha(String serialNumber, String identifier, int x, int y, int allowance){
        if(StrUtil.isEmpty(serialNumber) || StrUtil.isEmpty(identifier)){
            return false;
        }
        SlideCaptcha slideCaptcha = slideCaptchaRepository.findFirstBySerialNumberEquals(serialNumber);
        if(slideCaptcha == null  || !identifier.equals(slideCaptcha.getIdentifier())){
            return false;
        }

        //slide_interval=1800000
        if((System.currentTimeMillis() - slideCaptcha.getModifyTime().getTime()) > slideInterval){
            return false;
        }
        //只判断x，不判断y
        Integer integer = slideCaptcha.getX();
        boolean b = x >= integer - allowance && x <= integer + allowance;
        if(b){
            //校验成功则更新以待后续的校验
            slideCaptcha.setStatus(1);
            slideCaptchaRepository.saveAndFlush(slideCaptcha);
        }
        return b;

    }
    public boolean verifyCaptcha(String serialNumber, String identifier){
        if(StrUtil.isEmpty(serialNumber) || StrUtil.isEmpty(identifier)){
            return false;
        }
        SlideCaptcha slideCaptcha = slideCaptchaRepository.findFirstBySerialNumberEquals(serialNumber);
        if(slideCaptcha == null  || !identifier.equals(slideCaptcha.getIdentifier())){
            return false;
        }

        if((System.currentTimeMillis() - slideCaptcha.getModifyTime().getTime()) > slideInterval){
            return false;
        }
        //查看校验成功没有
        return SlideCaptcha.STATUS_SUCCESS == slideCaptcha.getStatus();

    }

    public boolean deleteCaptchaBy(String serialNumber){
        SlideCaptcha slideCaptcha = slideCaptchaRepository.findFirstBySerialNumberEquals(serialNumber);
        if(null != slideCaptcha){
            slideCaptchaRepository.delete(slideCaptcha);
        }
        return true;
    }
}
