package com.seeyon.apps.esign.utils;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.aicloud.common.JsonUtils;
import com.seeyon.apps.esign.config.EsignConfigProvider;
import com.seeyon.apps.esign.constants.EsignConfigConstants;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import www.seeyon.com.utils.StringUtil;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;


public class ProtUtil {

    private static final Log log = LogFactory.getLog(ProtUtil.class);
    private EsignConfigProvider configProvider = EsignConfigProvider.getInstance();

    /**
     * 获取一个token
     *
     * @return
     * @throws IOException
     * @throws FileNotFoundException
     */
    private String getToken(String oatokenurl,String restName,String restPassword,String loginName) throws FileNotFoundException, IOException {
        String address = oatokenurl + restName + "/" + restPassword;
        if(StringUtil.isNotEmpty(loginName)){
            address = address +"?loginName="+loginName;
        }
        DefaultHttpClient client = new DefaultHttpClient();
        String result = "";
        HttpGet get = new HttpGet(address);
        // 添加 Headers 信息
        get.addHeader(new BasicHeader("Accept", "application/json"));
        try {
            HttpResponse res = client.execute(get);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(res.getEntity());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        String token = "";
        if(result.contains("{")) {
            JSONObject jsObj = JSONObject.parseObject(result);
            token = jsObj.get("id").toString();
        }else {
            token = result;
        }
        return token;
    }


    /**
     * 调用post接口
     * @return
     */
    public String  sendPostNotification(String params, String urlParam, String token) throws IOException {
        String host = configProvider.getBizConfigByKey(EsignConfigConstants.OA_HOST);
        String tokenGetUrl = configProvider.getBizConfigByKey(EsignConfigConstants.getTokenUrl);
        String restName = configProvider.getBizConfigByKey(EsignConfigConstants.restName);
        String restPwd = configProvider.getBizConfigByKey(EsignConfigConstants.restPwd);
        String oatoken = getToken(host + tokenGetUrl,restName,restPwd,null);
        String url = host + urlParam+ token+"?token="+oatoken;
        log.info("调用恢复超级节点接口请求地址为: " + url);
        String respStr = HttpClient.httpPostRaw(url, params, null, null);
        log.info("调用恢复超级节点接口响应结果: " + respStr);
        if(!"1".equals(respStr)) {
            Map map = JsonUtils.parseObject(respStr, Map.class);
            throw new RuntimeException("恢复阻塞的超级节点失败: " + map.get("message"));
        }
        return respStr;
    }

}
