package com.chinacreator.video.service;

import com.chinacreator.video.bean.ChannelBean;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.ChannelDao;
import com.chinacreator.video.mapper.UniMobileDao;
import com.chinacreator.video.util.DesUtil;
import com.chinacreator.video.util.MD5;
import com.chinacreator.video.validate.ChannelValidate;
import com.chinacreator.video.validate.IpValidate;
import com.chinacreator.video.validate.TimetampValidate;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author czw
 */
@Component
public class UserCreditService {

    private static final Logger log = LoggerFactory.getLogger("uniMobile");

    @Autowired
    private UniMobileDao uniMobileDao;
    @Autowired
    private TimetampValidate timetampValidate;

    @Autowired
    private ChannelValidate channelValidate;

    @Autowired
    private ChannelDao channelDao;
    @Autowired
    private IpValidate ipValidate;


    public void check(OrderInfo orderInfo) throws Exception {
        paramVali(orderInfo);//参数验证
        timetampValidate.validate(orderInfo.getTimestamp()); //timetamp验证
        channelValidate.validate(orderInfo.getChannel()); //渠道验证
        ipValidate.volidate(orderInfo,"1"); //ip验证
        valiSign(orderInfo);   //      签名验证
        valiMob(orderInfo);  //手机号码验证
    }

    /**
     * 验证签名
     * @param orderInfo
     * @throws Exception
     */
    private void valiSign(OrderInfo orderInfo)throws Exception{
        String pwd = channelDao.queryChannel(orderInfo.getChannel()).getPassword();
        String signature1 = MD5.MD5Encode(orderInfo.getUserid() + orderInfo.getChannel() + orderInfo.getTimestamp() + pwd);
        System.out.println("signature1----"+signature1);
        if(!signature1.equals(orderInfo.getSignature())) {
            throw new BussinessException("10006", "签名校验失败");
        }
    }

    /**
     * 验证手机号码
     * @param orderInfo
     * @throws Exception
     */
    private void valiMob(OrderInfo orderInfo)throws Exception{
        String userid = orderInfo.getUserid();
        if(StringUtils.isEmpty(userid)) {
            throw new BussinessException("10007", "手机号不能为空");
        }
        ChannelBean channelBean = channelDao.queryChannel(orderInfo.getChannel());
        try {
            userid = DesUtil.decode(userid, channelBean.getPassword());
//            userid = AESUtil.decrypt(orderInfo.getUserid(),channelBean.getPassword());
            if(isValid(userid) && userid.length() == 11){
                orderInfo.setUserid(userid);
            }else{
                throw new BussinessException("10009", "手机号错误");
            }
        } catch (Exception e) {
            if(e instanceof BussinessException) {
                throw e;
            }else {
                throw new BussinessException("10008", "手机号加密错误");
            }
        }
    }

    /**
     * 参数验证
     * @param orderInfo
     * @throws Exception
     */
    private void paramVali(OrderInfo orderInfo)throws Exception {
        if (StringUtils.isEmpty(orderInfo.getChannel())
                || StringUtils.isEmpty(orderInfo.getTimestamp())
                || StringUtils.isEmpty(orderInfo.getUserid())
                || StringUtils.isEmpty(orderInfo.getSignature())
                || StringUtils.isEmpty(orderInfo.getNeedRoute())) {
            throw new BussinessException("80015", "参数不合法");
        }
    }



    private boolean isValid(String usermob){
        if (usermob == null) {
            return false;
        }
        usermob = usermob.replaceAll("[\\s-]", "");
        return usermob.matches(".*(1\\d{10}).*");
    }
}
