package com.desfate.chart.ui.old.OTC.datas;



import com.desfate.chart.util.ByteUtils;

import java.io.UnsupportedEncodingException;
import java.nio.ByteOrder;

public class HistoryQuoteResponse {
	// typedef struct tagQUERY_CYCLE_RSP
	// {
	// int16_t iExchCode; // 交易所代码
	// char szSymbol[64]; // 商品代码
	// int16_t iType; // 类型
	// int16_t iRaidx; // 小数位数
	// int32_t iCount; // 记录数
	// CYCLE_DATA cycData[0]; // 周期数据
	// } QUERY_CYCLE_RSP;
	// 交易所代码

	// 一笔周期数据长度
	private static int CYCLE_LENGTH = 36;
	
	// 一笔tik数据的长度
	private static int TIK_LENGTH = 40;
	
	// 历史数据抬头的长度
	private static int HEAD_LENGTH = 74;


    // 交易所代码  2
	private short exchangeCode;

	// 商品代码 64
	private String goodsCode;

	// 数据类型 2
	private short type;

	// 小数位数 2
	private short decimalPlace;

	// 记录数 4
	private int count;

	// 历史数据 
	private byte[] datas;
	

	public short getExchangeCode() {
		return exchangeCode;
	}



	public void setExchangeCode(short exchangeCode) {
		this.exchangeCode = exchangeCode;
	}



	public String getGoodsCode() {
		return goodsCode;
	}



	public void setGoodsCode(String goodsCode) {
		this.goodsCode = goodsCode;
	}



	public short getType() {
		return type;
	}



	public void setType(short type) {
		this.type = type;
	}



	public short getDecimalPlace() {
		return decimalPlace;
	}



	public void setDecimalPlace(short decimalPlace) {
		this.decimalPlace = decimalPlace;
	}



	public int getCount() {
		return count;
	}



	public void setCount(int count) {
		this.count = count;
	}



	public byte[] getContent() {
		return datas;
	}



	public void setContent(byte[] datas) {
		this.datas = datas;
	}


    /**
	 * 将二进制数据转换为历史请求返回对象
	 * 这里只取出数据的头和数据体
	 * @param content
	 *            二进制数据
	 * @return
	 */
	public static HistoryQuoteResponse toObject(byte[] content) {
		if (content == null) {
			return null;
		}

		// 如果小于最小长度 直接返回
		if (content.length < HEAD_LENGTH) {
			return null;
		}

		HistoryQuoteResponse response = new HistoryQuoteResponse();
		int index = 0;
		byte[] block;
		response.exchangeCode = ByteUtils.bytesToShort(content, index, ByteOrder.BIG_ENDIAN);
		index += 2;
		block = new byte[64];
		System.arraycopy(content, index, block, 0, block.length);
		int zeroIndex = -1;
		for (int i = 0;i <block.length; i++) {
			if(block[i] == 0){
				zeroIndex = i;
				break;
			}
		}
		if(zeroIndex > 0){
			block = new byte[zeroIndex + 1];
			System.arraycopy(content, index, block, 0, block.length);
		}
		try {
			response.goodsCode = new String(block, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}

		index += 64;
		response.type = ByteUtils.bytesToShort(content, index, ByteOrder.BIG_ENDIAN);
		index += 2;
		response.decimalPlace = ByteUtils.bytesToShort(content, index, ByteOrder.BIG_ENDIAN);
		index += 2;
		response.count = ByteUtils.bytesToInt(content, index, ByteOrder.BIG_ENDIAN);
		index += 4;

		if (response.count != 0) {
			if (content.length > HEAD_LENGTH) {
				response.datas = new byte[content.length - HEAD_LENGTH];
				System.arraycopy(content, index, response.datas, 0, response.datas.length);
			}
		}

		return response;
	}
}
