package cn.net.cpzslibs.prot;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.net.cpzslibs.prot.utils.JsonToMap;

public class Prot09UpDown extends ProtBase {

	protected final short iTaskCode9 = 9;
	protected final short iTaskCode901 = 901;
	protected final short typeUP = 1;// 取父标签
	protected final short typeDOWN = 2;// 取子标签
	public static final String kLabelID = "labelID";
	public static final String kName = "name";
	public static final String kProductID = "productID";
	private List<Long> list9;
	private List<Map<String, String>> list901;
	private String jsonData;

	/**
	 * 普通模式
	 * 
	 * @param socketCreate
	 * @param shType
	 * @throws IOException
	 */
	public void deal9UpDown(SocketCreate socketCreate, short shType)
			throws IOException {
		this.iSendTaskIndex = socketCreate.getiTaskIndex();
		recProt(socketCreate.getDis(), socketCreate.getDos(), iTaskCode9,
				socketCreate.getiLabel(), shType);
	}
	
	public void deal9UpDown(SocketCreate socketCreate, long labelId, short shType)
			throws IOException {
		this.iSendTaskIndex = socketCreate.getiTaskIndex();
		recProt(socketCreate.getDis(), socketCreate.getDos(), iTaskCode9,
				labelId, shType);
	}
	
	/**
	 * 普通模式<br/>
	 * 获取上级
	 * 
	 * @param socketCreate
	 * @throws IOException
	 */
	public void deal9Up(SocketCreate socketCreate) throws IOException {
		this.iSendTaskIndex = socketCreate.getiTaskIndex();
		recProt(socketCreate.getDis(), socketCreate.getDos(), iTaskCode9,
				socketCreate.getiLabel(), typeUP);
	}

	/**
	 * 普通模式<br/>
	 * 获取下级
	 * 
	 * @param socketCreate
	 * @throws IOException
	 */
	public void deal9Down(SocketCreate socketCreate) throws IOException {
		this.iSendTaskIndex = socketCreate.getiTaskIndex();
		recProt(socketCreate.getDis(), socketCreate.getDos(), iTaskCode9,
				socketCreate.getiLabel(), typeUP);
	}

	/**
	 * 汽配，只是查询下级标签
	 * 
	 * @param socketCreate
	 * @throws IOException
	 */
	public void deal901UpDown(SocketCreate socketCreate) throws IOException {
		this.iSendTaskIndex = socketCreate.getiTaskIndex();
		short shType = typeDOWN;// 云南汽配直接查下级
		recProt(socketCreate.getDis(), socketCreate.getDos(), iTaskCode901,
				socketCreate.getiLabel(), shType);
	}

	/**
	 * 9号协议数据
	 * 
	 * @return
	 */
	public List<Long> getList9() {
		return list9;
	}

	/**
	 * 901号数据
	 * 
	 * @return
	 */
	public List<Map<String, String>> getList901() {
		list901.addAll(parseJson(jsonData));
		return list901;
	}

	/**
	 * 发送协议：获取包和子的协议9和901通用头文件
	 * <p>
	 * 2014-10-30上午10:38:00
	 * </p>
	 * 
	 * @param dos
	 * @param iTaskCode
	 * @param iLabel
	 * @param iType
	 * @throws IOException
	 */
	protected void sendProt(DataOutputStream dos, short iTaskCode, long iLabel,
			short iType) throws IOException {
		short iBodySize = countBodySize(iLabel, iType);
		sendHeader(dos, iTaskCode, iBodySize);
		sendiLabel(dos, iLabel);
		dos.writeShort(iType);
		sendCheckCode(dos);
	}

	/**
	 * <br/>
	 * 2015-4-1下午4:56:55
	 * 
	 * @param dis
	 * @param dos
	 * @param iTaskCode
	 * @param label
	 * @param shType
	 * @throws IOException
	 * @throws JSONException
	 */
	protected void recProt(DataInputStream dis, DataOutputStream dos,
			short iTaskCode, long label, short shType) throws IOException {
		sendProt(dos, iTaskCode, label, shType);
		if (iTaskCode == iTaskCode9) {
			list9 = new ArrayList<Long>();
		} else if (iTaskCode == iTaskCode901) {
			list901 = new ArrayList<Map<String, String>>();
		}
		do {
			recHeader(dis);
			debugLog(iTaskCode, toStringRecHeader());
			if (isTaskCode(iTaskCode9)) {
				// 普通模式
				int size = (getiRecBodySize() - 2) / 8;
				for (int i = 0; i < size; i++) {
					long label_16 = reciLabel(dis);
					if (label_16 == 0) {
						list9.add(0L);
					} else {
						list9.add(label_16);
					}
				}
				recCheckCode(dis);
				if (getiRecTaskSubIndex() <= 0) {
					// 证明里面没有数据
					break;
				}
			} else if (isTaskCode(iTaskCode901)) {
				// 汽配模式
				int bodySize = getiRecBodySize() - 2;
				if (bodySize <= 0) {
					setSuccess(false);
					recCheckCode(dis);
					break;
				} else {
					byte[] jsons = recString(dis, bodySize);
					recCheckCode(dis);
					jsonData = new String(jsons, "utf-8");
					if (getiRecTaskSubIndex() <= 0) {
						break;
					}
				}

			} else {
				recFFFF(dis, iTaskCode);
				break;
			}

			try {
				// 休息1ms
				Thread.sleep(5);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} while (getiRecTaskSubIndex() > 0);

		if (iTaskCode == iTaskCode901) {
			// 901不需要走这里
			if (list901.size() > 0) {
				setSuccess(true);
			} else {
				setSuccess(false);
			}
		} else if (iTaskCode == iTaskCode9) {
			if (list9.size() != 0 && list9.get(0) != 0) {
				// 有数据,并且第0个数据不为0
				setSuccess(true);
			} else {
				// 没数据
				setSuccess(false);
			}
		}

	}

	/**
	 * Json解析数据 JSon格式(6)
	 * JSon格式[{"labelID":"XXX","name":"XXX",”productID”:”XXX”} …]
	 * <p>
	 * 2014-10-30上午11:33:17
	 * </p>
	 * 
	 * @param jsonStr
	 * @throws JSONException
	 */
	private List<Map<String, String>> parseJson(String jsonData) {
		// List<Map<String, String>> list = new ArrayList<Map<String,
		// String>>();
		// JSONArray arr;
		// arr = new JSONArray(jsonData);
		// for (int i = 0; i < arr.length(); i++) {
		// JSONObject temp = (JSONObject) arr.get(i);
		// Map<String, String> map = new HashMap<String, String>();
		// map.put(kLabelID, temp.getString(kLabelID));
		// map.put(kName, temp.getString(kName));
		// map.put(kProductID, temp.getString(kProductID));
		// list.add(map);
		// }
		return JsonToMap.toListMap(jsonData);
	}

	/**
	 * 901json数据 <br/>
	 * @return
	 */
	public String getJsonData() {
		return jsonData;
	}

}
