package cn.net.cpzslibs.prot.handset;

import com.google.gson.Gson;

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.bean.Prot36CarInfoBean;

/**
 * 车牌号验证，获取对应的车架号和发动机号
 * 
 * @author zwj
 * 
 * @date 2015-12-29 下午3:52:34
 */
public class Prot36CarGetInfo extends ProtBase {
	private final short iTaskCode36 = 36;
	private String rJson;

	private final int noCarNoErrCode = 0xFFB6;
	public void dealProt(SocketCreate socketCreate, String car_no) throws IOException {
		String sJson = "{\"car_no\": \"" + car_no + "\"}";
		sendProt(socketCreate.getDos(), iTaskCode36, sJson);
		recProt(socketCreate.getDis(), iTaskCode36);
	}

	/**
	 * 是否没有车辆信息的错误码
	 * @date 2015-12-30上午10:26:07
	 * @return
	 */
	public boolean isNoCarNoErrCode() {
		return this.noCarNoErrCode == getiRecErrcode();
	}
	/**
	 * 没数据为null
	 * 
	 * @date 2015-12-29下午3:32:41
	 * @return
	 */
	public String getJson() {
		return rJson;
	}

	/**
	 * 没数据为null
	 * 
	 * @date 2015-12-29下午3:34:29
	 * @return
	 */
	public Prot36CarInfoBean getCarInfo() {
		if (rJson == null || rJson.isEmpty() || !isSuccess()) {
			return null;
		}
		Gson gson = new Gson();
		Prot36CarInfoBean bean = gson.fromJson(rJson, Prot36CarInfoBean.class);
		return bean;
	}

	private void recProt(DataInputStream dis, short iTaskCode) throws IOException {
		recHeader(dis);
		if (isTaskCode(iTaskCode)) {
			setSuccess(true);
			int iBodySize = getiRecBodySize();
			rJson = getUTF8(dis, iBodySize - 2);
			recCheckCode(dis);
		} else {
			setSuccess(false);
			if (isFFFF()) {
				recFFFF(dis, iTaskCode);
			} else {

			}
		}

	}
	private void sendProt(DataOutputStream dos, short iTaskCode, String json) throws SocketTimeoutException,
			IOException {
		byte[] sendCarNums = string2bytes(json);
		short iBodySize = countBodySize(sendCarNums);
		sendHeader(dos, iTaskCode, iBodySize);
		dos.write(sendCarNums);
		sendCheckCode(dos);
	}
}
