/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  net.sf.json.JSONObject
 *  org.apache.commons.lang.StringUtils
 */
package com.hst.taxService.util;

import com.hst.taxService.util.AESUtil;
import com.hst.taxService.util.HttpUtils;
import com.hst.taxService.util.NuoNAccessToken;
import com.hst.taxService.util.OpenApiV1;
import com.hst.taxService.util.OpensnsException;
import com.hst.taxService.util.PrivateData;
import com.hst.taxService.util.PublicData;
import com.hst.taxService.util.RequestMode;
import com.hst.taxService.util.SecurityUtil;
import com.hst.taxService.util.ValidataUtil;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

public class NuonuoUtils {
    public static final String AUTHOR_URL = "http://dev.jss.com.cn/authorize";
    public static final String ACCESS_TOKEN = "http://dev.jss.com.cn/accessToken";
    public static final String APP_KEY = "mIPA6SC3";
    public static final String APP_SECRECT = "627FAC5875AA4D00";
    public static final String REVOKE_URL = "http://iteeio.zust.edu.cn/guoshui/index!dealNuoNuo.htm";
    public static NuoNAccessToken TOKEN = null;

    public static void loginNuoNuo() {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("client_id", APP_SECRECT);
        params.put("redirect_uri", REVOKE_URL);
        params.put("appKey", APP_KEY);
        params.put("response_type", "code");
        HttpUtils.doGet(AUTHOR_URL, params, "utf-8", false);
    }

    public static NuoNAccessToken getAccessToken(String code) {
        NuoNAccessToken token = new NuoNAccessToken();
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("code", code);
        params.put("client_id", APP_SECRECT);
        params.put("client_secret", APP_SECRECT);
        params.put("redirect_uri", REVOKE_URL);
        params.put("appKey", APP_KEY);
        params.put("grant_type", "authorization_code");
        String result = HttpUtils.doPost(ACCESS_TOKEN, params, "utf-8", false);
        if (!StringUtils.isEmpty((String)result)) {
            JSONObject json = JSONObject.fromObject((Object)result);
            if (json.has("expires_in") && json.get("expires_in") != null) {
                token.setExpires_in(json.get("expires_in").toString());
            }
            if (json.has("access_token") && json.get("access_token") != null) {
                token.setAccess_token(json.get("access_token").toString());
            }
            if (json.has("oauthUser") && json.get("oauthUser") != null) {
                token.setOauthUser(json.get("oauthUser").toString());
            }
            return token;
        }
        return null;
    }

    private static Map<String, String> getHeaders() {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("appKey", APP_KEY);
        headers.put("accessToken", TOKEN.getAccess_token());
        headers.put("compress", "GZIP");
        headers.put("signMethod", "AES");
        headers.put("dataType", "JSON");
        headers.put("appRate", SecurityUtil.app_rate);
        return headers;
    }

    private static PublicData getPublicData(String apiName) {
        PublicData pdData = new PublicData();
        pdData.setVersion("1.0");
        pdData.setTimestamp(String.valueOf(System.currentTimeMillis()));
        pdData.setMethod(apiName);
        return pdData;
    }

    private static RequestMode getRequestMode(PublicData pdata, PrivateData<Object> pvData) {
        RequestMode requestMode = new RequestMode();
        requestMode.setPrivate(pvData);
        requestMode.setPublic(pdata);
        return requestMode;
    }

    public static String getCompanyInfo(String swbh) {
        Map<String, String> headers = NuonuoUtils.getHeaders();
        PublicData pdData = NuonuoUtils.getPublicData("nuonuo.speedBilling.billingByDuty");
        PrivateData<Object> pvData = new PrivateData<Object>();
        ArrayList list = new ArrayList();
        HashMap<String, String> map = new HashMap<String, String>();
        String randomSixNum = AESUtil.getRandomCharacter(6);
        map.put("signid", SecurityUtil.MD5Encrypt(randomSixNum));
        map.put("taxnum", swbh);
        map.put("validator", randomSixNum);
        list.add(map);
        pvData.setServicedata(list);
        RequestMode requestMode = NuonuoUtils.getRequestMode(pdData, pvData);
        OpenApiV1 sdk = new OpenApiV1();
        String result = "";
        try {
            result = sdk.handle(SecurityUtil.url, headers, requestMode);
            ValidataUtil.isEmpty(result);
            return URLDecoder.decode(result, "UTF-8");
        }
        catch (OpensnsException e) {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
}
