package cn.net.cpzslibs.prot.handset;

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;

//汽配增加产品
public class Prot29AddCarProduct extends ProtBase {
	private final short iTaskCode29 = 29;
	private long iProductId = -1;

	/**
	 * 
	 * <br/>
	 * 2015-8-27下午1:48:05
	 * 
	 * @param create
	 * @param sName
	 *            产品名称
	 * @param sNId
	 *            产品型号
	 * @param sCarType
	 *            适配车型
	 * @param sOrigin
	 *            产地
	 * @param sUrl
	 *            地址
	 * @param sProducer
	 *            生产商
	 * @param sDesc
	 *            产品描述
	 * @param sSupplier
	 *            供货商
	 * @throws SocketTimeoutException
	 * @throws IOException
	 */
	public void dealProt(SocketCreate create, String sName, String sNId, String sCarType, String sOrigin, String sUrl,
                         String sProducer, String sDesc, String sSupplier) throws SocketTimeoutException, IOException {
		sendProt(create.getDos(), iTaskCode29, sName, sNId, sCarType, sOrigin, sUrl, sProducer, sDesc, sSupplier);
		recProt(create.getDis(), iTaskCode29);
	}

	public long getiProductId() {
		return iProductId;
	}

	private void recProt(DataInputStream dis, short iTaskCode) throws IOException {
		recHeader(dis);
		if (isFFFF()) {
			recFFFF(dis, iTaskCode);
		} else {
			iProductId = reciProductId(dis);
			recCheckCode(dis);
		}
	}

	private void sendProt(DataOutputStream dos, short iTaskCode, String sName, String sNId, String sCarType,
			String sOrigin, String sUrl, String sProducer, String sDesc, String sSupplier)
			throws SocketTimeoutException, IOException {
		byte[] names = string2bytes(sName);
		byte[] NIds = string2bytes(sNId);
		byte[] carTypes = string2bytes(sCarType);
		byte[] origins = string2bytes(sOrigin);
		byte[] urls = string2bytes(sUrl);
		byte[] producers = string2bytes(sProducer);
		byte[] descs = string2bytes(sDesc);
		byte[] suppliers = string2bytes(sSupplier);

		short iNameSize = (short) names.length;
		short iNidSize = (short) NIds.length;
		short iCarTypeSize = (short) carTypes.length;
		short iOriginSize = (short) origins.length;
		short iUrlSize = (short) urls.length;
		short iProducerSize = (short) producers.length;
		short iDescSize = (short) descs.length;
		short iSupplierSize = (short) suppliers.length;

		short iBodySize = (short) (countBodySize() + (short) 2 * 8 + iNameSize + iNidSize + iCarTypeSize + iOriginSize
				+ iUrlSize + iProducerSize + iDescSize + iSupplierSize);
		sendHeader(dos, iTaskCode, iBodySize);
		sendString(dos, iNameSize, names);
		sendString(dos, iNidSize, NIds);
		sendString(dos, iCarTypeSize, carTypes);
		sendString(dos, iOriginSize, origins);
		sendString(dos, iUrlSize, urls);
		sendString(dos, iProducerSize, producers);
		sendString(dos, iDescSize, descs);
		sendString(dos, iSupplierSize, suppliers);
		sendCheckCode(dos);
	}

	private void sendString(DataOutputStream dos, short size, byte[] sendBytes) throws IOException {
		dos.writeShort(size);
		dos.write(sendBytes);
	}
}
