package com.chinacreator.video.restful.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.DictionaryDao;
import com.chinacreator.video.restful.dao.WoplusReqDao;
import com.chinacreator.video.service.CommonService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@Service
public class WoplusReqService {

    @Autowired
    private WoplusReqDao woplusReqDao;
    private Logger logger=Logger.getLogger("woplusReq");
    @Autowired
    private DictionaryDao dictionaryDao;

    public void insert(Map<String, Object> params) throws Exception {
        List<String> directory=null;
        try {
            directory = (List<String>) params.get("directory");
            if (directory==null||directory.size()==0){
                throw new BussinessException("9001","{directory}参数不能为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BussinessException("9001","{directory}参数传入错误");
    }
        String woplusReqIp = dictionaryDao.getValue("woplusReqIp");
        if (StringUtils.isEmpty(woplusReqIp)){
            throw new BussinessException("9002","{woplusReqIp}未配置");
        }
        String[] ips = woplusReqIp.split(",");
        for (int i = 0; i < ips.length; i++) {
                String ip=ips[i];
                woplusReqDao.insert(directory,ip);
        }
    }



    @Autowired
    private ChannelValidate channelValidate;

    @Autowired
    private IpValidate ipValidate;

    @Autowired
    private TimetampValidate timetampValidate;

    @Autowired
    private ChannelDao channelDao;

    @Autowired
    private CommonService commonService;

    /***
     * 校验
     * @throws Exception
     */
    public void busiCheck(Map<String, Object> params) throws Exception {
        OrderInfo orderInfo=new OrderInfo();
        orderInfo.setTimestamp(String.valueOf(params.get("timestamp")));
        orderInfo.setChannel(String.valueOf(params.get("channel")));
        orderInfo.setSignature(String.valueOf(params.get("signature")));
        orderInfo.setSpid(String.valueOf(params.get("directory")));
        orderInfo.setInvokeface("woplusReq");
        orderInfo.setCpid("woplusreq");
        //timetamp验证
        timetampValidate.validate(orderInfo.getTimestamp());
        //渠道验证
        channelValidate.validate(orderInfo.getChannel());
        //ip验证
        ipValidate.volidate(orderInfo,"0");
        //签名验证
        validate(orderInfo);
    }


    public void validate(OrderInfo orderInfo) throws Exception {
        String lcalsign = getSign(orderInfo);
        lcalsign += channelDao.queryChannel(orderInfo.getChannel()).getPassword();
        lcalsign = MD5.MD5Encode(lcalsign);
        logger.info("woplus本地签名=========="+lcalsign+"====woplusReq请求签名======"+orderInfo.getSignature());
        if(!lcalsign.equals(orderInfo.getSignature())) {
            throw new BussinessException("10006", "签名校验失败");
        }
    }
    /**
     * 拼接签名信息 cpid+spid+userid+type+channel+timestamp
     * @param orderInfo
     * @return
     */
    private String getSign(OrderInfo orderInfo) {
        if (orderInfo == null) {
            return "";
        }
        StringBuilder strb = new StringBuilder();
        if(orderInfo.getOrderid() != null && !"".equals(orderInfo.getOrderid())){
            strb.append(orderInfo.getOrderid());
        }
        strb.append(orderInfo.getSpid());
        strb.append(orderInfo.getChannel());
        strb.append(orderInfo.getTimestamp());
        return strb.toString();
    }

}
