package com.tax.token.utils;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.tax.token.constants.TokenConstants;
import com.tax.token.model.TkToken;
import com.tax.token.model.TkZoneInfo;
import com.yxcheng.token.utils.HttpHelper;
import com.yxcheng.token.utils.JSONHelper;
import com.yxcheng.token.utils.MD5Util;
import com.yxcheng.token.utils.ResponseContent;
import org.apache.http.HttpException;
import org.apache.log4j.Logger;


public class TokenGetUtil {
	
	private static final Logger logger = Logger.getLogger(TokenGetUtil.class);

	public static String url = TokenConstants.TOKEN_CHECK_RUNGETTOKEN;
	public static String username = TokenConstants.TOKEN_CHECK_USERNAME;
	public static String password = TokenConstants.TOKEN_CHECK_PASSWORD;
	
	public static String tokenFrom = TokenConstants.TOKEN_FROM;

    public static String getToken(TkToken tokenEntity, TkZoneInfo zoneInfo) {
        return getToken(tokenEntity.getTaxno(),zoneInfo.getZoneCode());
    }


        /**
        * @描述:获取token公共类
        * @作者：曹小红
        * @日期：2017年2月8日 上午10:13:05
        * @param
        * @return
        * @return String
        */
	@SuppressWarnings("unchecked")
	public static String getToken(String taxno, String zoneCode) {
		
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		String tokenCode = "";
		
		String dqbm = zoneCode;
		try {
			ResponseContent ret = null;
			if("0".equals(tokenFrom)) {
				String force = "true";
				paramsMap.put("sh", taxno);
				paramsMap.put("dqbm", dqbm);
				paramsMap.put("force", force);
				paramsMap.put("username", username);
				paramsMap.put("sign", MD5Util.MD5Encode(taxno + dqbm + force + username + password));
				ret = HttpHelper.postEntity(url, paramsMap);
			} else if("1".equals(tokenFrom)){
//				String ymbb = zoneInfo.getYmbb();
//				String timeStamp = String.valueOf(new Date().getTime());
//				String secert = tokenEntity.getSecert();
//
//				paramsMap.put("dqdm", dqbm);
//				paramsMap.put("cert", taxno);
//				paramsMap.put("ymbb", ymbb);
//				paramsMap.put("index", keyCode);
//				paramsMap.put("time", timeStamp);
//				paramsMap.put("user", username);
//				paramsMap.put("secert", secert);
//				sign = MD5Util.MD5Encode(dqbm + taxno + ymbb + keyCode + timeStamp + username + password);
//				ret = HttpHelper.getUrlRespContent(url + "?dqdm="+dqbm+ "&cert=" + taxno +"&ymbb="+ymbb +
//						"&index=" + keyCode + "&time=" +timeStamp+"&user=" + username + "&sign=" +sign+ "&secert=" +secert);
			}else if ("2".equals(tokenFrom)) {

                ret = HttpHelper
                        .getUrlRespContent(url + "?zoneCode=" + dqbm + "&taxno=" + taxno + "&userCode=" + username);
                logger.info("runGetToken " + taxno + " result info is : " + ret.getContent() + ", tokenFrom is : "
                        + tokenFrom);

                String retStr = ret.getContent();
                Map<String, Object> retMap = JSONHelper.parserToMap(retStr);
                String retCode = String.valueOf(retMap.get("errorCode"));

                if (retCode.equals("0") && null != retMap.get("data")) {
                    tokenCode = String.valueOf(retMap.get("data"));
                }
            } else if ("4".equals(tokenFrom)) {
                String url = "http://172.20.15.153:8181/token/token?dqdm=1100&ymbb=3.1.01&cert=12344&index=1&user=dx&time=20170323&sign=4db3bc9708b1bafcacbabada6d913008&serNo=T00000";
                ret = HttpHelper
                        .getUrlRespContent(url);
                logger.info("runGetToken " + taxno + " result info is : " + ret.getContent() + ", tokenFrom is : "
                        + tokenFrom);

                String retStr = ret.getContent();
                Map<String, Object> retMap = JSONHelper.parserToMap(retStr);
                String retCode = String.valueOf(retMap.get("code"));

                if (retCode.equals("0") && null != retMap.get("token")) {
                    tokenCode = String.valueOf(retMap.get("token"));
                }
            }
			logger.info("runGetToken " +taxno+ " result info is : " + ret.getContent() + ", tokenFrom is : " + tokenFrom);
			String retStr = ret.getContent();
			Map<String, Object> retMap = JSONHelper.parserToMap(retStr);
			String retCode = String.valueOf(retMap.get("code"));
			
			if(retCode.equals("0") && null != retMap.get("token")) {
				tokenCode = String.valueOf(retMap.get("token"));
			} else if(retCode.equals("4")) {
				//还在获取中什么也不做
			} else {
				//tokenEt.setStatus("3");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tokenCode;
	}


	public static String getTestToken(){
        String url = "http://88.taxservices.cn/token/token?dqdm=1100&ymbb=3.1.01&cert=12344&index=1&user=dx&time=20170323&sign=4db3bc9708b1bafcacbabada6d913008&serNo=T00000";
        ResponseContent ret = null;
        String tokenCode = null;
        try {
            ret = HttpHelper
                    .getUrlRespContent(url);

            String retStr = ret.getContent();
            Map<String, Object> retMap = JSONHelper.parserToMap(retStr);
            String retCode = String.valueOf(retMap.get("code"));

            if (retCode.equals("0") && null != retMap.get("token")) {
                tokenCode = String.valueOf(retMap.get("token"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (HttpException e) {
            e.printStackTrace();
        }
        return tokenCode;
    }
	
}
