package cn.net.cpzslibs.prot.handset;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.SocketTimeoutException;

import cn.net.cpzslibs.prot.ProtBase;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.Prot24TrayBoxBean;

public class Prot24TrayBox extends ProtBase {
	private final short iTaskCode24 = 24;
	private final short iStateOut = 0;// 0 已出托盘
	private final short iStateUnOut = 1;// 1未出托盘
	private final short iStateAll = 2;// 2所有箱
	private String errMsg;
	/**
	 * 没有要获取的箱
	 */
	private final int NoBox = 0xFFD8;
	/**
	 * 没有要获取的托盘
	 */
	private final int NoTray = 0xFFD7;
	/**
	 * json数据
	 */
	private String jsonData = null;

	/**
	 * 未出库托盘
	 * 
	 * @author zwj<br/>
	 *         2015-5-3 下午9:50:54<br/>
	 * @param create
	 * @param iBoxId
	 *            箱id
	 * @throws SocketTimeoutException
	 * @throws IOException
	 */
	public void dealUnOut(SocketCreate create, long iBoxId)
			throws SocketTimeoutException, IOException {
//		recProt(create.getDos(), create.getDis(), iTaskCode24, iBoxId,
//				iStateUnOut);
		dealProt(create, iTaskCode24, iBoxId, iStateUnOut);
	}

	/**
	 * 已出托盘
	 * 
	 * @author zwj<br/>
	 *         2015-5-3 下午9:50:54<br/>
	 * @param create
	 * @param iBoxId
	 *            箱id
	 * @throws SocketTimeoutException
	 * @throws IOException
	 */
	public void dealOut(SocketCreate create, long iBoxId)
			throws SocketTimeoutException, IOException {
//		recProt(create.getDos(), create.getDis(), iTaskCode24, iBoxId,
//				iStateOut);
		dealProt(create, iTaskCode24, iBoxId, iStateOut);
	}

	/**
	 * 托盘所有数据
	 * 
	 * @author zwj<br/>
	 *         2015-5-3 下午9:50:54<br/>
	 * @param create
	 * @param iBoxId
	 *            箱id
	 * @throws SocketTimeoutException
	 * @throws IOException
	 */
	public void dealAll(SocketCreate create, long iBoxId)
			throws SocketTimeoutException, IOException {
		
//		recProt(create.getDos(), create.getDis(), iTaskCode24, iBoxId,
//				iStateAll);
		dealProt(create, iTaskCode24, iBoxId, iStateAll);
	}

	private void dealProt(SocketCreate create, short iTaskCode, long iBoxId,
                          short iState) throws SocketTimeoutException, IOException {
		this.iSendTaskIndex = create.getiTaskIndex();
		recProt(create.getDos(), create.getDis(), iTaskCode, iBoxId, iState);
	}

	private void recProt(DataOutputStream dos, DataInputStream dis,
			short iTaskCode, long iBoxId, short iState)
			throws SocketTimeoutException, IOException {
		sendProt(dos, iTaskCode, iBoxId, iState);
		recHeader(dis);
		if (isTaskCode(iTaskCode)) {
			setSuccess(true);
			short iJsonSize = dis.readShort();
			jsonData = new String(recString(dis, iJsonSize), "utf-8");
			recCheckCode(dis);
			setSuccess(true);
		} else {
			setSuccess(false);
			recFFFF(dis, iTaskCode);
			if (getiRecErrcode() == NoBox) {
				errMsg = "没有出库箱信息";
			} else if (getiRecErrcode() == NoTray) {
				errMsg = "无法获取托盘信息";
			} else {
				errMsg = "异常(" + getiRecErrcode() + ")";
			}
		}
	}

	/**
	 * 
	 * @author zwj<br/>
	 *         2015-5-3 下午9:51:59<br/>
	 * @param dos
	 * @param iTaskCode
	 * @param iBoxId
	 *            箱号
	 * @param iState
	 *            状态
	 * @throws SocketTimeoutException
	 * @throws IOException
	 */
	private void sendProt(DataOutputStream dos, short iTaskCode, long iBoxId,
			short iState) throws SocketTimeoutException, IOException {
		short iBodySize = countBodySize(iBoxId, iState);
		sendHeader(dos, iTaskCode, iBodySize);
		sendiLabel(dos, iBoxId);
		dos.writeShort(iState);
		sendCheckCode(dos);
	}

	/**
	 * json数据
	 * 
	 * @author zwj<br/>
	 *         2015-5-3 下午9:45:59<br/>
	 * @return
	 */
	public String getJsonData() {
		return jsonData;
	}

	/**
	 * 获取到数据 <br/>
	 * {boxID=2, state=0} <br/>
	 * 2015-5-7下午2:10:50
	 * 
	 * @return
	 */
	public Prot24TrayBoxBean getJsonBean() {
		Gson gson = new Gson();
		return gson.fromJson(jsonData, new TypeToken<Prot24TrayBoxBean>() {
		}.getType());
	}

	/**
	 * 错误信息 <br/>
	 * 2015-5-8下午1:35:32
	 * 
	 * @return
	 */
	public String getErrMsg() {
		return errMsg;
	}

}
