package com.chinacreator.video.validate;

import com.chinacreator.video.bean.CPInfo;
import com.chinacreator.video.bean.ChannelBean;
import com.chinacreator.video.bean.KuaiShouHelpBean;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.CPDao;
import com.chinacreator.video.mapper.ChannelDao;
import com.chinacreator.video.mapper.KuaiShouHelpDao;
import com.chinacreator.video.util.AESUtil;
import com.chinacreator.video.util.DesUtil;
import com.chinacreator.video.util.IpAddrUtil;
import com.chinacreator.video.util.MD5;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.chinacreator.video.util.UsermobUtil.isValid;

@Component
public class KuaiShouValidate {
    private static final Logger log = LoggerFactory.getLogger("kuaiShouHelp");
    private static final String MOB_KEY = "ac22273abb2f4960";
    @Autowired
    private KuaiShouHelpDao kuaiShouHelpDao;

    @Autowired
    private ChannelDao channelDao;
    /**
     * 验证时间戳
     * @param
     * @throws BussinessException
     */
    public void valiTime(KuaiShouHelpBean bean) throws Exception {
        String timetamp = bean.getTimestamp();
        if(StringUtils.isEmpty(timetamp)) {
            throw new BussinessException("10001", "timestamp不能为空");
        }
        long localTimestamp = (System.currentTimeMillis())/1000;
        try {
            if((localTimestamp - NumberUtils.toLong(timetamp)) >300 || (localTimestamp - NumberUtils.toLong(timetamp)) <-300) {
                System.out.println("====================="+(localTimestamp - NumberUtils.toLong(timetamp)));
                throw new BussinessException("10002", "timestamp已失效");
            }
        } catch (Exception e) {
            if(e instanceof BussinessException) {
                throw e;
            }else {
                throw new BussinessException("10002", "timestamp已失效");
            }
        }
    }

    /**
     * 验证渠道
     * @param bean
     * @throws Exception
     */
    /*public void valiChannel(KuaiShouHelpBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getChannel())) {
            throw new BussinessException("10023", "渠道参数不能为空");
        }
        KuaiShouHelpBean kuaiShouHelpBean = kuaiShouHelpDao.queryChannel(bean.getChannel());
        if(kuaiShouHelpBean == null) {
            throw new BussinessException("10024", "渠道参数错误");
        }
    }*/

    public void validate(KuaiShouHelpBean bean) throws Exception {
        String userid = bean.getUserid();
        String message = bean.getMessage();
        if(StringUtils.isEmpty(userid)) {
            throw new BussinessException("10007", "手机号不能为空");
        }
        if(StringUtils.isEmpty(message)) {
            throw new BussinessException("10008", "反馈信息不能为空");
        }

        if(isValid(userid) && userid.length() == 11){
            bean.setUserid(userid);
            log.info(userid);
        }else{
            throw new BussinessException("10009", "手机号错误");
        }
        if(message.equals("")||message == null){
            throw new BussinessException("10011", "反馈信息不得为空");
        }
        if(message.length() <= 200){
            bean.setMessage(message);
        }else{
            throw new BussinessException("10010", "反馈信息长度超过限制");
        }

    }

    public void orderValidate(KuaiShouHelpBean bean) throws Exception {
        String pwd = channelDao.queryChannel(bean.getChannel()).getPassword();
        String userid = bean.getUserid();
        String decodeUserid = DesUtil.decode(userid,pwd);
        String channel = bean.getChannel();
        String timestamp = bean.getTimestamp();
        String signature = bean.getSignature();
        if(StringUtils.isEmpty(userid)) {
            throw new BussinessException("10007", "手机号不能为空");
        }
        if(decodeUserid.length() != 11){
            throw new BussinessException("10008", "手机号长度不合法");
        }

        if(StringUtils.isEmpty(signature)) {
            throw new BussinessException("10006", "签名验证失败");
        }
        String s = MD5.MD5Encode(userid + channel + timestamp + pwd);
        if(!s.equals(signature)){
            throw new BussinessException("10006", "签名验证失败");
        }
    }

    public void validateChannel(KuaiShouHelpBean bean) throws Exception {
        String channel = bean.getChannel();
        if(StringUtils.isEmpty(channel)) {
            throw new BussinessException("10023", "渠道参数不能为空");
        }
        ChannelBean channelBean = channelDao.queryChannel(bean.getChannel());
        if(channelBean == null) {
            throw new BussinessException("10024", "渠道参数错误");
        }
    }
}
