package com.yihu.jw.wlyy.wlyyhttp;

import com.alibaba.fastjson.JSONObject;
import com.yihu.jw.entity.hospital.consult.WlyyHospitalSysDictDO;
import com.yihu.jw.entity.wlyyinfo.OauthWlyyConfigDO;
import com.yihu.jw.hospital.dict.WlyyHospitalSysDictDao;
import com.yihu.jw.util.http.HttpClientUtil;
import com.yihu.jw.wlyy.dao.OauthWlyyConfigDao;
import org.apache.commons.collections.map.HashedMap;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Trick on 2019/8/19.
 */
@Service
@Transactional
public class WlyyHttpService {

    private static final Logger logger = LoggerFactory.getLogger(WlyyHttpService.class);

    @Autowired
    private OauthWlyyConfigDao oauthWlyyConfigDao;

    @Autowired
    private HttpClientUtil httpClientUtil;
    @Autowired
    private WlyyHospitalSysDictDao wlyyHospitalSysDictDao;

    /**
     * @param configId 配置ID
     * @param param key为param,的参数
     * @param mapParams map形式的传参
     * @return
     */
    public JSONObject sendWlyyMes(String configId,JSONObject param,Map<String,String> mapParams) {

        try{

            OauthWlyyConfigDO oauthWlyyConfigDO = oauthWlyyConfigDao.findOne(configId);
            //token获取accesstoken
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("appid", oauthWlyyConfigDO.getAppId()));
            params.add(new BasicNameValuePair("appSecret", oauthWlyyConfigDO.getAppSecret()));
            String res = httpClientUtil.post(oauthWlyyConfigDO.getTokenUrl(), params, "UTF-8");
            String token = null;
            JSONObject rsjson = JSONObject.parseObject(res);
            logger.info("sendWlyyMes token ：" + rsjson.toString());
            Integer status = rsjson.getInteger("status");

            if (status == 10000) {
                //设置入参
                List<NameValuePair> p = new ArrayList<>();

                //组装key为param的入参
                if(param != null){
                    p.add(new BasicNameValuePair("param", param.toJSONString()));
                }


                //组装map形式的传参
                if(mapParams!= null && !mapParams.keySet().isEmpty()){
                    for (String paramkey:mapParams.keySet()){
                        p.add(new BasicNameValuePair(paramkey, mapParams.get(paramkey)));
                    }
                }


                //设置头部
                token = rsjson.getJSONObject("result").getString("accesstoken");
                Map<String,Object> headerMap = new HashedMap();
                headerMap.put("accesstoken",token);
                logger.info("sendWlyyMes url ："+oauthWlyyConfigDO.getUrl());
                String rs = httpClientUtil.headerPost(oauthWlyyConfigDO.getUrl(),p,"UTF-8",headerMap);
                logger.info("sendWlyyMes headerPost ："+rs);

                JSONObject re = JSONObject.parseObject(rs);
                Integer s = re.getInteger("status");

                if(s == 200){
                    return re;
                }else{
                    return null;
                }
            }else{
                return null;
            }
        }catch (Exception e){
            logger.error("sendWlyyMes error:"+e.toString());
        }
        return null;
    }
    /**
     * @param configId 配置ID
     * @param param key为param,的参数
     * @return
     */
    public JSONObject sendWlyyMesGet(String configId,String param) {

        try{

            OauthWlyyConfigDO oauthWlyyConfigDO = oauthWlyyConfigDao.findOne(configId);
            //token获取accesstoken
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("appid", oauthWlyyConfigDO.getAppId()));
            params.add(new BasicNameValuePair("appSecret", oauthWlyyConfigDO.getAppSecret()));
            String res = httpClientUtil.post(oauthWlyyConfigDO.getTokenUrl(), params, "UTF-8");
            String token = null;
            JSONObject rsjson = JSONObject.parseObject(res);
            logger.info("sendWlyyMes token ：" + rsjson.toString());
            Integer status = rsjson.getInteger("status");

            if (status == 10000) {


                //设置头部
                token = rsjson.getJSONObject("result").getString("accesstoken");
                Map<String,Object> headerMap = new HashedMap();
                headerMap.put("accesstoken",token);
                logger.info("sendWlyyMes url ："+oauthWlyyConfigDO.getUrl()+param);
                String rs = httpClientUtil.get(oauthWlyyConfigDO.getUrl()+param,"utf-8",headerMap);

                JSONObject re = JSONObject.parseObject(rs);
                Integer s = re.getInteger("status");

                if(s == 200){
                    return re;
                }else{
                    return null;
                }
            }else{
                return null;
            }
        }catch (Exception e){
            logger.error("sendWlyyMes error:"+e.toString());
        }
        return null;
    }
    public  JSONObject testgeet(String ip){
        String gt="d99d7ba0ce0ad944d02c11e2e00b184f";
        String url = "http://bypass.geetest.com/v1/bypass_status.php";
        String register="http://localhost:8088/register";
        JSONObject result = new JSONObject();
        List<WlyyHospitalSysDictDO> list = wlyyHospitalSysDictDao.findByDictName("geetConfig");
        for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:list){
            if ("type".equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                result.put("type",wlyyHospitalSysDictDO.getDictValue());
            }
            if ("register".equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                register = wlyyHospitalSysDictDO.getDictValue();
            }
            if ("bypass".equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                url = wlyyHospitalSysDictDO.getDictValue();
            }
            if ("gt".equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                gt = wlyyHospitalSysDictDO.getDictValue();
            }
        }
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("gt",gt));
        String res = httpClientUtil.post(url, params, "UTF-8");
        JSONObject statusObject = JSONObject.parseObject(res);
        if (statusObject!=null){
            result.put("offline",statusObject.getString("status"));
        }
        System.out.println(res);

        Long t= new Date().getTime();
        String registerRes= httpClientUtil.get(register+"?t="+t,"UTF-8");
        System.out.println(registerRes);
        JSONObject registerObject= JSONObject.parseObject(registerRes);
        if (registerObject!=null){
            result.put("challenge",registerObject.getString("challenge"));
            result.put("newCaptcha",registerObject.getString("new_captcha"));
        }
        result.put("gt",gt);

        System.out.println(result.toJSONString());
        return result;
    }

}
