package jj.tech.paolu.utils.wb;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.fluent.Request;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class WBHttp {
	
	//从第一条开始迭代获取分类目录
	public static String sortSub_url = "http://api.weepal.cn/EAMS-XAPI/api/v1/sztd/getSortSub";
	public static String getSortTree_url = "http://api.weepal.cn/EAMS-XAPI/api/v1/sztd/getSortTree";
	
	
	//
	
	public static String getAnnex_url = "http://api.weepal.cn/EAMS-XAPI/api/v1/sztd/getAnnex";
	public static String ss;
	public static String sss;
	public static String appKey= "SZTD00898";
	public static String appSecret= "16E58F6750C246818FBD84EAEF3FC998";
	
	public static ObjectMapper objectMapper = new ObjectMapper();
	
	public static List<String> getSortSub(String kid){
		try {
			List<String> retrunList = new ArrayList<String>();
			
			String timestamp = String.valueOf(System.currentTimeMillis());
			String url = sortSub_url 
					+ "?appKey="+ appKey 
					+ "&timestamp=" + timestamp
					+ "&sign=" + DigestUtils.md5Hex(appKey+timestamp+appSecret)
					;
			if(StringUtils.isNotBlank(kid)) {
				url = url + "&parentID=" + kid;
			}
			System.out.println(url);
			InputStream stean =
				Request.Get(url)
				.connectTimeout(15000)
				.socketTimeout(15000)
				.execute()
				.returnContent()
				.asStream();
			JsonNode root = objectMapper.readTree(stean);
			JsonNode data = root.get("data");
			//ArrayNode datasetArray = (ArrayNode)root.get("data");
			for (final JsonNode item : data) {
		        System.out.println(item);
		        String kidStr = item.asText("kid");
		        retrunList.add(kidStr);
		    }
			return retrunList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	public static Integer getSortTree(Integer pageNumber){
		if(pageNumber<1) {
			return 0;
		}
		try {
			List<String> dataList = new ArrayList<String>();
			
			String timestamp = String.valueOf(System.currentTimeMillis());
			String url = getSortTree_url 
					+ "?appKey="+ appKey 
					+ "&timestamp=" + timestamp
					+ "&sign=" + DigestUtils.md5Hex(appKey+timestamp+appSecret)
					+ "&pageNumber=" + pageNumber
					;
			
			System.out.println(url);
			InputStream stean =
				Request.Get(url)
				.connectTimeout(15000)
				.socketTimeout(15000)
				.execute()
				.returnContent()
				.asStream();
			
			JsonNode root = objectMapper.readTree(stean);
			JsonNode data = root.get("data");
			//ArrayNode datasetArray = (ArrayNode)root.get("data");
			for (final JsonNode item : data) {
		        System.out.println(item);
		        
//		        String kidStr = item.asText("kid");
		    }
			
			return pageNumber+1;
			
		
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		
	}
	
	
	public String getAnnexUrl(){
		return null;
	}
	
	public String getCatalogUrl(){
		return null;
	}
	
	public static void main(String[] args) {
		getSortSub("D0B3E995-9B56-4933-8C51-ACDC1D8F8CB2");
	}
	
}
